Undoubtedly Lucky Numbers
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 and y (0 ≤ x, y ≤ 9), that the decimal representation of number a (without leading zeroes) contains only digits x and y.
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 (1 ≤ n ≤ 109) — Polycarpus’s number.
Output
Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky.
题目大意为给你数字 n ,求在1-n的范围内,只包含两个不同数字的数的个数;
这题比较巧妙的是dfs,每次在数的尾部添加一个数,每次有两种数字可以添加,这样暴力更省时间;
代码:
#include<bits/stdc++.h>
#define ll long long
#define pa pair<int,int>
#define lson k<<1
#define rson k<<1|1
#define inf 0x3f3f3f3f
//ios::sync_with_stdio(false);
using namespace std;
const int N=100100;
const int M=1000100;
const ll mod=1e9+7;
int a[3];
int n;
set<ll>se;
void dfs(ll p){
for(int i=1;i<=2;i++){
ll q=p;
q=q*10+(ll)a[i];
if(q<=n&&q){
se.insert(q);
dfs(q);
}
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n;
for(int i=0;i<=9;i++){
for(int j=i+1;j<=9;j++){
a[1]=i,a[2]=j;
dfs(0);
}
}
cout<<se.size()<<endl;
return 0;
}