您的位置:首页 > 其它

HDU 2824 The Euler function(筛法求欧拉phi函数)

2015-10-06 08:36 453 查看
该题就是一个筛法求欧拉phi函数值,我预处理了前缀和,没想到比直接求还慢。。。

细节参见代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
#include<cmath>
using namespace std;
typedef long long ll;
const int INF = 1000000000;
const int maxn = 3000000 + 5;
int a,b,T;
ll phi[maxn];
void init() {
int i,j;
memset(phi, 0, sizeof(phi));
phi[1] = 1;
for(int i = 2; i < maxn-3; i++) if(!phi[i]) {
for(j = i; j < maxn-3; j+=i) {
if(!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i-1);
}
}
phi[0] = 0;
for(int i=1;i<=maxn-3;i++)
phi[i] = phi[i-1] + phi[i];
}
int main() {
init();
while(~scanf("%d%d",&a,&b)) {
printf("%I64d\n",phi[b] - phi[a-1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm-icpc 数论 phi 函数