题目来源: rihkddd
基准时间限制:1 秒 空间限制:131072 KB 分值: 80 难度:5级算法题
 收藏
 关注
给出一个n,求1-n这n个数,同n的最大公约数的和。比如:n = 6
1,2,3,4,5,6 同6的最大公约数分别为1,2,3,2,1,6,加在一起 = 15
 
Input
1个数N(N <= 10^9)
Output
公约数之和
Input示例
6
Output示例
15
思路:
目的是求∑(i= 1,n) gcd( i , n );
gcd( i , n ) = x,表示x是n的因子。稍作变形gcd( i / x , n / x) = 1,
看到这个式子可以想到欧拉函数,也就是求比n/x小的与其互质的个数。
因为这些书和n/x互质,乘上x后与n的最大公约数只有x。
也就是说我们先求出每个因子,然后计算每个因子有多少贡献即可。
 

/*
* Author: sweat123
* Created Time: 2016/6/27 14:01:46
* File Name: main.cpp
*/
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<time.h>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1<<30
#define MOD 1000000007
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define pi acos(-1.0)
using namespace std;
const int MAXN = ;
int n;
ll ef(int n)
{
ll cnt = n;
int i;
for(i = ; i * i <= n; i++){
if(n % i == )
{
cnt -= cnt / i; // (x-x/p1) *(1-1/p2)*(1-1/p3)*(1-1/p4).....
while(n % i == )
n /= i;
}
}
if(n > ) cnt -= cnt / n;
return cnt;
} int main(){
while(~scanf("%d",&n)){
ll ans = ;
for(int i = ; i <= (int)sqrt(n); i++){
if(n % i == ){
ans += ef(n / i) * i;
if(n / i != i){
ans += ef(i) * (n / i);
}
}
}
printf("%lld\n",ans);
}
return ;
}

最新文章

  1. ubuntu连接Android调试
  2. 服务器的Arch Linux,CentOS的,Debian的,Fedora的,Gentoo的,openSUSE的,Slackware的,和Ubuntu哪个好
  3. C#发送邮件源码
  4. 如何隐藏DLL中,导出函数的名称?
  5. Ubuntu环境openresty的安装
  6. 关于web.xml的格式
  7. Linux基础(三)
  8. Java 前后端List传值
  9. Exception in thread &quot;main&quot; java.lang.NoClassDefFoundError: org/apache/hadoop/fs/CanUnbuffer
  10. 【BZOJ1211】【HNOI2004】树的计数 prufer序列
  11. 我用MATLAB撸了一个2D LiDAR SLAM
  12. flask 自动切换环境
  13. 【Linux】【Jenkins】邮件发送失败的问题javax.mail.MessagingException: Could not connect to SMTP host:
  14. ubuntu 14.04安装zabbix3.0以及汉化
  15. bind函数详解(转)
  16. Intel daal数据预处理
  17. python3爬虫(find_all用法等)
  18. HTML5 Canvas ( 图形变换矩阵 ) transform, setTransform
  19. Java程序设计17——多线程-Part-B
  20. QuantLib 金融计算——随机过程之一般 Black Scholes 过程

热门文章

  1. QFontMetrics的一个问题
  2. https网站跳转到http网站时,referrer获取不到的问题
  3. Java的super调用案例: super.getClass()返回的是子类自己
  4. Yeo 17-ROI parcellation
  5. ASP.NET中处理自定义错误的最佳方式
  6. Router的创建者——RouteBuilder
  7. 1003. Emergency
  8. cpu负载和利用率
  9. Makefile 中:= ?= += =的区别
  10. 我这样理解js里的this