Artifact Content
Not logged in

Artifact 4b8ef53b93c8e743139ed9fdf35e21a4ae4d63fa


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class TheAlmostLuckyNumbersDivOne { public:
	LL rec(LL n, int miss_atmost=1, LL prefix=0)
	{
		if( n<prefix || miss_atmost<0 )
			return 0;
		LL cnt = (prefix==0?0:1);
		for(int i=(prefix==0?1:0); i<=9; ++i)
			cnt += rec(n, miss_atmost-(i==4||i==7?0:1), prefix*10+i);
		return cnt;
	}
	LL find(LL a, LL b)
	{
		return rec(b) - rec(a-1);
		LL cnt = 0;
		for(int len=1; len<=16; ++len) // 長さ len の
			for(int pat=0; pat<(1<<len); ++pat) // ビットパターンを全列挙
			{
				// [0,1] -> [4,7] に変えれば lucky number の全生成
				LL val = 0;
				for(int i=0; i<len; ++i)
					val = val*10 + (pat&(1<<i) ? 7 : 4);
				if( a<=val && val<=b )
					++cnt;

				// Almost Lucky Number は 1 箇所違う
				for(int m=0; m<len; ++m) if(pat&(1<<m)) // 7 を変えて
					for(int mTo=(m==0?1:0); mTo<=9; ++mTo) if(mTo!=4 && mTo!=7) // 0-3,5-6,8-9 にしてみる
					{
						LL val = 0;
						for(int i=0; i<len; ++i)
							val = val*10 + (i==m ? mTo : (pat&(1<<i)) ? 7 : 4);
						if( a<=val && val<=b )
							++cnt;
					}
			}
		return cnt;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, TheAlmostLuckyNumbersDivOne().find(a, b));}
int main(){

CASE(0)
	long long a = 4LL; 
	long long b = 7LL; 
	long long _ = 4LL; 
END
CASE(1)
	long long a = 8LL; 
	long long b = 19LL; 
	long long _ = 4LL; 
END
CASE(2)
	long long a = 28LL; 
	long long b = 33LL; 
	long long _ = 0LL; 
END
CASE(3)
	long long a = 12345678900LL; 
	long long b = 98765432100LL; 
	long long _ = 91136LL; 
END
CASE(4)
	long long a = 1LL; 
	long long b = 10000000000000000LL; 
	long long _ = -1LL; 
END
CASE(5)
	long long a = 1LL; 
	long long b = 1LL; 
	long long _ = 1LL; 
END
CASE(6)
	long long a = 1LL; 
	long long b = 99LL; 
	long long _ = -1LL; 
END

}
// END CUT HERE