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;
}
细节参见代码:
#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;
}
相关文章推荐
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数
- Lua常用时间函数使用实例
- Lua函数与字符串处理简明总结
- Lua学习笔记之表和函数
- Lua中实现sleep函数功能的4种方法
- Lua函数用法研究
- Lua基础教程之赋值语句、表达式、流程控制、函数学习笔记
- PowerShell函数简明教程
- 探索PowerShell(十一)函数介绍