Artifact 29d8b3c8147890dbd94157bb268f511e5543cd33
#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;
static const int MODVAL = 1000000007;
bool is_square_free(int n)
{
for(int p=2; p*p<=n; ++p)
if( n%(p*p) == 0 )
return false;
return true;
}
static const int SP[] = {2,3,5,7,11,13,17,19}; // <= sqrt(N)
static const int SPN = sizeof(SP)/sizeof(*SP);
int small_prime_mask(int n)
{
int m = 0;
for(int i=0; i<SPN; ++i)
if( n%SP[i] == 0 )
m |= 1<<i;
return m;
}
int big_prime_factor(int n)
{
for(int i=0; i<SPN; ++i)
while( n%SP[i] == 0 )
n /= SP[i];
return n;
}
class SquareFreeSets {
public:
int countPerfect(int N, int K)
{
// way[k][m] = # of ways to make a k-elem psf-set with small prime factors m
int way[501][1<<SPN] = {};
way[0][0] = 1;
for(int n=2; n<=N; ++n)
if( is_square_free(n) )
if( big_prime_factor(n) == 1 )
{
int mask = small_prime_mask(n);
for(int k=K; k>=1; --k)
for(int m=0; m<(1<<SPN); ++m)
if( (m|mask) == m )
way[k][m] = (way[k][m] + way[k-1][m&~mask]) % MODVAL;
}
else if( big_prime_factor(n) == n )
{
vector<int> mask;
for(int p=1; n*p<=N; ++p)
if( is_square_free(p) )
mask.push_back( small_prime_mask(p) );
for(int k=K; k>=1; --k)
for(int m=0; m<(1<<SPN); ++m)
for(int j=0; j<mask.size(); ++j)
if( (m|mask[j]) == m )
way[k][m] = (way[k][m] + way[k-1][m&~mask[j]]) % MODVAL;
}
// summing up
int sum = 0;
for(int k=1; k<=K; ++k)
for(int m=0; m<(1<<SPN); ++m)
sum = (sum + way[k][m]) % MODVAL;
return sum;
}
};
// 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> 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(); }
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;}
template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
int N = 5;
int K = 1;
int RetVal = 3;
return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); }
int Test_(Case_<1>) {
int N = 5;
int K = 2;
int RetVal = 6;
return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); }
int Test_(Case_<2>) {
int N = 5;
int K = 3;
int RetVal = 7;
return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); }
int Test_(Case_<3>) {
int N = 6;
int K = 3;
int RetVal = 9;
return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); }
int Test_(Case_<4>) {
int N = 28;
int K = 41;
int RetVal = 1599;
return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); }
int Test_(Case_<5>) {
int N = 500;
int K = 500;
int RetVal = 59964104;
return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); }
int Test_(Case_<6>) {
int N = 100;
int K = 1;
int RetVal = 60;
return verify_case(RetVal, SquareFreeSets().countPerfect(N, K)); }
template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<> void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE