QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#55073#1860. Historic BreakthroughyyyyxhAC ✓75ms3144kbC++113.6kb2022-10-12 08:45:072022-10-12 08:45:10

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-12 08:45:10]
  • 评测
  • 测评结果:AC
  • 用时:75ms
  • 内存:3144kb
  • [2022-10-12 08:45:07]
  • 提交

answer

#include <cstdio>
#include <random>
#include <algorithm>
using namespace std;
typedef __uint128_t ull;
char stk[100],*tp;
mt19937_64 rng(random_device{}());
ull rd(ull lim){return (ull(rng())<<64|rng())%lim+1;}
template<typename T>
void write(T x){
	tp=stk;
	if(!x) putchar('0');
	while(x) *++tp=(x%10)^48,x/=10;
	while(tp!=stk) putchar(*tp--);
	putchar('\n');
}
template<typename T>
void read(T &x){
	x=0;char c=getchar();
	while(c<48||c>57) c=getchar();
	do x=(x<<1)+(x<<3)+(c^48),c=getchar();
	while(c>=48&&c<=57);
}
ull mul(ull a,ull b,ull p){
	ull res=0;
	while(b){
		res=(res+(b%10)*a%p)%p;
		a=10*a%p;b/=10;
	}
	return res;
}
ull qpow(ull a,ull b,ull p){
	ull res=1;
	while(b){
		if(b&1) res=mul(res,a,p);
		a=mul(a,a,p);b>>=1;
	}
	return res;
}
ull qpow(ull a,int b){
	ull res=1;
	while(b){
		if(b&1) res*=a;
		a*=a;b>>=1;
	}
	return res;
}
bool is_prime(ull n){
	if(n==2||n==3||n==5||n==7) return 1;
	if(n%2==0||n%3==0||n%5==0||n%7==0) return 0;
	ull e=n-1;int s=0;
	while(~e&1) e>>=1,++s;
	for(int it=0;it<10;++it){
		ull cur=qpow(rd(n-1),e,n);
		if(cur==1) continue;
		for(int jt=0;jt<s;++jt){
			ull nxt=mul(cur,cur,n);
			if(nxt==1&&cur!=n-1) return 0;
			cur=nxt;
			if(cur==1) break;
		}
		if(cur!=1) return 0;
	}
	return 1;
}
ull gcd(ull a,ull b){
	if(!b) return a;
	return gcd(b,a%b);
}
ull m,ex,num;
ull pr_list[100003];
int rk;
void get_factor(ull);
void factorize(ull n){
	if(n==1) return;
	ull l,r;
	l=211;r=1414213562373095168;
	while(l<=r){
		ull x=(l+r)>>1;
		ull cur=x*x;
		if(cur==n) return factorize(x);
		if(cur<n) l=x+1;
		if(cur>n) r=x-1;
	}
	l=211;r=1259921049895;
	while(l<=r){
		ull x=(l+r)>>1;
		ull cur=x*x*x;
		if(cur==n) return factorize(x);
		if(cur<n) l=x+1;
		if(cur>n) r=x-1;
	}
	l=211;r=18205643;
	while(l<=r){
		ull x=(l+r)>>1;
		ull cur=qpow(x,5);
		if(cur==n) return factorize(x);
		if(cur<n) l=x+1;
		if(cur>n) r=x-1;
	}
	l=211;r=153413;
	while(l<=r){
		ull x=(l+r)>>1;
		ull cur=qpow(x,7);
		if(cur==n) return factorize(x);
		if(cur<n) l=x+1;
		if(cur>n) r=x-1;
	}
	l=211;r=1996;
	while(l<=r){
		ull x=(l+r)>>1;
		ull cur=qpow(x,11);
		if(cur==n) return factorize(x);
		if(cur<n) l=x+1;
		if(cur>n) r=x-1;
	}
	l=211;r=620;
	while(l<=r){
		ull x=(l+r)>>1;
		ull cur=qpow(x,13);
		if(cur==n) return factorize(x);
		if(cur<n) l=x+1;
		if(cur>n) r=x-1;
	}
	if(is_prime(n)) pr_list[rk++]=n;
	else get_factor(n);
}
void get_factor(ull n){
	ull a;
	while(1){
		a=rd(n-1);
		while(gcd(a,n)>1) a=rd(n-1);
		ull ans=qpow(a,m,n);
		if(ans==1) break;
		else return factorize(gcd(n,ans-1));
	}
	ull cur=qpow(a,ex,n);
	if(cur==1) return get_factor(n);
	ull nxt=mul(cur,cur,n);
	while(nxt!=1) cur=nxt,nxt=mul(cur,cur,n);
	if(cur==n-1) return get_factor(n);
	ull fact=gcd(cur-1,n);
	factorize(fact);factorize(n/fact);
}
const int small_pr[46]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61
					  ,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137
					  ,139,149,151,157,163,167,173,179,181,191,193,197,199};
