## bzoj 2301 莫比乌斯反演

/**************************************************************
Problem: 2301
Language: Pascal
Result: Accepted
Time:34964 ms
Memory:1204 kb
****************************************************************/

var
a, b, c, d, k, t                    :longint;
ans                                 :int64;
i                                   :longint;
prime, miu, mindiv                  :array[0..50010] of longint;
sum                                 :array[0..50010] of int64;

procedure make;
var
i, j                                :longint;
begin
miu[1]:=1;
for i:=2 to 50000 do
begin
if mindiv[i]=0 then
begin
inc(prime[0]);
prime[prime[0]]:=i;
mindiv[i]:=i;
miu[i]:=-1;
end;
for j:=1 to prime[0] do
begin
if i*prime[j]>50000 then break;
mindiv[i*prime[j]]:=prime[j];
if i mod prime[j]=0 then
begin
miu[i*prime[j]]:=0;
break;
end else
miu[i*prime[j]]:=-miu[i];
end;
end;
for i:=1 to 50000 do sum[i]:=sum[i-1]+miu[i];
end;

function calc(n,m:longint):longint;
var
t, t1, t2                           :int64;
i                                   :longint;
xx                                  :int64;
begin
calc:=0;
i:=1;
if n>m then xx:=m else xx:=n;
while i<=xx do
begin
t1:=n div (n div i);
t2:=m div (m div i);
if t1<t2 then t:=t1 else t:=t2;
calc:=calc+(sum[t]-sum[i-1])*(n div i)*(m div i);
i:=t+1;
end;
end;

begin
make;
for i:=1 to t do
begin
ans:=int64(calc(b div k,d div k))
-int64(calc((c-1) div k,b div k))
-int64(calc((a-1) div k,d div k))
+int64(calc((a-1) div k,(c-1) div k));
writeln(ans);
end;
end.

posted on 2013-12-23 18:46  BLADEVIL  阅读(...)  评论(...编辑  收藏

• 随笔 - 244
• 文章 - 0
• 评论 - 12