ba35b3a9cf 2011-06-25 kinaba: #include <iostream> ba35b3a9cf 2011-06-25 kinaba: #include <sstream> ba35b3a9cf 2011-06-25 kinaba: #include <iomanip> ba35b3a9cf 2011-06-25 kinaba: #include <vector> ba35b3a9cf 2011-06-25 kinaba: #include <string> ba35b3a9cf 2011-06-25 kinaba: #include <map> ba35b3a9cf 2011-06-25 kinaba: #include <set> ba35b3a9cf 2011-06-25 kinaba: #include <algorithm> ba35b3a9cf 2011-06-25 kinaba: #include <numeric> ba35b3a9cf 2011-06-25 kinaba: #include <iterator> ba35b3a9cf 2011-06-25 kinaba: #include <functional> ba35b3a9cf 2011-06-25 kinaba: #include <complex> ba35b3a9cf 2011-06-25 kinaba: #include <queue> ba35b3a9cf 2011-06-25 kinaba: #include <stack> ba35b3a9cf 2011-06-25 kinaba: #include <cmath> ba35b3a9cf 2011-06-25 kinaba: #include <cassert> ba35b3a9cf 2011-06-25 kinaba: #include <cstring> ba35b3a9cf 2011-06-25 kinaba: using namespace std; ba35b3a9cf 2011-06-25 kinaba: typedef long long LL; ba35b3a9cf 2011-06-25 kinaba: typedef complex<double> CMP; ba35b3a9cf 2011-06-25 kinaba: ba35b3a9cf 2011-06-25 kinaba: class TheAlmostLuckyNumbersDivOne { public: ba35b3a9cf 2011-06-25 kinaba: LL rec(LL n, int miss_atmost=1, LL prefix=0) ba35b3a9cf 2011-06-25 kinaba: { ba35b3a9cf 2011-06-25 kinaba: if( n<prefix || miss_atmost<0 ) ba35b3a9cf 2011-06-25 kinaba: return 0; ba35b3a9cf 2011-06-25 kinaba: LL cnt = (prefix==0?0:1); ba35b3a9cf 2011-06-25 kinaba: for(int i=(prefix==0?1:0); i<=9; ++i) ba35b3a9cf 2011-06-25 kinaba: cnt += rec(n, miss_atmost-(i==4||i==7?0:1), prefix*10+i); ba35b3a9cf 2011-06-25 kinaba: return cnt; ba35b3a9cf 2011-06-25 kinaba: } ba35b3a9cf 2011-06-25 kinaba: LL find(LL a, LL b) ba35b3a9cf 2011-06-25 kinaba: { ba35b3a9cf 2011-06-25 kinaba: return rec(b) - rec(a-1); ba35b3a9cf 2011-06-25 kinaba: LL cnt = 0; ba35b3a9cf 2011-06-25 kinaba: for(int len=1; len<=16; ++len) // 長さ len の ba35b3a9cf 2011-06-25 kinaba: for(int pat=0; pat<(1<<len); ++pat) // ビットパターンを全列挙 ba35b3a9cf 2011-06-25 kinaba: { ba35b3a9cf 2011-06-25 kinaba: // [0,1] -> [4,7] に変えれば lucky number の全生成 ba35b3a9cf 2011-06-25 kinaba: LL val = 0; ba35b3a9cf 2011-06-25 kinaba: for(int i=0; i<len; ++i) ba35b3a9cf 2011-06-25 kinaba: val = val*10 + (pat&(1<<i) ? 7 : 4); ba35b3a9cf 2011-06-25 kinaba: if( a<=val && val<=b ) ba35b3a9cf 2011-06-25 kinaba: ++cnt; ba35b3a9cf 2011-06-25 kinaba: ba35b3a9cf 2011-06-25 kinaba: // Almost Lucky Number は 1 箇所違う ba35b3a9cf 2011-06-25 kinaba: for(int m=0; m<len; ++m) if(pat&(1<<m)) // 7 を変えて ba35b3a9cf 2011-06-25 kinaba: for(int mTo=(m==0?1:0); mTo<=9; ++mTo) if(mTo!=4 && mTo!=7) // 0-3,5-6,8-9 にしてみる ba35b3a9cf 2011-06-25 kinaba: { ba35b3a9cf 2011-06-25 kinaba: LL val = 0; ba35b3a9cf 2011-06-25 kinaba: for(int i=0; i<len; ++i) ba35b3a9cf 2011-06-25 kinaba: val = val*10 + (i==m ? mTo : (pat&(1<<i)) ? 7 : 4); ba35b3a9cf 2011-06-25 kinaba: if( a<=val && val<=b ) ba35b3a9cf 2011-06-25 kinaba: ++cnt; ba35b3a9cf 2011-06-25 kinaba: } ba35b3a9cf 2011-06-25 kinaba: } ba35b3a9cf 2011-06-25 kinaba: return cnt; ba35b3a9cf 2011-06-25 kinaba: } ba35b3a9cf 2011-06-25 kinaba: }; ba35b3a9cf 2011-06-25 kinaba: ba35b3a9cf 2011-06-25 kinaba: // BEGIN CUT HERE ba35b3a9cf 2011-06-25 kinaba: #include <ctime> ba35b3a9cf 2011-06-25 kinaba: double start_time; string timer() ba35b3a9cf 2011-06-25 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } ba35b3a9cf 2011-06-25 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) ba35b3a9cf 2011-06-25 kinaba: { os << "{ "; ba35b3a9cf 2011-06-25 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) ba35b3a9cf 2011-06-25 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } ba35b3a9cf 2011-06-25 kinaba: void verify_case(const long long& Expected, const long long& Received) { ba35b3a9cf 2011-06-25 kinaba: bool ok = (Expected == Received); ba35b3a9cf 2011-06-25 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; ba35b3a9cf 2011-06-25 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } ba35b3a9cf 2011-06-25 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); ba35b3a9cf 2011-06-25 kinaba: #define END verify_case(_, TheAlmostLuckyNumbersDivOne().find(a, b));} ba35b3a9cf 2011-06-25 kinaba: int main(){ ba35b3a9cf 2011-06-25 kinaba: ba35b3a9cf 2011-06-25 kinaba: CASE(0) ba35b3a9cf 2011-06-25 kinaba: long long a = 4LL; ba35b3a9cf 2011-06-25 kinaba: long long b = 7LL; ba35b3a9cf 2011-06-25 kinaba: long long _ = 4LL; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(1) ba35b3a9cf 2011-06-25 kinaba: long long a = 8LL; ba35b3a9cf 2011-06-25 kinaba: long long b = 19LL; ba35b3a9cf 2011-06-25 kinaba: long long _ = 4LL; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(2) ba35b3a9cf 2011-06-25 kinaba: long long a = 28LL; ba35b3a9cf 2011-06-25 kinaba: long long b = 33LL; ba35b3a9cf 2011-06-25 kinaba: long long _ = 0LL; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(3) ba35b3a9cf 2011-06-25 kinaba: long long a = 12345678900LL; ba35b3a9cf 2011-06-25 kinaba: long long b = 98765432100LL; ba35b3a9cf 2011-06-25 kinaba: long long _ = 91136LL; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(4) ba35b3a9cf 2011-06-25 kinaba: long long a = 1LL; ba35b3a9cf 2011-06-25 kinaba: long long b = 10000000000000000LL; ba35b3a9cf 2011-06-25 kinaba: long long _ = -1LL; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(5) ba35b3a9cf 2011-06-25 kinaba: long long a = 1LL; ba35b3a9cf 2011-06-25 kinaba: long long b = 1LL; ba35b3a9cf 2011-06-25 kinaba: long long _ = 1LL; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: CASE(6) ba35b3a9cf 2011-06-25 kinaba: long long a = 1LL; ba35b3a9cf 2011-06-25 kinaba: long long b = 99LL; ba35b3a9cf 2011-06-25 kinaba: long long _ = -1LL; ba35b3a9cf 2011-06-25 kinaba: END ba35b3a9cf 2011-06-25 kinaba: ba35b3a9cf 2011-06-25 kinaba: } ba35b3a9cf 2011-06-25 kinaba: // END CUT HERE