-
Notifications
You must be signed in to change notification settings - Fork 4
/
gc3.cpp
369 lines (344 loc) · 6.35 KB
/
gc3.cpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include <stdio.h>
#include <gmpxx.h>
#include <set>
#include <vector>
#include <list>
#include <bitset>
#include <iostream>
#include <algorithm>
#define MAXR 1024
#ifndef N
#define N 2
#endif
typedef std::bitset<MAXR> dtype;
#define ONE ((dtype)1)
int isp[MAXR];
int pc;
int plist[MAXR];
bool operator<(const dtype& d1, const dtype& d2)
{
int i;
for(i=0;i<MAXR;i++){
bool b1=d1[i];
bool b2=d2[i];
if(!b1&&b2)return true;
if(b1&&!b2)return false;
}
return false;
}
struct Data{
mpz_class mul;
dtype nlist;
dtype sqrinfo;
bool operator<(const Data& d)const{
if(mul<d.mul)return true;
if(mul>d.mul)return false;
if(nlist<d.nlist)return true;
if(d.nlist<nlist)return false;
return sqrinfo<d.sqrinfo;
}
};
typedef std::set<Data> MySet;
dtype dec[MAXR];
int decc[MAXR][MAXR];
MySet alldata;
void initp()
{
int i,j;
for(i=2;i<MAXR;i++)isp[i]=1;
for(i=2;i<MAXR;i++){
if(isp[i]){
plist[pc++]=i;
for(j=i*i;j<MAXR;j+=i){
isp[j]=0;
}
}
}
for(i=1;i<MAXR;i++){
int x=i;
dtype r;
for(j=0;j<pc;j++){
if(plist[j]>x)break;
int jc=0;
while(x%plist[j]==0){
jc++;x/=plist[j];
}
decc[i][j]=jc;
if(jc&1){
r.set(j);
}
}
dec[i]=r;
}
}
mpz_class gprod;
std::vector<int> ilist;
mpz_class target;
std::vector<int> tlist;
long find(int starti, int level)
{
int i;
if(level==N){
if(gprod==target){
#if 0
std::vector<int> v1,v2;
for(i=0;i<N;i++){
v1.push_back(tlist[i]);
}
int j;
for(i=0,j=0;i<2*N;i++){
if(j>=N||ilist[i]!=tlist[j]){
v2.push_back(ilist[i]);
}else{
j++;
}
}
std::sort(v1.begin(),v1.end());
std::sort(v2.begin(),v2.end());
std::cout<<"{";
for(i=0;i<N;i++){
std::cout<<" "<<v1[i];
}
std::cout<<" }{";
for(i=0;i<N;i++){
std::cout<<" "<<v2[i];
}
std::cout<<" }"<<std::endl;
#endif
return 1;
}
return 0;
}
long r=0;
for(i=starti;i<2*N;i++){
gprod*=ilist[i];
tlist.push_back(ilist[i]);
if(target%gprod==0){
r += find(i+1,level+1);
}
tlist.pop_back();
gprod/=ilist[i];
}
return r;
}
int pcount[MAXR];
int maxdim;
long blen;
long *buf;
bool indexinrange(int c[MAXR])
{
int i;
for(i=0;i<maxdim;i++){
if(c[i]>pcount[i])return false;
}
return true;
}
long count2index(int c[MAXR])
{
int i;
long index=0;
for(i=0;i<maxdim;i++){
index*=pcount[i]+1;
index+=c[i];
}
return index;
}
bool gonext(int cindex[MAXR], int lc[MAXR])
{
int i;
for(i=maxdim-1;i>=0;i--){
if(cindex[i]<pcount[i]){
cindex[i]++;
return true;
}else{
cindex[i]=lc[i];
}
}
return false;
}
void addreduce(int x, int level, int t)
{
int lc[MAXR];
int cindex[MAXR];
int dindex[MAXR];
int i;
for(i=0;i<maxdim;i++){
lc[i]=decc[x][i]*t;
if(lc[i]>pcount[i])return;
cindex[i]=lc[i];
}
int sgn = -1;
if(t&1)sgn= 1;
do{
long cc = count2index(cindex);
for(i=0;i<maxdim;i++)dindex[i]=cindex[i]-lc[i];
long dd = count2index(dindex);
buf[level*blen+cc]+=sgn*buf[(level-t)*blen+dd];
}while(gonext(cindex, lc));
}
void reducelast(int x, int level)
{
int lc[MAXR];
int i;
for(i=0;i<maxdim;i++){
lc[i]=decc[x][i]*(level+1);
if(lc[i]>pcount[i])return;
}
int sgn = -1;
if((level+1)&1)sgn=1;
buf[level*blen+count2index(lc)]+=sgn;
}
long findhalf(const Data& d)
{
int i,j;
for(i=0;i<pc;i++)pcount[i]=0;
for(i=1;i<MAXR;i++){
if(d.nlist.test(i)){
for(j=0;j<pc;j++){
pcount[j]+=decc[i][j];
}
}
}
blen=1L;
for(i=pc-1;i>=0;i--)if(pcount[i]>0)break;
maxdim=i+1;
for(i=0;i<maxdim;i++)pcount[i]/=2;//find square root
for(i=0;i<maxdim;i++){
blen*=(pcount[i]+1);
if(blen>0x7FFFFFFFFFL){
std::cerr<<"Buffer len overflow\n";
exit(-1);
}
}
buf = new long[N*blen];
if(NULL == buf){
std::cerr<<"Out of memory\n";
exit(-1);
}
memset(buf,0,N*blen*sizeof(long));
gprod=1;
ilist.clear();
tlist.clear();
for(i=1;i<MAXR;i++){
if(d.nlist.test(i)){
ilist.push_back(i);
}
}
if(ilist.size()!=2*N){
std::cerr<<"Invalid count of integer\n";
exit(-1);
}
for(i=0;i<2*N;i++){
int v=ilist[i];
if(indexinrange(decc[v])){
long ind = count2index(decc[v]);
buf[ind]=1L;
}
}
for(j=1;j<N;j++){//cal for j+1 numbers
int t;
for(t=1;t<=j;t++){
for(i=0;i<2*N;i++){
addreduce(ilist[i],j,t);
}
}
for(i=0;i<2*N;i++){
reducelast(ilist[i],j);
}
for(i=0;i<blen;i++){
if(buf[j*blen+i]%(j+1)!=0){
std::cerr << "Invalid buffer data, not multiple of "<<j+1<<std::endl;
exit(-1);
}
buf[j*blen+i]/=j+1;
}
}
long r = buf[N*blen-1];
delete []buf;
return r;
}
int main()
{
int n,m;
initp();
Data d;
d.mul=1;
d.sqrinfo=0;
d.nlist=0;
for(m=1;m<=2*N;m++){
d.mul*=m;
d.nlist.flip(m);
d.sqrinfo^=dec[m];
}
alldata.insert(d);
auto it = alldata.begin();
while(1){
if(it->nlist==0){
std::cerr<<"overflow\n";
fflush(stderr);
break;//end of search
}
if(it->sqrinfo==0){//find result
std::cout<<"N="<<N<<std::endl;
std::cout<<sqrt(it->mul)<<"<";
auto nit=it;
long c=0;
while(nit!=alldata.end()&&nit->mul==it->mul){
// if(findhalf(*nit));
c++;
nit++;
}
std::cout<<c<<">"<<std::endl;
nit=it;
while(nit!=alldata.end()&&nit->mul==it->mul){
std::cout<<"{ ";
int ii;
for(ii=0;ii<MAXR;ii++){
if(nit->nlist.test(ii)){
std::cout<<ii<<" ";
}
}
std::cout<<"}<";
std::cout<<findhalf(*nit)/2;
std::cout<<">";
std::cout<<std::endl;
nit++;
}
std::cout<<std::endl;
fflush(stdout);
break;
}
for(n=MAXR-1;n>=0;n--){
if(it->nlist.test(n))break;//find last digit
}
if(n==MAXR-1){
d.nlist=0;//flag for end of search
d.mul=(it->mul/n)*m;
d.sqrinfo=0;
alldata.insert(d);
}else{
m=n+1;
d.nlist=it->nlist;
d.nlist.flip(n);d.nlist.flip(m);
d.mul=(it->mul/n)*m;
d.sqrinfo=it->sqrinfo^dec[n]^dec[m];
alldata.insert(d);
}
while(it->nlist.test(n))n--;
if(n>=1 && it->nlist.test(n-1)){
m=n-1;
d.nlist=it->nlist;
d.nlist.flip(n);d.nlist.flip(m);
d.mul=(it->mul/m)*n;
d.sqrinfo=it->sqrinfo^dec[n]^dec[m];
alldata.insert(d);
}
{
auto nit=it;
++nit;
alldata.erase(it);
it=nit;
}
}
return 0;
}