## bzoj 2005 NOI 2010 能量采集

Σi<=n Σj<=m Σ d|gcd(i,j) phi(d)

/**************************************************************
Problem: 2005
Language: Pascal
Result: Accepted
Time:36 ms
Memory:2568 kb
****************************************************************/

var
i, j                                :longint;
prime, mindiv, phi                  :array[0..100010] of int64;
ans                                 :int64;
n, m                                :int64;

procedure swap(var a,b:int64);
var
c                                   :int64;
begin
c:=a; a:=b; b:=c;
end;

begin
if n>m then swap(n,m);
phi[1]:=1;
for i:=2 to n do
begin
if mindiv[i]=0 then
begin
inc(prime[0]);
prime[prime[0]]:=i;
mindiv[i]:=i;
phi[i]:=i-1;
end;
for j:=1 to prime[0] do
begin
if i*prime[j]>m then break;
mindiv[i*prime[j]]:=prime[j];
if i mod prime[j]=0 then
begin
phi[i*prime[j]]:=phi[i]*prime[j];
break;
end else
phi[i*prime[j]]:=phi[i]*(prime[j]-1);
end;
end;
for i:=1 to n do
ans:=ans+(n div i)*(m div i)*phi[i];
ans:=ans*2-n*m;
writeln(ans);
end.

posted on 2013-12-23 16:44  BLADEVIL  阅读(341)  评论(0编辑  收藏  举报