244B】Undoubtedly Lucky Numbers (dfs打表 + 二分)
題幹:
Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x = 4, and y = 7, then numbers 47, 744, 4 are lucky.
Let's call a positive integer a undoubtedly lucky, if there are such digits x
Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number.
Input
The first line contains a single integer n
Output
Print a single integer that says, how many positive integers that do not exceed nare undoubtedly lucky.
Examples
Input
10
Output
10
Input
123
Output
113
Note
In the first test sample all numbers that do not exceed 10 are undoubtedly lucky.
In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky.
題目大意:
輸入一個n,定義Undoubtedly Lucky 數字是僅由小於等於兩種數字組成的。問你有多少個 小於等於n的 是Undoubtedly Lucky 的數字。
解題報告:
寫法很多啊這題也可以直接用set去重,,,dfs別寫萎了就行了。。然後注意這題二分用upper_bound不能用lowerbound。。(因為要分很多種情況,,比如包含這個點或者不包含這個點)
AC程式碼:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll up;
int tot;
ll a[MAX*100];
void dfs(ll cur,ll wei,ll i,ll j) {
if(wei > 10 /*|| cur > up*/) return ;
a[++tot] = cur;
// if(cur == 101) {
// putchar('a');
// }
dfs(cur*10+i,wei+1,i,j);
dfs(cur*10+j,wei+1,i,j);
}
int main()
{
cin>>up;
for(int i = 0; i<=9; i++) {
for(int j = i+1; j<=9; j++) {
dfs(0,0,i,j);
}
}
sort(a+1,a+tot+1);
int len = unique(a+1,a+tot+1) - a-1;
// printf("len = %d\n",len);
//// for(int i = 1; i<=len; i++) printf("%lld ",a[i]);
// puts("");
printf("%d\n",upper_bound(a+1,a+len+1,up) - a - 1 -1);
return 0 ;
}
總結:
至於為什麼main函式的內層for迴圈為什麼要從i+1開始,這是因為我們現在是列舉的那兩個數字,這樣會保證沒有重複啊,不然肯定會多搜一倍的東西。。