QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#105112#983. The Hash TablechenshiAC ✓124ms1880kbC++1.3kb2023-05-13 00:29:202023-05-13 00:29:24

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-13 00:29:24]
  • 评测
  • 测评结果:AC
  • 用时:124ms
  • 内存:1880kb
  • [2023-05-13 00:29:20]
  • 提交

answer

#include<cstdio>
using namespace std;
int T,n,m,pri[9999],cnt,pw[9999],v[9999];long long ans;
long long calc(int n,int a,int b,int c){
	if(!a||!n) return b/c*1ll*n;
	if(a>=c||b>=c) return calc(n,a%c,b%c,c)+b/c*1ll*n+a/c*(n*(n-1ll)/2);
	int t=(a*1ll*n+b)/c;
	return n*1ll*t-calc(t,c,c+a-b-1,a);
}
inline long long Calc(int n,int a,int b,int c,int t){
	if(a<0) b+=(n-1)*a,a=-a;
	if(b<0){
		if(!a) return 0;
		int tmp=(-b+a-1)/a;
		n-=tmp;b+=a*tmp;
	}
	return (n>0)?calc(n,a,b,c)+t*n:0;
}
void dfs(int nw,int a,int b,int coef){
	if(nw>cnt){
		for(int i=0,t1,t2;i<2;++i) for(int j=0;j<2;++j) if((i*a+j*b)%2==0){
			t1=(n-1)/a-i;t2=(n-1)*2/a-i;t1>>=1;t2>>=1;
			if(t1>=0) ans+=coef*(Calc(t1+1,a,(i*a-j*b)/2,b,j)-Calc(t1+1,-a,n-1-(i*a+j*b)/2,b,j));
			if(t2>=0) ans+=coef*Calc(t2+1,-a,n-1-(i*a+j*b)/2,b,j);
		}
		return;
	}
	for(int i=0,t=1;i<=pw[nw];++i,t*=pri[nw]){
		dfs(nw+1,a*t,b*v[nw]/t,coef);
		if(i<pw[nw]) dfs(nw+1,a*t*pri[nw],b*v[nw]/t,-coef);
	}
}
int main(){
	for(scanf("%d",&T);T--;printf("%lld\n",ans),ans=cnt=0){
		scanf("%d%d",&n,&m);
		for(int i=2;i*i<=m;++i) if(m%i==0){
			pri[++cnt]=i;pw[cnt]=0;v[cnt]=1;
			for(;m%i==0;m/=i) ++pw[cnt],v[cnt]*=i;
		}
		if(m>1) pri[++cnt]=m,pw[cnt]=1,v[cnt]=m;
		dfs(1,1,1,1);
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 1880kb

input:

3
5 4
1234 5678
5 4

output:

4
229
4

result:

ok 3 tokens

Test #2:

score: 0
Accepted
time: 1ms
memory: 1520kb

input:

5
919191919 998244353
919191919 308308924
124312512 700980343
199712020 199712020
1000000000 1000000000

output:

420069742
18975162173
34523625
619107226
36400000000

result:

ok 5 tokens

Test #3:

score: 0
Accepted
time: 1ms
memory: 1536kb

input:

5
351177178 2
236814804 3
406487669 4
107706571 5
206252441 6

output:

30831352411422332
15578125268692158
41308056059019556
2088126907497711
5908342860201211

result:

ok 5 tokens

Test #4:

score: 0
Accepted
time: 92ms
memory: 1516kb

input:

5
795965436 698377680
775372176 735134400
891189723 555660000
255414898 555660000
649803503 967458816

output:

252131828509
252898591532
196092107465
16019483439
23110007395

result:

ok 5 tokens

Test #5:

score: 0
Accepted
time: 120ms
memory: 1732kb

input:

5
980056074 669278610
786804145 536870912
94743614 669278610
925596206 551350800
277753133 735134400

output:

151494104505
16332684795
1386109471
391665329539
32367779875

result:

ok 5 tokens

Test #6:

score: 0
Accepted
time: 124ms
memory: 1600kb

input:

5
407928831 901800900
959460685 551350800
929921323 901800900
880362378 551350800
169887581 551350800

output:

24176516170
420863755861
126194540248
354298688005
13128893811

result:

ok 5 tokens

Test #7:

score: 0
Accepted
time: 1ms
memory: 1608kb

input:

5
755923335 903960666
248398318 89883150
32020160 729647777
48000958 745177909
261977197 120541067

output:

5122927135
10499768724
1250410
5736978
980686184

result:

ok 5 tokens

Test #8:

score: 0
Accepted
time: 1ms
memory: 1552kb

input:

5
120618885 904615503
860299168 970357266
2922308 99586084
116144876 802899228
621873179 98196855

output:

72362680
1890408813
77476
180492837
23279216759

result:

ok 5 tokens

Test #9:

score: 0
Accepted
time: 2ms
memory: 1504kb

input:

5
337929091 905270340
472298323 850733078
973824456 50926103
184354331 860587779
981801929 75885412

output:

2695205304
791722210
18137207967
13641011
50173808550

result:

ok 5 tokens

Test #10:

score: 0
Accepted
time: 2ms
memory: 1744kb

input:

5
725900160 906132534
140660629 407214962
935092856 84357639
706890499 221743500
461717852 516104007

output:

3448528660
288899219
61675795925
96294825272
475220310

result:

ok 5 tokens

Test #11:

score: 0
Accepted
time: 0ms
memory: 1760kb

input:

5
943210366 906787371
752627016 287590774
905995005 183214075
775132721 279464819
821646602 493792564

output:

11587484479
1606204266
21788486368
3802176853
10347608659

result:

ok 5 tokens

Test #12:

score: 0
Accepted
time: 1ms
memory: 1548kb

input:

5
943210366 900720143
752627016 900720143
905995005 942060233
775132721 942060233
821646602 962178361

output:

1522977645
931104902
1306217667
941841841
691366758

result:

ok 5 tokens