UVA 11426 GCD - Extreme (II)

 

G=0;
for(i=1;i<N;i++)
for(j=i+1;j<=N;j++)
{
  G+=gcd(i,j);
}
 
SampleInput
10
100
200000
0
SampleOutput
67
13015
143295493160
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
using namespace std;
typedef long long LL;
const LL N = 4000000 + 10;
const LL MOD = 1000;
LL s[N], f[N], phi[N];
void funp()
{
    LL i, j;
    for(i = 2; i < N; i++)
        phi[i] = 0;
    phi[1] = 1;
    for(i = 2; i < N; i++)
        if(!phi[i]) {
            for(j = i; j < N; j += i) {
                if(!phi[j])
                    phi[j] = j;
                phi[j] = phi[j] / i * (i - 1);
            }
        }
}
LL init()
{
    LL i, j;
    funp();
    for(i = 1; i < N; i++) {
        for(j = i + i; j < N; j += i)
            f[j] += i * phi[j / i];
    }
    s[2] = f[2];
    for(i = 3; i < N; i++)
        s[i] = s[i - 1] + f[i];
}
int main()
{
    LL n;
    init();
    while(scanf("%lld", &n), n) {
        printf("%lld\n", s[n]);
    }
    return 0;
}

 

posted @ 2017-04-24 20:53  byonlym  阅读(133)  评论(0编辑  收藏  举报