HDU 2582 规律题
来源:互联网 发布:网络逃犯坐火车 编辑:程序博客网 时间:2024/04/25 16:57
f(n)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 165 Accepted Submission(s): 92
Problem Description
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[n][1],C[n][2],……,C[n][n-1])
C[n][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.
f(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n).
Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n-1])
C[n][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.
Input
There are several test case. For each test case:One integer n(3<=n<=1000000). The end of the in put file is EOF.
Output
For each test case:
The output consists of one line with one integer f(n).
The output consists of one line with one integer f(n).
Sample Input
326983
Sample Output
337556486
Source
ECJTU 2009 Spring Contest
Recommend
lcy
直接推,真的很难推出什么名堂。。
但是通过打表找GCD的规律的话,你会发现如下规律:
GCD(n),当n只有一个质因子的时候GCD(n)不会为1。
此时GCD等于n的这个唯一的质因子。
所以预处理的时候,只需要判断i是不是可以写成p^k就行了
顺便说一句,必须要用__int64才能存的下。
我的代码:
#include<stdio.h>__int64 prime[100000];bool flag[1000001];__int64 sum[1000001];void init(){__int64 i,j,num=0;for(i=2;i<=1000000;i++){if(!flag[i]){prime[num++]=i;for(j=i*i;j<=1000000;j=j+i)flag[j]=true;}}}__int64 solve(__int64 n){__int64 i,k,ret=0;for(i=0;prime[i]*prime[i]<=n;i++){if(n%prime[i]==0){n=n/prime[i];while(n%prime[i]==0)n=n/prime[i];k=prime[i];ret=ret+1;}if(ret>=2)return 1;}if(n>1){ret=ret+1;k=n;}if(ret>=2)return 1;elsereturn k;}int main(){__int64 n,i;init();for(i=3;i<=1000000;i++){if(flag[i])sum[i]=sum[i-1]+solve(i);elsesum[i]=sum[i-1]+i;}while(scanf("%d",&n)!=EOF)printf("%I64d\n",sum[n]);return 0;}
- HDU 2582 规律题
- hdu 1792规律题
- hdu 4925规律题
- hdu 5524 规律题
- hdu 4148 规律题
- HDU 6043 规律题
- HDU 1046 数学规律题
- HDU 4602 Partition 规律题
- HDU 4279 Number 规律题
- hdu 1046 Gridland(规律题)
- hdu 1021 (找规律题)
- HDU 1719 Friend 【规律题】
- hdu 2041 简单规律题
- HDU 5835 Danganronpa ---规律题
- 规律:HDU
- hdu 1214 圆桌会议 规律题 小水
- hdu 1331 找规律 超级水题
- HDU 4940 Destroy Transportation system 规律题
- ExtJS 日期格式转换的三种方式
- linux用户文件解析
- (23)重定向和请求派
- 软件产品|营销网站的设计思路(一)
- Java远程访问Domino数据库
- HDU 2582 规律题
- 用JAVA访问DOMINO邮箱数据
- 理解JAVA中的单例模式 --->花1分钟看看
- 利用 LotusScript 灵活操作 Lotus Notes 富文本域
- UML模型映射
- drupal7 services
- Domino中运用ajax判断帐号是否存在的简单例子
- C++下this指针实现机制分析(针对vc++ 6.0)
- 关于解决zencart乱码的措施