1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
#include <iostream> #include <sstream> #include <algorithm> #include <queue> #include <vector> #include <cstdlib> #include <cstdio> #include <string> #include <cstring> #include <ctime> #include <iomanip> #include <cmath> #include <set> #include <map> #include <list> #include <stack> using namespace std; #define MAXN 1000000000 vector<long long> super; void gen(long long num,long long seed1,long long seed2,bool sp){ if (num > MAXN) { return; } if (sp) { gen(seed1*10+seed1, seed1, 0, false); gen(seed1*10, seed1, 0, false); }else{ //cout << num << endl; super.push_back(num); gen(num*10+seed1,seed1,seed2,false); gen(num*10+seed2,seed1,seed2,false); } } int main(){ for (int i = 1; i <= 9; i++) { for (int j = i+1;j <= 9; j++) { gen(0, i, j, false); } } for (int i = 1; i <= 9; i++) { gen(0, i, 0,true); } sort(super.begin(), super.end()); super.erase(unique(super.begin(), super.end()),super.end()); //cout << super.size() << endl; long long n = 0; while (cin >> n) { int res = 0; for (int i = 1; i < super.size(); i++) { if (super[i] <= n) { res++; } } cout << res << endl; } } |
把那些lucky numbers预先生成出来打进一个表里(预处理),接收到输入的时候然后只要查表就可以了。