void solve(){
	read(m);m<<=1;ex=num=m;
	rk=0;while(~ex&1) ex>>=1;
	for(int it=0;it<46;++it){
		int cpr=small_pr[it];
		if(num%cpr) continue;
		pr_list[rk++]=cpr;
		while(num%cpr==0) num/=cpr;
	}
	factorize(num);
	sort(pr_list,pr_list+rk);
	rk=unique(pr_list,pr_list+rk)-pr_list;
	reverse(pr_list,pr_list+rk);
	ull res=1;
	for(int i=0;i<rk;++i){
		ull cpr=pr_list[i];
		if(m%cpr) continue;
		int cnt=0;
		while(m%cpr==0) m/=cpr,++cnt;
		m/=cpr-1;res*=qpow(cpr,(cnt+1)>>1);
	}
	write(res);
}
int main(){
	int tc;read(tc);
	while(tc--) solve();
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 4ms
memory: 2988kb

input:

3
20
21
475750381222420656586462245096576000

output:

10
7
1497700821900508526

result:

ok 3 number(s): "10 7 1497700821900508526"

Test #2:

score: 0
Accepted
time: 69ms
memory: 3008kb

input:

51
348387408908517538156281238966340503
269891120302452381431351214335847781
747207543121624879797402427368860
500118165772005573992050274078796601
376563350255195175098956276198783051
855996192374691515214841787085600
491448606692239765059794615991064231
123619467864337410141102480048000000
7114827...

output:

834730386302688203
734698741393303847
38657773487574029
1000118158791255599
867828727636041299
41376351752391209
991411727479799147
819415677966571060
533472702079376326
419694411774324997
119851595982618319
24024477947405473
730267680763188643
269435681305057117
809387811759102827
29392724088327775...

result:

ok 51 numbers

Test #3:

score: 0
Accepted
time: 59ms
memory: 3144kb

input:

50
590955592280751522125185760551589472
450753984250583112023852704149662080
196704025354160782063198166237303808
382785853244719627595443384812477912
40522659517926585041466149305181616
26478235572251423131073298958930080
490320199321080674802144988571268192
110281951063110963040645709560838400
948...

output:

1331689688366264319
949479804339042269
1090685758659983022
945075124476539231
434862903013111412
398589648217243506
1012639928054749381
699351669356204744
543210198772784757
1132463196576070170
848907776403266445
1930255754904417056
1189528317659705086
686463867402133171
102891055039662950
182071571...

result:

ok 50 numbers

Test #4:

score: 0
Accepted
time: 57ms
memory: 2992kb

input:

50
276259542446423651625925027373027232
393684955823722570867768699571572176
857720676067612917869599746201489600
17110455728330975252747068107907200
542281457444687912305057901366280320
2654306616117951734640416629182720
322861266726126116829643702477914336
298853927939020940780632877440807680
7898...

output:

1293520230715335156
1086778493949362559
1464686748629892505
190080489690965899
1545864800321934334
76672170019366097
1024398581737711713
1096526389684540348
2349064908930748272
50307494154045329
445092096339592380
1435004850383139296
1529324330815083956
2097596248514948892
760541100765245579
3818739...

result:

ok 50 numbers

Test #5:

score: 0
Accepted
time: 75ms
memory: 2984kb

input:

50
453008189735946954708861108359363203
551025652219715865084914059564383721
786164844307406583446593304065657003
610291465035142731460915809600409753
706864586054180662022440079345324653
570551915704950184495149575882325703
864916087207438864260538795023947461
421455605824822507806251352877855381
3...

output:

951848926811336963
1049786313703618319
1253925710963298323
1104799950249041903
1189003436541863543
1068224616553045163
1315230844534478567
918101961467050247
802814943898092227
762571582574907779
831979843661865359
797606718229530359
938154503358815423
1303037683800527639
793773369441477119
14021898...

result:

ok 50 numbers

Test #6:

score: 0
Accepted
time: 72ms
memory: 3116kb

input:

50
1300378470060305026424038382191232
6956378996245323843606514078615500
589244226744677712771854578698400
237091357130763153045978263910123672
161751450022115587601924824730219160
132669464182049885124281942384188456
67134267644722497849437741098688712
286785555483759509945063633526861
327655419420...

output:

51004138467328681
117952585187174711
34329342804633713
688610038029305417
568773901505426053
515110919481552113
366427282885665097
23949386230845223
255990812380074931
48745809601284947
45479093200495939
363088169939630143
116834934318262613
311344543295176663
115798704650850539
1071834160097733031
...

result:

ok 50 numbers