【BZOJ2820】YY的GCD(莫比乌斯反演)
题意:给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对,多组数据
T = 10000
N, M <= 10000000
思路:
1 const max=10000000; 2 var sum:array[0..max]of int64; 3 prime,flag,f,mu:array[0..max]of longint; 4 n,m,i,j,t,v,cas:longint; 5 6 function clac(n,m:longint):int64; 7 var t,x,y,t1,t2,i,pos:longint; 8 begin 9 if n>m then 10 begin 11 t:=n; n:=m; m:=t; 12 end; 13 clac:=0; i:=1; 14 while i<=n do 15 begin 16 x:=n div i; y:=m div i; 17 t1:=n div x; t2:=m div y; 18 if t1<t2 then pos:=t1 19 else pos:=t2; 20 clac:=clac+(sum[pos]-sum[i-1])*x*y; 21 i:=pos+1; 22 end; 23 end; 24 25 begin 26 assign(input,'bzoj2820.in'); reset(input); 27 assign(output,'bzoj2820.out'); rewrite(output); 28 mu[1]:=1; 29 for i:=2 to max do 30 begin 31 if flag[i]=0 then 32 begin 33 inc(m); prime[m]:=i; 34 mu[i]:=-1; 35 end; 36 j:=1; 37 while (j<=m)and(prime[j]*i<=max) do 38 begin 39 t:=prime[j]*i; flag[t]:=1; 40 if i mod prime[j]=0 then 41 begin 42 mu[t]:=0; break; 43 end; 44 mu[t]:=-mu[i]; 45 inc(j); 46 end; 47 end; 48 for i:=1 to m do 49 for j:=1 to max div prime[i] do 50 begin 51 t:=prime[i]*j; 52 f[t]:=f[t]+mu[j]; 53 end; 54 for i:=1 to max do sum[i]:=sum[i-1]+f[i]; 55 read(cas); 56 for v:=1 to cas do 57 begin 58 read(n,m); 59 writeln(clac(n,m)); 60 end; 61 close(input); 62 close(output); 63 end.
null