QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#54242#1860. Historic BreakthroughzhouhuanyiAC ✓102ms12448kbC++113.3kb2022-10-07 16:59:522022-10-07 16:59:54

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-07 16:59:54]
  • 评测
  • 测评结果:AC
  • 用时:102ms
  • 内存:12448kb
  • [2022-10-07 16:59:52]
  • 提交

answer

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<random>
#include<map>
#include<cassert>
#define N 1000000
#define inf 1e9
#define INF 2e18
using namespace std;
mt19937 RAND(0);
__int128 read()
{
	char c=0;
	__int128 sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
struct node
{
	__int128 num,data;
};
node tong[N+1];
int T,length,maxn=1000000000,cnt,v[N+1],st[N+1],leng;
bool nprime[N+1];
__int128 n,m,sm,pw[N+1],maxinf=(__int128)(1e36);
map<__int128,int>p;
void write(__int128 x)
{
	if (!x) return;
	write(x/10),printf("%d",(int)(x%10));
	return;
}
__int128 fast_mul(__int128 a,__int128 b,__int128 p)
{
	__int128 mul=a,res=0;
	while (b)
	{
		if (b&1) res=(res+mul)%p;
		mul=(mul+mul)%p,b>>=1;
	}
	return res;
}
__int128 fast_pow(__int128 a,__int128 b,__int128 p)
{
	__int128 mul=a,res=1;
	while (b)
	{
		if (b&1) res=fast_mul(res,mul,p);
		mul=fast_mul(mul,mul,p),b>>=1;
	}
	return res;
}
__int128 gcd(__int128 x,__int128 y)
{
	if (y==0) return x;
	return gcd(y,x%y);
}
__int128 get_pow(__int128 a,int b,__int128 c)
{
	__int128 mul=1;
	bool op=0;
	for (int i=1;i<=b;++i)
	{
		if (mul<=c/a) mul*=a;
		else op=1;
	}
	return op?c+1:mul;
}
__int128 F(__int128 x,__int128 d)
{
	__int128 y=0;
	for (int i=log(INF)/log(2);i>=0;--i)
		if (y+(1ll<<i)<=INF&&get_pow(y+(1ll<<i),d,x)<=x)
			y+=(1ll<<i);
	return y;
}
bool isprime(__int128 x)
{
	if (x<=N) return (!nprime[x]);
	if (x%2==0||x%3==0||x%5==0||x%7==0||x%11==0) return 0;
	__int128 a,d=x-1;
	int res=0;
	while (!(d&1)) d>>=1,res++;
	for (int i=1;i<=10;++i)
	{
		a=RAND()%maxn+1,pw[0]=fast_pow(a,d,x);
		for (int j=1;j<=res;++j) pw[j]=fast_mul(pw[j-1],pw[j-1],x);
		if (pw[res]!=1) return 0;
		for (int j=res-1;j>=0;--j)
		{
			if (pw[j]==x-1) break;
			if (pw[j]!=1) return 0;
		}
	}
	return 1;
}
void solve(__int128 x)
{
	if (x<=N)
	{
		while (x!=1) p[v[x]]++,x/=v[x];
		return;
	}
	__int128 a,d;
	for (int i=1;st[i]<=10000;++i)
		if (x%st[i]==0)
		{
			p[st[i]]++,solve(x/st[i]);
			return;
		}
	for (int i=1;i<=10;++i)
	{
	    d=F(x,i);
		if (isprime(d)&&get_pow(d,i,x)==x)
		{
			while (x%d==0) x/=d,p[d]++;
			return;
		}
	}
	for (int i=1;i<=10;++i)
	{
		a=RAND()%maxn+1;
		while (gcd(a,m)!=1) a=RAND()%maxn+1;
		if (fast_pow(a,m,x)!=1)
		{
			d=gcd(x,(fast_pow(a,m,x)-1)%x),solve(d);
			return;
		}
		else
		{
			pw[0]=fast_pow(a,sm,x);
			for (int i=1;i<=cnt;++i) pw[i]=fast_mul(pw[i-1],pw[i-1],x);
			for (int i=cnt-1;i>=0;--i)
			{
				if (pw[i]==-1) break;
				if (pw[i]!=1)
				{
					d=gcd(pw[i]+1,x),solve(d),solve(x/d);
					return;
				}
			}
		}
	}
	return;
}
int main()
{
	__int128 x;
	for (int i=2;i<=N;++i)
		if (!nprime[i])
		{
			st[++leng]=v[i]=i;
			for (int j=(i<<1);j<=N;j+=i) v[j]=i,nprime[j]=1;
		}
	T=read();
	while (T--)
	{
		sm=m=read()<<1,length=cnt=0,n=1,p.clear();
		while (!(sm&1)) sm>>=1,cnt++;
		solve(m);
		for (auto it:p) tong[++length]=(node){it.first,it.second};
		for (int i=length;i>=1;--i)
			if (tong[i].data)
			{
				for (int j=1;j<=((tong[i].data+1)>>1);++j) n*=tong[i].num;
				x=tong[i].num-1;
				for (int j=1;j<=i-1;++j)
					while (x%tong[j].num==0)
						x/=tong[j].num,tong[j].data--;
			}
		write(n),puts("");
	}
	return 0;
}

详细

Test #1:

score: 100
Accepted
time: 13ms
memory: 11388kb

input:

3
20
21
475750381222420656586462245096576000

output:

10
7
1497700821900508526

result:

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

Test #2:

score: 0
Accepted
time: 90ms
memory: 12220kb

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: 74ms
memory: 12224kb

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: 68ms
memory: 12448kb

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: 102ms
memory: 10488kb

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: 67ms
memory: 11060kb

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