您的位置:首页 > 其它

LightOj1007 - Mathematically Hard(欧拉函数)

2016-10-24 19:14 323 查看
题目链接:http://lightoj.com/volume_showproblem.php?problem=1007

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

线性打表求值即可;

结果会爆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
= {0};
LL ans
= {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\n", t++, ans[b]-ans[a-1]);
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: