HDU 2582 - f(n)(因数分解+素筛+规律)

f(n)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 806    Accepted Submission(s): 497


 

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.

 

 

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).

 

 

Sample Input

 

3

26983

 

 

Sample Output

 

3

37556486

 

思路: Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n-1]),通过打表找规律后发现,

n 为质数时 ,Gcd(n) = n;

n 只有一个质因子时,Gcd(n) = 这个质因子

n 有多个质因子时, Gcd(n)  = 1;

先素筛打表,然后把每个Gcd(n)的和 预处理出来。

#include<bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define ll long long

const int maxn = 1e6+10;
const int N = 3010;

ll n;
ll a[maxn],pri[maxn];
bool prime[maxn];
void Is_prime(){
    memset(prime,true,sizeof(prime));
    prime[0] = prime[1] = false;
    for(int i=2;i*i<maxn;i++){
        if(prime[i]){
            for(int j=2;i*j<maxn;j++){
                prime[i*j] = false;
            }
        }
    }
    ll num = 1;
    for(int i=1;i<=maxn;i++){
        if(prime[i]){
            pri[num++] = i;
        }
    }
}
int G(ll n){
    if(prime[n])  return n;
    ll num = 0,ans = 1;
    for(int i=1;pri[i]*pri[i]<=n;i++){
        if(n%pri[i]==0){
            num ++;
            ans = pri[i];
            while(n%pri[i]==0)n/=pri[i];
        }
        if(num > 1) return 1;
    }
    if(n!=1){
        num ++ ;ans = n;
    }
    if(num > 1)return 1;
    return ans;
}
void get_num(){
    a[0] = a[1] = a[2] = 0;
    for(int i=3;i<maxn;i++){
        a[i] = G(i) + a[i-1];
    }
}
int main(){
    Is_prime();
    get_num();
    while(scanf("%lld",&n)!=EOF){
        printf("%lld\n",a[n]);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值