4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const LL MODVAL = 1000000007; 4fd800b3a8 2011-02-23 kinaba: typedef vector< vector<LL> > matrix; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<LL> vMul( const matrix& A, const vector<LL>& B ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const int n = A.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<LL> C(n); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<n; ++j) 4fd800b3a8 2011-02-23 kinaba: C[i] = (C[i] + A[i][j] * B[j]) % MODVAL; 4fd800b3a8 2011-02-23 kinaba: return C; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: matrix mMul( const matrix& A, const matrix& B ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: const int n = A.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: matrix C(n, vector<LL>(n)); 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<n; ++i) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<n; ++j) { 4fd800b3a8 2011-02-23 kinaba: LL Cij = 0; 4fd800b3a8 2011-02-23 kinaba: for(int k=0; k<n; ++k) 4fd800b3a8 2011-02-23 kinaba: Cij = (Cij + A[i][k] * B[k][j]) % MODVAL; 4fd800b3a8 2011-02-23 kinaba: C[i][j] = Cij; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return C; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: matrix mPow( matrix M, LL t ) // t>0 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: matrix R; 4fd800b3a8 2011-02-23 kinaba: for(; t; t>>=1, M=mMul(M,M)) 4fd800b3a8 2011-02-23 kinaba: if( t&1 ) 4fd800b3a8 2011-02-23 kinaba: R = (R.empty() ? M : mMul(R,M)); 4fd800b3a8 2011-02-23 kinaba: return R; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL GCD(LL a, LL b) { while(a) swap(b%=a,a); return b; } 4fd800b3a8 2011-02-23 kinaba: LL LCM(LL a, LL b) { return a/GCD(a,b)*b; } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class AvoidFour { public: 4fd800b3a8 2011-02-23 kinaba: int count(long long n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: matrix A(6, vector<LL>(6)); 4fd800b3a8 2011-02-23 kinaba: A[1][0] = 8; A[1][1] = 9; A[1][2] = 9; A[1][3] = 9; A[1][4] = 9; 4fd800b3a8 2011-02-23 kinaba: A[2][0] = 1; A[2][1] = 1; 4fd800b3a8 2011-02-23 kinaba: A[3][2] = 1; 4fd800b3a8 2011-02-23 kinaba: A[4][3] = 1; 4fd800b3a8 2011-02-23 kinaba: A[5][1] = 1; A[5][2] = 1; A[5][3] = 1; A[5][4] = 1; A[5][5] = 1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<LL> v(6); 4fd800b3a8 2011-02-23 kinaba: v[0] = 1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<LL> fours; 4fd800b3a8 2011-02-23 kinaba: for(LL f=44; f<=n; f=f*10+4) 4fd800b3a8 2011-02-23 kinaba: fours.push_back(f); 4fd800b3a8 2011-02-23 kinaba: int N = fours.size(); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL total = 0; 4fd800b3a8 2011-02-23 kinaba: for(int m=0; m<(1<<N); ++m) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL lcm=1, bitCnt=0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; (1<<i)<=m; ++i) 4fd800b3a8 2011-02-23 kinaba: if( m & (1<<i) ) { 4fd800b3a8 2011-02-23 kinaba: lcm = LCM(lcm, fours[i]); 4fd800b3a8 2011-02-23 kinaba: if( lcm > n ) 4fd800b3a8 2011-02-23 kinaba: break; 4fd800b3a8 2011-02-23 kinaba: bitCnt ++; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: if( lcm <= n ) { 4fd800b3a8 2011-02-23 kinaba: matrix Af = mPow(A, lcm); 4fd800b3a8 2011-02-23 kinaba: Af[5] = A[5]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL sub = vMul(mMul(A,mPow(Af,n/lcm)), v)[5]; 4fd800b3a8 2011-02-23 kinaba: total = (bitCnt&1 ? total-sub+MODVAL : total+sub) % MODVAL; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return total; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, AvoidFour().count(n));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: long long n = 4LL; 4fd800b3a8 2011-02-23 kinaba: int _ = 9998; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: long long n = 5LL; 4fd800b3a8 2011-02-23 kinaba: int _ = 99980; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: long long n = 87LL; 4fd800b3a8 2011-02-23 kinaba: int _ = 576334228; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: long long n = 88LL; 4fd800b3a8 2011-02-23 kinaba: int _ = 576334228; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: long long n = 4128LL; 4fd800b3a8 2011-02-23 kinaba: int _ = 547731225; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE