题意

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. CSS(二)属性--文本设置

    HTML代码一 <body> <div>这是一个很黑很黑的夜晚,黑云密布,没有任何光亮透过.卖火柴的小姑娘.......</div> </body> C ...

  2. ActiveMQ的P2P示例

    ActiveMQ的P2P示例(点对点通信) (1)下载安装activemq,启动activeMQ. 详细步骤参考博客:http://www.cnblogs.com/DFX339/p/9050878.h ...

  3. php随手记

    引用(&)是变量的别名,而不是指针,可用unset(变量名)把此变量的别名注销掉,等于没有声明此变量. @为错误抑制符,可以用在任何表达式前面. ``为命令操作符,可以执行系统命令. inst ...

  4. transclude

    http://jsfiddle.net/ospatil/A969Z/157/ transclude :true  允许指令内部的dom元素, 保留到 自定义指令的template属性里的含有 ng-t ...

  5. C语言获取系统时间的几种方式

    C语言获取系统时间的几种方式 2009-07-22 11:18:50|  分类: 编程学习 |字号 订阅     C语言中如何获取时间?精度如何? 1 使用time_t time( time_t * ...

  6. softmax与多分类

    sotfmax 函数在机器学习和深度学习中有着广泛的应用, 主要用于多分类问题. softmax 函数 1. 定义 假定数组V,那么第i个元素的softmax值为 也就是该元素的指数 除以 所有元素的 ...

  7. 实现简单的shell sed替换功能

    通过脚本传参数可以实现替换 # -*-coding:utf-8-*- # Author:sunhao import sys f = open('yesterday','r',encoding='utf ...

  8. MVC实现上传图片的方法

    Form提交时,须注意form需要添加属性enctype="multipart/form-data",否则Request.Files.Count=0,无法上传图片. cshtml代 ...

  9. mysql str_to_date 字符串 转日期时间

    SELECT STR_TO_DATE('2018-05-05 14:00:00.5555','%Y-%m-%d %H:%i:%s') from DUAL;

  10. run

    和配置块不同,运行块在注入器创建之后被执行,它是所有AngularJS应用中第一个被执行的方法运行块通常用来注册全局的事件监听器.例如,我们会在.run()块中设置路由事件的监听器以及过滤未经授权的请 ...