博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 2582 f(n) 素因子
阅读量:3903 次
发布时间:2019-05-23

本文共 1538 字,大约阅读时间需要 5 分钟。

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(3)=3;Gcd(4)=2;Gcd(5)=5;Gcd(6)=1;Gcd(7)=7;Gcd(8)=2;Gcd(9)=3;Gcd(10)=1;这道题的规律如下:1.如果为x素数,则Gcd(x)=x;2.如果素因子只有一个则Gcd(x)=素因子3.如果有多个素因子,则Gcd(x)=1;*/#include 
#include
#include
#include
#include
#include
using namespace std;const int maxn=1000005;int n;int isnot[maxn];vector
pri;long long int sum[maxn];void init(){ memset (isnot,0,sizeof(isnot)); memset (sum,0,sizeof(sum));}void Judge (){ for (int i=2;i<=maxn-5;i++) { if(!isnot[i]) { pri.push_back(i); for (int j=i*2;j<=maxn-5;j+=i) { isnot[j]=1; } } }}int Pnum (int x){ int num=0,p; for (int i=0;pri[i]*pri[i]<=x;i++) { if(x%pri[i]==0) { num++; if(num>1) return 1; p=pri[i]; while (x%pri[i]==0) { x/=pri[i]; } } } if(x!=1) { num++; x=n; } if(num>1) return 1; else return p;}int main(){ init(); Judge(); sum[3]=3; for (int i=4;i<=maxn-5;i++) { if(!isnot[i]) sum[i]=sum[i-1]+i; else sum[i]=sum[i-1]+Pnum(i); } while (scanf("%d",&n)!=EOF) { printf("%lld\n",sum[n]); } return 0;}

 

 

转载地址:http://rxaen.baihongyu.com/

你可能感兴趣的文章
最新最全Apache源码编译安装
查看>>
最新mysql数据库源码编译安装。
查看>>
第一章 vue入门
查看>>
Linux文件引用计数的逻辑
查看>>
linux PCIe hotplug arch analysis
查看>>
LDD3 study note 0
查看>>
cpio compress and extract
查看>>
PCI SMMU parse in ACPI
查看>>
const使用实例
查看>>
面向对象设计案例——点和圆关系
查看>>
深拷贝与浅拷贝
查看>>
WinForm 打开txt文件
查看>>
WinForm 实现日志记录功能
查看>>
WinForm 读取照片文件
查看>>
WinForm ComboBox不可编辑与不可选择
查看>>
WinForm textbox控件设置为不可编辑
查看>>
winForm ImageList图像控件使用
查看>>
WinForm TabControl标签背景色
查看>>
Winform TabControl标签美化
查看>>
打印日志类
查看>>