hdoj f(n) 2582 (GCD打表&找规律)好题
来源:互联网 发布:sql nvl函数 编辑:程序博客网 时间:2024/06/05 18:47
f(n)
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 378 Accepted Submission(s): 230
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
通过打表找GCD的规律的话,你会发现如下规律:
GCD(n),当n只有一个质因子的时候GCD(n)不会为1,此时GCD等于n的这个唯一的质因子。
#include<stdio.h>#include<string.h>#include<algorithm>#define ll __int64#define N 1000010using namespace std;ll p[N];bool flag[N];ll sum[N];void init(){__int64 i,j,num=0;for(i=2;i<=N;i++){if(!flag[i]){p[num++]=i;for(j=i*i;j<=N;j+=i)flag[j]=true;}}}ll solve(ll n){ll i,k,ans=0;for(i=0;p[i]*p[i]<=n;i++){if(n%p[i]==0){n/=p[i];while(n%p[i]==0)n/=p[i];k=p[i];ans++;}if(ans>=2)return 1;}if(n>1){ans++;k=n;}if(ans>=2)return 1;elsereturn k;}int main(){ll n,i;init();for(i=3;i<=N;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;}
0 0
- hdoj f(n) 2582 (GCD打表&找规律)好题
- HDOJ 2582 f(n) (YY+找规律)
- Problem F. √ (N im) -Gym 100345F-找规律+打表
- f(n) + 找规律
- hdu 2582 f(n)(找规律,素数筛选,优化)
- HDOJ 2674 N!Again(找规律)
- hdu_2582 f(n) (找规律)
- hdoj 1005 Number Sequence (找规律,打表)
- Alarm(找规律+素数打表)
- hdu2582 f(n) 找规律 素数筛
- HDU2583 f(n) 规律题
- ACM ICPC 2008–2009, NEERC, Problem F Fenwick Tree(找规律,打表) Codeforces Gym 100623F
- zoj 3622 Magic Number (找规律+打表)
- hdu 4664 Triangulation(博弈论 sg找规律打表)
- UVA 10706 Number Sequence (找规律 + 打表 + 查找)
- 玲珑学院OJ 1010 - Alarm(找规律+打表)
- Number Sequence 【打表】+【找规律】
- hdu_6124_打表_找规律
- 各种内部排序算法复杂度的比较和排序方法的选择
- 计算机网络(分组交换与电路交换)
- Number of 1 Bits
- $locationChangeStart ,$locationChangeSuccess,$routeChangeStart,$routeChangeSuccess如何使用
- 【PA2014】【BZOJ3727】Zadanie
- hdoj f(n) 2582 (GCD打表&找规律)好题
- 黑马程序员之System、Runtime、Math、Date、Calendar类
- 动态引用APK文件
- Android LocalSocket与Socket 区别
- UVA 11624 两次bfs搞搞
- UITextView 光标定位
- WPF Prism 中Command绑定的简单应用
- 关于UI适配的文档
- 计算机网络(浅谈应用层协议原理)