【NJU749D】triple(莫比乌斯反演)

题意:

cas<=100 n<=10^5

思路:与两个数的没什么区别

F(d)=(n div d)*(n div d-1)*(n div d-2) div 6

再加上喜闻乐见的下底函数分块

又是一个不能测P的萎靡题库……

 1 const max=1000000;
 2 var mu,flag,prime:array[0..max]of longint;
 3     sum:array[0..max]of int64;
 4     n,m,v,pos,i,j,t,cas:longint;
 5     ans,x:int64;
 6 begin
 7  assign(input,'triple.in'); reset(input);
 8  assign(output,'triple.out'); rewrite(output);
 9  mu[1]:=1;
10  for i:=2 to max do
11  begin
12   if flag[i]=0 then
13   begin
14    inc(m); prime[m]:=i;
15    mu[i]:=-1;
16   end;
17   j:=1;
18   while (j<=m)and(prime[j]*i<=max) do
19   begin
20    t:=prime[j]*i; flag[t]:=1;
21    if i mod prime[j]=0 then
22    begin
23     mu[t]:=0;
24     break;
25    end;
26    mu[t]:=-mu[i];
27    inc(j);
28   end;
29  end;
30  for i:=1 to max do sum[i]:=sum[i-1]+mu[i];
31  readln(cas);
32  for v:=1 to cas do
33  begin
34   readln(n,m);
35   ans:=0; i:=1;
36   n:=n div m;
37   while i<=n do
38   begin
39    x:=n div i; pos:=n div x;
40    ans:=ans+x*(x-1)*(x-2) div 6*(sum[pos]-sum[i-1]);
41    i:=pos+1;
42   end;
43   writeln(ans);
44  end;
45  close(input);
46  close(output);
47 end.

 

posted on 2017-04-18 16:00  myx12345  阅读(167)  评论(0编辑  收藏  举报

导航