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 int MODVAL = 1000000007; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: bool is_square_free(int n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int p=2; p*p<=n; ++p) 4fd800b3a8 2011-02-23 kinaba: if( n%(p*p) == 0 ) 4fd800b3a8 2011-02-23 kinaba: return false; 4fd800b3a8 2011-02-23 kinaba: return true; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const int SP[] = {2,3,5,7,11,13,17,19}; // <= sqrt(N) 4fd800b3a8 2011-02-23 kinaba: static const int SPN = sizeof(SP)/sizeof(*SP); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int small_prime_mask(int n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int m = 0; 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<SPN; ++i) 4fd800b3a8 2011-02-23 kinaba: if( n%SP[i] == 0 ) 4fd800b3a8 2011-02-23 kinaba: m |= 1<<i; 4fd800b3a8 2011-02-23 kinaba: return m; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int big_prime_factor(int n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int i=0; i<SPN; ++i) 4fd800b3a8 2011-02-23 kinaba: while( n%SP[i] == 0 ) 4fd800b3a8 2011-02-23 kinaba: n /= SP[i]; 4fd800b3a8 2011-02-23 kinaba: return n; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class SquareFreeSets { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int countPerfect(int N, int K) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // way[k][m] = # of ways to make a k-elem psf-set with small prime factors m 4fd800b3a8 2011-02-23 kinaba: int way[501][1<<SPN] = {}; 4fd800b3a8 2011-02-23 kinaba: way[0][0] = 1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int n=2; n<=N; ++n) 4fd800b3a8 2011-02-23 kinaba: if( is_square_free(n) ) 4fd800b3a8 2011-02-23 kinaba: if( big_prime_factor(n) == 1 ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int mask = small_prime_mask(n); 4fd800b3a8 2011-02-23 kinaba: for(int k=K; k>=1; --k) 4fd800b3a8 2011-02-23 kinaba: for(int m=0; m<(1<<SPN); ++m) 4fd800b3a8 2011-02-23 kinaba: if( (m|mask) == m ) 4fd800b3a8 2011-02-23 kinaba: way[k][m] = (way[k][m] + way[k-1][m&~mask]) % MODVAL; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: else if( big_prime_factor(n) == n ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: vector<int> mask; 4fd800b3a8 2011-02-23 kinaba: for(int p=1; n*p<=N; ++p) 4fd800b3a8 2011-02-23 kinaba: if( is_square_free(p) ) 4fd800b3a8 2011-02-23 kinaba: mask.push_back( small_prime_mask(p) ); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int k=K; k>=1; --k) 4fd800b3a8 2011-02-23 kinaba: for(int m=0; m<(1<<SPN); ++m) 4fd800b3a8 2011-02-23 kinaba: for(int j=0; j<mask.size(); ++j) 4fd800b3a8 2011-02-23 kinaba: if( (m|mask[j]) == m ) 4fd800b3a8 2011-02-23 kinaba: way[k][m] = (way[k][m] + way[k-1][m&~mask[j]]) % MODVAL; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // summing up 4fd800b3a8 2011-02-23 kinaba: int sum = 0; 4fd800b3a8 2011-02-23 kinaba: for(int k=1; k<=K; ++k) 4fd800b3a8 2011-02-23 kinaba: for(int m=0; m<(1<<SPN); ++m) 4fd800b3a8 2011-02-23 kinaba: sum = (sum + way[k][m]) % MODVAL; 4fd800b3a8 2011-02-23 kinaba: return sum; 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() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int N = 5; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 3; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int N = 5; 4fd800b3a8 2011-02-23 kinaba: int K = 2; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 6; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int N = 5; 4fd800b3a8 2011-02-23 kinaba: int K = 3; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 7; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int N = 6; 4fd800b3a8 2011-02-23 kinaba: int K = 3; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 9; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int N = 28; 4fd800b3a8 2011-02-23 kinaba: int K = 41; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1599; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: int N = 500; 4fd800b3a8 2011-02-23 kinaba: int K = 500; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 59964104; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: int N = 100; 4fd800b3a8 2011-02-23 kinaba: int K = 1; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 60; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: