QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#749209#9748. 最大公因数的平方和liaoz123TL 110ms57112kbC++141.5kb2024-11-14 23:03:292024-11-14 23:03:30

Judging History

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

  • [2024-11-14 23:03:30]
  • 评测
  • 测评结果:TL
  • 用时:110ms
  • 内存:57112kb
  • [2024-11-14 23:03:29]
  • 提交

answer

#include<bits/stdc++.h>
#define uint unsigned int
using namespace std;
typedef long long ll;
const int N=1e5+5;
int n,B,a[N],b[N];
uint pa[N][333],pb[N][333];
int posa[N],posb[N];
uint f[N],mu[N];
bool vis[N];
int p[N],cnt,m;
unordered_map<int,uint> t[N];
void pre(){
	mu[1]=1;
	for(int i=2;i<N;i++){
		if(!vis[i])p[++cnt]=i,mu[i]=-1;
		for(int j=1;j<=cnt&&p[j]*i<N;j++){
			vis[p[j]*i]=true;
			if(i%p[j]==0)break;
			mu[p[j]*i]=-mu[i];
		} 
	}
}
void insert(int x,int y,uint z){
	for(int i=x;i<=n;i+=(i&(-i)))
	for(int j=y;j<=n;j+=(j&(-j)))
	t[i][j]+=z;
}
uint sum(int x,int y){
	uint ss=0;
	for(int i=x;i;i-=(i&(-i)))
	for(int j=y;j;j-=(j&(-j)))
	ss+=t[i][j];return ss;
}
uint solve(int l,int r){
	if(!l||!r)return 0;
	uint ss=0;
	for(int i=1;i<=B;i++)
	ss+=pa[l][i]*pb[r][i]*f[i];
	ss+=sum(l,r);
	return ss;
}
int main(){
	scanf("%d",&n);B=pow(n,0.6667);pre();
	for(int i=1;i<=n;i++)scanf("%d",&a[i]),posa[a[i]]=i;
	for(int i=1;i<=n;i++)scanf("%d",&b[i]),posb[b[i]]=i;
	for(int i=1;i<=B;i++)
	for(int j=1;j<=n;j++){
		pa[j][i]=pa[j-1][i]+(a[j]%i==0);
		pb[j][i]=pb[j-1][i]+(b[j]%i==0);
	} 
	for(uint i=1;i<=n;i++)
	for(uint j=i;j<=n;j+=i)
	f[j]+=i*i*mu[j/i];
	for(int i=B+1;i<=n;i++)
	for(int j=i;j<=n;j+=i)
	for(int z=i;z<=n;z+=i)insert(posa[j],posb[z],f[i]);
	scanf("%u",&m);
	while(m--){
		uint l,r,L,R;
		scanf("%u%u%u%u",&l,&r,&L,&R);
		printf("%u\n",(solve(r,R)-solve(l-1,R)-solve(r,L-1)+solve(l-1,L-1)+(1ll<<32)));
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 14160kb

input:

5
4 1 5 3 2
1 2 3 4 5
5
3 3 2 3
3 4 2 4
3 4 3 4
5 5 1 1
1 1 2 2

output:

2
14
12
1
4

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 7ms
memory: 19176kb

input:

1000
564 82 818 337 917 532 941 590 783 74 256 176 714 159 262 803 869 716 12 448 523 981 664 533 797 520 872 966 710 103 118 101 288 113 363 356 87 475 515 641 147 374 424 788 883 496 230 157 926 942 999 220 886 509 987 99 400 458 993 343 946 684 205 4 925 727 875 939 830 95 486 870 741 846 916 321...

output:

21490984
1224951
277460
4677079
45620670
68717204
1487646
213162875
1696541
874252
822545
69652827
56058810
47821969
3261458
10540347
15664913
80605699
139540643
76151128
52126736
5245980
10101625
152049237
55449324
11352001
47057255
3509727
62148584
29861100
106478852
16350268
31181559
8679031
1564...

result:

ok 100 lines

Test #3:

score: 0
Accepted
time: 4ms
memory: 17676kb

input:

1000
840 720 960 900 936 660 756 924 360 780 864 480 540 504 420 630 600 990 672 792 576 336 810 528 912 240 816 648 880 432 560 624 828 288 588 300 700 252 612 768 800 882 684 468 980 450 180 396 972 552 702 510 168 546 888 594 896 930 312 870 1000 680 280 210 270 216 858 570 910 264 696 920 520 69...

output:

6552686
533067
169126
49
94392
15874
646003
9382
45717
1
456976
1
1
91544660
261221
4
1
73636
136002694
11087
467
697765
878
35
1
170897
96
221941
8716057
702144
623544
108101127
453642
289514
953537
14504512
11427616
146345360
16
688131
1
809601
343338
4
1
664608
1
2864172
1116528
1
304730035
47964...

result:

ok 100 lines

Test #4:

score: 0
Accepted
time: 110ms
memory: 57112kb

input:

5000
812 2241 3168 254 2304 2907 711 1116 2083 3445 755 4289 1848 2265 1481 4458 1701 4233 3301 4425 3793 1937 4891 4422 3891 556 2867 4041 1020 4089 3388 1013 729 896 870 365 1621 3506 2575 2714 4405 2449 326 3935 3480 4801 166 4269 1256 1587 4240 1503 3768 868 3997 1730 1077 2061 2353 2224 4528 27...

output:

807941321
455240624
2627537620
2424135152
2589015411
3591134314
2113263772
1298284545
3406697170
2076026315
4213807974
968114259
3793172020
1976412878
2776780447
1338391782
2380849218
3625502333
1067059142
414621999
107082133
600207777
621583381
3876041158
133853393
717061929
1404224227
527861594
68...

result:

ok 100 lines

Test #5:

score: 0
Accepted
time: 78ms
memory: 52672kb

input:

5000
4620 3360 4680 3960 4200 3780 2520 4320 3600 4800 2880 4032 3696 1680 2640 4368 2160 3120 4080 4560 3024 4752 4536 3240 4950 3168 1260 4140 2100 2940 3300 3900 3150 3276 3420 2772 1440 2400 2340 2700 4284 3840 4896 4500 4860 4788 3060 3744 3528 4410 1980 4704 2016 1800 3640 2808 4830 2184 2730 ...

output:

69632396
1247
901133646
4046059109
200952
1396802106
540629598
755337245
557588
2435483
9457953
1145548972
12
32223496
287792028
988209
4486883
6786453
2246716847
1
1957940484
112
8734547
4
980245
790140126
82212229
508
2621537
1897066516
321180652
14246
497053918
212
10
24674630
10480880
2323949
27...

result:

ok 1000 lines

Test #6:

score: -100
Time Limit Exceeded

input:

50000
45360 27720 40320 42840 47520 41580 37800 32760 43680 47880 46200 36960 49140 30240 39600 25200 35280 46800 47040 37440 44352 43200 20160 33600 31680 48960 44100 34320 38640 36720 42000 23760 42120 49896 48048 48720 28560 15120 21840 33264 39312 49680 41040 31920 18480 28080 35640 22680 44880 ...

output:


result: