【HDU - 2582】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.

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=gcd(C_n^1,C_n^2,...,G_n^{n-1})

规律为:

(1)如果n有多个素因子,那么Gcd=1; 
(2)如果n只有一个素因子,那么Gcd=该素因子。(比如16=2^4,只有一个素因子2所以Gcd=2。而11本身是素数也只有一个素因子,所以gcd=11就是它本身)

 

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
ll Gcd[1000100];
int main()
{
	
//	for(int j=1;j<=45;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);
//	}
	memset(Gcd,0,sizeof(Gcd));
	for(int i=2;i<=1000000;i++)
	{
		if(!Gcd[i])
		{
			Gcd[i]=i;
			for(int j=i*2;j<=1000000;j+=i)
			{
				if(!Gcd[j])//j不是素数,现在找到他的第一个因子是i
				Gcd[j]=i;
				else//之前已经找到了他的一个因子,现在又找到一个,说明不止一个因子
				Gcd[j]=1; 
			}
		}
	}
	for(int i=4;i<=1000000;i++)
	{
		Gcd[i]+=Gcd[i-1];
	}
	int n;
	while(~scanf("%d",&n))
	{
		printf("%lld\n",Gcd[n]);
	}
	return 0; 
} 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值