integer f(n)(数论)

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.
InputThere are several test case. For each test case:One integer n(3<=n<=1000000). The end of the in put file is EOF.OutputFor each test case:

The output consists of one line with one integer f(n).
Sample Input
3
26983
Sample Output
3
37556486
题意:这也是根据条件求解f[n],f(n)= Gcd(3)+Gcd(4)+…+Gcd(i)+…+Gcd(n). 看着这个条件,和对里面Gcd的解释挺复杂的。
解题思路:这都题,之前一直找不到方法,因为Gcd的求解有点复杂,然后通过观看大佬的博客才知道人家用打表找出了规律;
1.当n为素数时,那么Gcd[n]就为n本身;
2.当n有且只有一个素因子时,除了自己本身,那么Gcd[n]就为这个素因子,如4,4=2^2所就为2;
3.当n有多个素因子时,那么Gcd[n]就为1;
解题步骤
1)查找范围内的,素数和素数的某些倍数(只有这一个素因子),因为他们的Gcd的值都为这个素数,然后有其他有多个素因子的就为1;
2)让然后根据等式,储存所有的数;
3)然后根据输入,进行对应输出就行;

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 1000005
long long Gcd[maxn];
int main()
{
    int n;
    memset(Gcd,0,sizeof(Gcd));
    for(int i=2;i<=maxn;i++)
        {
            if(!Gcd[i])
                {
                    Gcd[i]=i;
                    for(int j=i*2;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];
    while(scanf("%d",&n)!=EOF)
        {
            cout<<Gcd[n]<<endl;
        }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值