题意

Language:Default
Longge's problem
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10642 Accepted: 3563

Description

Longge is good at mathematics and he likes to think about hard mathematical problems which will be solved by some graceful algorithms. Now a problem comes: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N.



"Oh, I know, I know!" Longge shouts! But do you know? Please solve it.

Input

Input contain several test case.

A number N per line.

Output

For each N, output ,∑gcd(i, N) 1<=i <=N, a line

Sample Input

2
6

Sample Output

3
15

Source

POJ Contest,Author:Mathematica@ZSU

分析

\[\sum_{i=1}^n\gcd(i,n) \\
=\sum_{d|n}d*\sum_{i=1}^{\frac nd}[\gcd(i,\frac nd)=1]=\sum_{d|n}d*\varphi(\frac nd) \\
=\sum_{d|n}d*\frac nd *\prod_{i=1,p_i|\frac nd}^m(1-\frac 1p_i)
\]

那么直接把\(n\)质因数分解就行了。时间复杂度\(O(\sqrt{n})\)

#include<iostream>
typedef long long ll;
int main(){
ll n;
while(~scanf("%lld",&n)){
ll ans=n;
for(ll i=2,cnt;i*i<=n;++i)if(n%i==0){
cnt=0;
while(n%i==0) n/=i,++cnt;
ans=ans/i*((i-1)*cnt+i);
}
if(n>1) ans=ans/n*((n-1)+n);
printf("%lld\n",ans);
}
return 0;
}

POJ2480 Longge's problem的更多相关文章

  1. poj2480——Longge's problem(欧拉函数)

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9190   Accepted: 3073 ...

  2. POJ2480 Longge's problem gcd&&phi

    题意简洁明了.做这题主要是温习一下phi的求法.令gcd(i,n)=k,实际上我们只需要求出有多少个i使得gcd(i,n)=k就可以了,然后就转化成了求phi(n/k)的和,但是n很大,我们不可能预处 ...

  3. POJ2480:Longge's problem(欧拉函数的应用)

    题目链接:传送门 题目需求: Given an integer N(1 < N < 2^31),you are to calculate ∑gcd(i, N) 1<=i <=N ...

  4. Longge's problem poj2480 欧拉函数,gcd

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6918   Accepted: 2234 ...

  5. poj 2480 Longge's problem 欧拉函数+素数打表

    Longge's problem   Description Longge is good at mathematics and he likes to think about hard mathem ...

  6. POJ 2480 Longge's problem 欧拉函数—————∑gcd(i, N) 1<=i <=N

    Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6383   Accepted: 2043 ...

  7. poj 2480 Longge's problem [ 欧拉函数 ]

    传送门 Longge's problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7327   Accepted: 2 ...

  8. Longge's problem

    Longge's problem 求\(\sum_{i=1}^ngcd(i,n)\),\(n< 2^{31}\). 解 理解1: 注意式子的实际意义,显然答案只可能在n的约数中,而现在问题变成了 ...

  9. Longge's problem(欧拉函数应用)

    Description Longge is good at mathematics and he likes to think about hard mathematical problems whi ...

随机推荐

  1. unity中实现三个Logo图片进行3秒钟的若隐若现后互相切换Logo图片

    private List<Sprite> storeTexture; public void Start() { storeTexture = new List<Sprite> ...

  2. caffe,Inception v2 Check failed: top_shape[j] == bottom[i]->shape(j)

    使用Caffe 跑 Google 的Inception V2 对输入图片的shape有要求,某些shape输进去可能会报错. Inception model中有从conv和pooling层concat ...

  3. 使用perfect进行服务端开发

    最近闲来无事,研究了下基于perfect的swift后端开发.根据大神的博客进行了简单的配置,加深下印象也算是和各位分享一下. 参考博客:http://www.cnblogs.com/ludashi ...

  4. requests(第三方模块) 请求、登录、下载网页

    import requests  #http://docs.python-requests.org/en/latest/api/ 说明文档 ''' requests.request(method,ur ...

  5. rnn-手写数字识别-网络结构-shape

    手写数字识别经典案例,目标是: 1. 掌握tf编写RNN的方法 2. 剖析RNN网络结构 tensorflow编程 #coding:utf-8 import tensorflow as tf from ...

  6. poj3080(kmp+枚举)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20163   Accepted: 8948 Descr ...

  7. AndroidSDK 自带定位工具 uiautomatorviewer

    前言:uiautomatorviewer是androidSDK自带的定位工具 1.打开目录D:\Android\androidSDK\tools\bin 2.点击启动uiautomator,页面显示如 ...

  8. Pycharm连接Git及使用

    环境: Git-2.7.2-32-bit_setup.1457942412.exe TortoiseGit-2.4.0.2-64bit.msi 安装配置Git后,打开Pycharm.file--> ...

  9. mysql创建用户并给用户分配权限

    1.登录Mysql [root@xufeng Desktop]# mysql -u root -pEnter password: Welcome to the MySQL monitor. Comma ...

  10. wx小程序 使用字体

    1.下载项目下的字体库 2.解压复制iconfont.css中的代码到,小程序 app.wxss 3.使用: //icon-begindate表示开始时间的图标 <text class=&quo ...