hud 2582 f(n)
大表:
#include<iostream> #include<stdio.h> #include<algorithm> using namespace std; typedef long long ll; int main(){ for(int j=1;j<=50;j++)//打表 { int n=j; ll ans=n; ll tt=n; for(int i=2;i<n;i++) { tt=tt*(n-i+1)/i; ans=__gcd(ans,tt); } printf("%d %d\n",j,ans); } return 0; }
结论:Gcd(n) : 当n是素数,则为本身,否则为1;
代码:
#include<iostream> #include<stdio.h> using namespace std; typedef long long ll; const int maxn = 1e6+10; ll Gcd[maxn]; void init(){ for(int i=0;i<=maxn;i++) Gcd[i] = 0; for(int i =2;i<=maxn;i++){ if(!Gcd[i]){ Gcd[i] = i; for(int j=2*i;j<=maxn;j+=i){ if(!Gcd[j]) Gcd[j] = i; else Gcd[j] = 1; } } } for(int i=4;i<=maxn;i++) Gcd[i]+=Gcd[i-1]; } int main(){ init(); int n; while(scanf("%d",&n)!=EOF){ cout<<Gcd[n]<<endl; } return 0; }