QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#372155#3004. It's a Mod, Mod, Mod, Mod WorldInfinityNSAC ✓59ms3924kbC++14829b2024-03-31 00:05:102024-03-31 00:05:11

Judging History

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

  • [2024-03-31 00:05:11]
  • 评测
  • 测评结果:AC
  • 用时:59ms
  • 内存:3924kb
  • [2024-03-31 00:05:10]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll long long

ll Solve(int p,int q,int n){
	if(p>=q){
		return Solve(p%q,q,n);
	}
	if(p==0)return 0;
	if(q%p==0){
		int t=q/p;
		if(n>t){
			return Solve(p,q,t)*(n/t)+Solve(p,q,n%t);
		}
		int blk=q/p;
		int blks=n/blk;
		ll ans=(ll)n*(n-1)/2*p;
		ans-=(ll)blks*(blks-1)/2*q*blk;
		ans-=(ll)blks*(n-blks*blk)*q;
		return ans;
	}
	int n1=(ll)p*(n-1)/q+1;
	ll tmp=Solve(q,p,n1);
	ll sum=(ll)n1*(n1-1)/2*q;
	sum-=tmp;
	ll need=p/__gcd(p,q);
	int dv=(n1+need-1)/need;
	sum+=(ll)p*(n1-dv);
	sum/=p;
	sum=(ll)n*(n1-1)-sum;
	ll ans=(ll)n*(n-1)/2*p;
	ans-=sum*q;
	return ans;
}

int main(){
	int t;
	scanf("%i",&t);
	while(t--){
		int p,q,n;
		scanf("%i %i %i",&p,&q,&n);
		ll ans=Solve(p,q,n+1);
		printf("%lld\n",ans);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 28ms
memory: 3744kb

input:

91125
999956 999956 999956
999956 999956 999957
999956 999956 999958
999956 999956 999959
999956 999956 999960
999956 999956 999961
999956 999956 999962
999956 999956 999963
999956 999956 999964
999956 999956 999965
999956 999956 999966
999956 999956 999967
999956 999956 999968
999956 999956 999969
...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
499956500946
499956500946
499957500902
499958500857
499959500811
499960500764
499961500716
499962500667
499963500617
499964500566
499965500514
499966500461
499967500407
499968500352
499969500296
499970500239
49...

result:

ok 91125 lines

Test #2:

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

input:

8000
1 1 1
1 1 2
1 1 3
1 1 4
1 1 5
1 1 6
1 1 7
1 1 8
1 1 9
1 1 10
1 1 999991
1 1 999992
1 1 999993
1 1 999994
1 1 999995
1 1 999996
1 1 999997
1 1 999998
1 1 999999
1 1 1000000
1 2 1
1 2 2
1 2 3
1 2 4
1 2 5
1 2 6
1 2 7
1 2 8
1 2 9
1 2 10
1 2 999991
1 2 999992
1 2 999993
1 2 999994
1 2 999995
1 2 999...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
2
2
3
3
4
4
5
5
499996
499996
499997
499997
499998
499998
499999
499999
500000
500000
1
3
3
4
6
6
7
9
9
10
999991
999993
999993
999994
999996
999996
999997
999999
999999
1000000
1
3
6
6
7
9
12
12
13
15
1499988
1499988
1499989
1499991
1499994
1499994
149999...

result:

ok 8000 lines

Test #3:

score: 0
Accepted
time: 44ms
memory: 3792kb

input:

100000
848401 999985 1000000
999527 999616 1000000
999789 999914 1000000
999479 999722 1000000
999841 999933 1000000
406226 999991 1000000
940598 999982 1000000
999708 999994 1000000
948123 999993 1000000
999789 999851 1000000
999522 999893 1000000
999977 999983 1000000
999912 999924 1000000
999232 ...

output:

499992309650
499992847584
499999028720
499990037714
499999288213
499994780341
499990845156
499998993982
499997047626
499998796124
499997350691
499999498946
499993962456
499999037615
499998396930
499994213619
499996210819
499998734816
499998193499
499997294296
499997311600
499997563064
499998257020
4...

result:

ok 100000 lines

Test #4:

score: 0
Accepted
time: 58ms
memory: 3880kb

input:

100000
276544 661397 84822
450280 333101 69413
273309 622139 246693
731970 90981 550001
606352 518276 101164
812691 168203 790853
98599 798097 886901
292688 792934 987579
447910 689959 311317
41624 797440 706737
921438 988902 506461
949886 363820 536239
214632 593976 642315
657797 687112 1001
481835...

output:

28050739501
11561046740
76748732242
25017313230
26215260156
66512110595
353916345605
391541916956
107399223391
281787020632
250419547980
97547118940
190750940808
345563873
290206495717
91866855387
107030416770
31409284551
81134882478
50587710613
28824979443
12077656210
126886307743
216791739528
1864...

result:

ok 100000 lines

Test #5:

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

input:

100000
803592 687221 934373
999545 991069 529082
875185 968212 727643
950635 957530 602207
885034 922979 801761
880161 961085 898509
696622 832975 941815
733134 969545 885444
971899 876472 950412
830028 716275 976560
899356 883946 787926
864158 917358 972542
863455 963893 941024
977884 822913 821837...

output:

321059666273
262172980269
352256802002
288321229980
370007657538
431772636765
392254148690
429236971285
416502688210
349742010515
348243992180
446068974702
453523417767
338149672589
355430775685
401615793680
270086747209
360313589303
370869897573
288206709315
470549116905
252046988544
427852655100
3...

result:

ok 100000 lines

Test #6:

score: 0
Accepted
time: 16ms
memory: 3784kb

input:

91125
1 1 1
1 1 2
1 1 3
1 1 4
1 1 5
1 1 6
1 1 7
1 1 8
1 1 9
1 1 10
1 1 11
1 1 12
1 1 13
1 1 14
1 1 15
1 1 16
1 1 17
1 1 18
1 1 19
1 1 20
1 1 21
1 1 22
1 1 23
1 1 24
1 1 25
1 1 26
1 1 27
1 1 28
1 1 29
1 1 30
1 1 31
1 1 32
1 1 33
1 1 34
1 1 35
1 1 36
1 1 37
1 1 38
1 1 39
1 1 40
1 1 41
1 1 42
1 1 43
1 ...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14
15
15
16
16
17
17
18
18
19
19
20
20
21
21
22
22
23
1
3
3
4
6
6
7
9
9
10
12
12
13
15
15
16
18
18
19
21
21
22
24
24
25
27
27
28
30
30
31
33
33
34
...

result:

ok 91125 lines