【Undoubtedly Lucky Numbers】【CodeForces - 244B】(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
Polycarpus has integer n. He wants to know how many positive integers that do not exceed n
Input
The first line contains a single integer n (1 ≤ n ≤ 109) — Polycarpus's number.
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,問小於等於n有多少個幸運數字,幸運數字的定義就是其只由1或者2位數字迴圈組成的,因為之前做過類似的題目,就是dfs把每位的情況都實現出來,雖然n的資料範圍達到了1e9,然而實際數字數目遠遠小於這個,就不用擔心會爆記憶體了。
ac程式碼:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+1000;
set<ll> a;
int n,x,y;
void dfs(ll num,int pos)
{
if(num>n||pos>10)
return;
a.insert(num);
dfs(num*10+x,pos+1);
dfs(num*10+y,pos+1);
}
int main()
{
scanf("%d",&n);
for(x=0;x<10;x++)
for(y=x+1;y<10;y++)
dfs(0,0);
printf("%d\n",a.size()-1);
}
//這裡偷懶使用了x y為全域性變數,使得dfs簡化了資料。