HDU 2582 f(n) (组合数的gcd)

78 篇文章 0 订阅


f(n)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 365    Accepted Submission(s): 221


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
 
Source
 
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2582

题目大意:求f(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n),Gcd(n)=gcd(C[n][1],C[n][2],……,C[n][n-1])

题目分析:显然若n有两个以上的素因子则Gcd(n) = 1,若n=p^k则Gcd(n) = p,O(n)素数筛 + O(20 * primenum)处理p^k的数,预处理复杂度为O(n),查询直接O(1)

#include <cstdio>
#define ll long long
int const MAX = 1e6 + 5;
int p[MAX];
bool noprime[MAX];
ll num[MAX], ans[MAX];

void pre()
{
    int pnum = 0;
    for(int i = 2; i < MAX; i++)
    {
        num[i] = 1;
        if(!noprime[i])
            p[pnum ++] = i;
        for(int j = 0; j < pnum && i * p[j] < MAX; j++)
        {
            noprime[i * p[j]] = true;
            if(i % p[j] == 0)
                break;
        }
    }
    for(int i = 0; i < pnum; i++)
    {
        ll tmp = 1;
        for(int j = 0; j <= 20; j++)
        {
            tmp = tmp * p[i];
            if(tmp > MAX)
                break;
            num[tmp] = p[i];
        }
    }
    for(int i = 3; i < MAX; i++)
        ans[i] = ans[i - 1] + num[i];
}

int main()
{
    pre();
    int n;
    while(scanf("%d", &n) != EOF)
        printf("%lld\n", ans[n]);
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值