# 【bzoj2401】陶陶的难题I “高精度”+欧拉函数+线性筛

7
1
10
100
1000
10000
100000
1000000

1
2127
18446224
183011304660
1827127167830060
18269345553999897648
182690854273058293758232

“高精度”+欧拉函数+线性筛

$\ \ \ \ \sum\limits_{i=1}^n\sum\limits_{j=1}^ilcm(i,j)\\=\sum\limits_{i=1}^n\sum\limits_{d|i}\sum\limits_{j=1}^i[gcd(i,j)=d]\frac{ij}d\\=\sum\limits_{i=1}^n\sum\limits_{d|i}\sum\limits_{j=1}^{\frac id}[gcd(\frac id,j)=1]ij\\=\sum\limits_{i=1}^ni\sum\limits_{d|i}\sum\limits_{j=1}^{\frac id}[gcd(\frac id,j)=1]j\\=\sum\limits_{i=1}^ni\sum\limits_{d|i}\sum\limits_{j=1}^{d}[gcd(d,j)=1]j$

$\ \ \ \ \sum\limits_{i=1}^ni\sum\limits_{d|i}\sum\limits_{j=1}^{d}[gcd(d,j)=1]j\\=\frac{\sum\limits_{i=1}^ni(\sum\limits_{d|i}d\varphi(d)+1)}2$

$2·\frac{\sum\limits_{i=1}^ni(\sum\limits_{d|i}d\varphi(d)+1)}2-\sum\limits_{i=1}^ni=\sum\limits_{i=1}^ni\sum\limits_{d|i}d\varphi(d)$

（其中很多乱七八糟的东西都消掉了，剩下的式子非常优美）

$f(p^a)=\frac{p^{2a+1}+1}{p+1}$

#include <cstdio>
#define N 1000010
#define k 1000000
typedef long long ll;
ll f[N] , sx[N] , sy[N];
int w[N] , v[N] , prime[N] , tot , np[N];
int main()
{
int i , j , t , T , n;
f[1] = 1;
for(i = 2 ; i <= k ; i ++ )
{
if(!np[i]) w[i] = v[i] = i , f[i] = (ll)i * i - i + 1 , prime[++tot] = i;
for(j = 1 ; j <= tot && (t = i * prime[j]) <= k ; j ++ )
{
np[t] = 1;
if(i % prime[j] == 0)
{
w[t] = w[i] , v[t] = v[i] * w[i] , f[t] = ((ll)v[t] * v[t] * w[t] + 1) / (w[t] + 1) * f[t / v[t]];
break;
}
else w[t] = v[t] = prime[j] , f[t] = f[i] * f[prime[j]];
}
}
for(i = 1 ; i <= k ; i ++ ) sx[i] = sx[i - 1] + i * f[i] , sy[i] = sy[i - 1] + sx[i] / 1000000000000ll , sx[i] %= 1000000000000ll;
scanf("%d" , &T);
while(T -- )
{
scanf("%d" , &n);
if(sy[n]) printf("%lld%012lld\n" , sy[n] , sx[n]);
else printf("%lld\n" , sx[n]);
}
return 0;
}


posted @ 2017-09-13 20:05  GXZlegend  阅读(337)  评论(0编辑  收藏  举报