-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoinJam.cpp
More file actions
135 lines (132 loc) · 2.55 KB
/
CoinJam.cpp
File metadata and controls
135 lines (132 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
* Google Code Jam 2016 Online Qualification Round 3
* Coded by Ziyi Tang, CS480 S16 Algorithmic Problem Solving, New York University
*
*/
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <cmath>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
using namespace std;
typedef unsigned long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
typedef vector<pi> vpi;
typedef vector<vpi> vvpi;
const int INF = (int)1E9;
const long INFL = (long)1E18;
const int dir[4][2] = {{-1,0},{0,1},{1,0},{0,-1}};
#define REP(i,s,t) for(int i=(s);i<(t);i++)
#define FILL(x,v) memset(x,v,sizeof(x))
#define MAXN 1000000010
#define MAXM 1000000
int n,j;
ll dig[12][20];
ll _sieve_size;
bitset<MAXN> bs;
vi primes;
vector<int> re;
void sieve(ll upperbound){
_sieve_size = upperbound + 1;
bs.set();
bs[0] = bs[1] = 0;
for(ll i = 2; i <= _sieve_size; i++) if (bs[i]) {
for (ll j = i * i; j <= _sieve_size; j += i)
bs[j] = 0;
primes.push_back((int)i);
}
}
bool isPrime(ll N){
// note: only work for N <= (last prime in vi "primes")^2
if (N <= _sieve_size) return bs[N];
for (int i = 0; i < (int)primes.size(); i++)
if (N % primes[i] == 0)
return false;
return true;
}
ll power(ll base, ll p){
if(p == 0) return 1LL;
ll tmp = power(base,p/2);
tmp = tmp*tmp;
if(p&1)
tmp*=base;
return tmp;
}
bool check(ll cur){
REP(i,2,11){
ll sum = 0LL;
REP(j,0,n){
if(cur & (1 << j)){
sum+=dig[i][j];
}
}
//cout << sum << endl;
if(isPrime(sum)) return false;
int sz = primes.size();
//cout << sum << endl;
REP(i,0,sz){
if(sum%primes[i] == 0){
re.push_back(primes[i]);
break;
}
}
}
return true;
}
void ConvertToBinary(int n)
{
if (n / 2 != 0) {
ConvertToBinary(n / 2);
}
printf("%d", n % 2);
}
int main(){
int test;
cin >> test;
sieve(MAXN);
REP(cas,1,test+1){
re.clear();
ll now = 0LL;
//cout << "go" << endl;
REP(i,2,11){
REP(j,0,20){
dig[i][j] = power(i,j);
//cout << dig[i][j] << endl;
}
}
cin >> n >> j;
ll cur = (1 << (n-1));
int cont = 0;
printf("Case #%d:\n", cas);
while(cur <= (1 << n)-1){
if(!(cur&1)) cur|=1;
if(check(cur)){
cont++;
ConvertToBinary(cur);
REP(i,2,11){
cout << " ";
cout << re[i-2];
}
//cout << "sz" << re.size() << endl;
cout << endl;
}
cur+=1;
re.clear();
if(cont == j) break;
}
}
return 0;
}