hdu 2582(数论相关定理+素数筛选+整数分解)
2016-05-26 10:33
453 查看
f(n)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 457 Accepted Submission(s): 279
[align=left]Problem Description[/align]
This time I need you to calculate the f(n) . (3<=n<=1000000)
f(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n).
Gcd(n)=gcd(C
[1],C
[2],……,C
[n-1])
C
[k] means the number of way to choose k things from n some things.
gcd(a,b) means the greatest common divisor of a and b.
[align=left]Input[/align]
There are several test case. For each test case:One integer n(3<=n<=1000000). The end of the in put file is EOF.
[align=left]Output[/align]
For each test case:
The output consists of one line with one integer f(n).
[align=left]Sample Input[/align]
3
26983
[align=left]Sample Output[/align]
3 37556486
这题要用到这个公式:
设
,那么G的值为:
n为素数:本身
n有多个素因子:1
n只有一个素因子:该因子
920MS才跑过去。。没优化了。。记得用_int64保存结果。
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream> using namespace std; typedef long long LL; const int N = 1000001; bool p ; LL g ; LL f ; void init(){ memset(g,0,sizeof(g)); for(int i=2;i<N;i++){ if(!p[i]){ g[i]=(LL)i; for(LL j = (LL)i*i;j<N;j+=i){ p[j] = true; } } } f[2]=0; for(int i=3;i<N;i++){ int cnt = 0; if(g[i]==0){ int temp=-1; int n = i; for(int j=2;j*j<=n;j++){ if(n%j==0){ temp=(LL)j; cnt++; while(n%j==0){ n/=j; } } } if(n>1) {temp=(LL)n,cnt++;} if(cnt!=1) g[i]=1; if(cnt==1) g[i]=(LL)temp; } f[i]=f[i-1]+g[i]; } } int main() { init(); int n; while(~scanf("%d",&n)){ printf("%lld\n",f ); } return 0; }
相关文章推荐
- 为简单而努力:Android封装类详解
- css:使用笔记(不断更新中...)
- Linux scp 远程复制命令详解
- 面向对象基础
- iOS界面间传值的几种方式
- Linux下DNS服务器部署
- thinkphp的field方法的用法总结,或许还有你不知道的
- mysql db 下载
- codeforces 323A. Black-and-White Cube 构造
- CUDA7.5安装和vs2013配置
- 检测到ContextSwitchDeadlock解决方法
- 关于Spring的xsd文件版本号的问题
- Spring MVC Ajax请求加载更多
- DOS部分命令
- PHP+sqlite数据库操作示例(创建/打开/插入/检索)
- 179. Largest Number
- Codeforces Round #354 (Div. 2)(C)尺取
- 数据结构与算法的分析 —— 渐进复杂度(三个记号)
- Queue LifoQueue and PriorityQueue
- 解决方法:android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留几个类)