51nod 1040最大公约数和(欧拉函数)
2024-08-24 02:40:08
收藏
关注
给出一个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 ;
}
最新文章
- ubuntu连接Android调试
- 服务器的Arch Linux,CentOS的,Debian的,Fedora的,Gentoo的,openSUSE的,Slackware的,和Ubuntu哪个好
- C#发送邮件源码
- 如何隐藏DLL中,导出函数的名称?
- Ubuntu环境openresty的安装
- 关于web.xml的格式
- Linux基础(三)
- Java 前后端List传值
- Exception in thread ";main"; java.lang.NoClassDefFoundError: org/apache/hadoop/fs/CanUnbuffer
- 【BZOJ1211】【HNOI2004】树的计数 prufer序列
- 我用MATLAB撸了一个2D LiDAR SLAM
- flask 自动切换环境
- 【Linux】【Jenkins】邮件发送失败的问题javax.mail.MessagingException: Could not connect to SMTP host:
- ubuntu 14.04安装zabbix3.0以及汉化
- bind函数详解(转)
- Intel daal数据预处理
- python3爬虫(find_all用法等)
- HTML5 Canvas ( 图形变换矩阵 ) transform, setTransform
- Java程序设计17——多线程-Part-B
- QuantLib 金融计算——随机过程之一般 Black Scholes 过程
热门文章
- QFontMetrics的一个问题
- https网站跳转到http网站时,referrer获取不到的问题
- Java的super调用案例: super.getClass()返回的是子类自己
- Yeo 17-ROI parcellation
- ASP.NET中处理自定义错误的最佳方式
- Router的创建者——RouteBuilder
- 1003. Emergency
- cpu负载和利用率
- Makefile 中:= ?= += =的区别
- 我这样理解js里的this