LightOj1007

题目链接:http://lightoj.com/volume_showproblem.php?problem=1007

题意:给你两个数a和b,求他们之间所有数的欧拉值得平方和; a and b (2 ≤ a ≤ b ≤ 5 * 106).

线性打表求值即可;

结果会爆long long,要用unsigned long long %llu形式;

#include <stdio.h>
#include <string.h>
#include <algorithm>
typedef unsigned long long LL;
#define N 5000001
using namespace std;

int f[N] = {0};
LL ans[N] = {0};

void Init()
{
    for(int i=2; i<N; i++)
    {
        if(f[i]) continue;
        for(int j=i; j<N; j+=i)
        {
            if(!f[j]) f[j] = j;
            f[j] = f[j]/i*(i-1);
        }
    }
    ans[0] = 0;
    for(int i=1; i<N; i++)
        ans[i] = ans[i-1] + (LL)f[i]*f[i];
}

int main()
{
    Init();

    int T, a, b, t = 1;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &a, &b);
        printf("Case %d: %llu
", t++, ans[b]-ans[a-1]);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5994207.html