题意

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. weblogic部署web项目(war包)

    第一步,启动并访问weblogic,进入登录页面 第二步,进入主页面,开始部署项目 第三步,上载项目war包 选择需要上载的本地war包 第四步,开始项目配置 继续下一步 选择红色标记的配置 第五步, ...

  2. day23 模块02

    核能来袭--模块 2 1.nametuple() 2.os模块 3.sys模块(重点) 4.序列化 (四个函数) 5.pickle(重点) 6.json(重点中的重点) 1.nametuple() 命 ...

  3. 关于collectionview布局的坑

    不知道写了多少次collectionview,步了很多坑,现在看来虽然达到了自己想要的结果,却不知道其中所以然.还是总结一下,免得再走弯路: 场景是这样的,我要定制一个显示选择图片的排列,想要实现横向 ...

  4. html回顾随笔1(*^__^*)

    1.text—align 与float 区别: float是针对div一类的容器来说.text-align是对于容器里的文本或者图片来说靠左或靠右水平对齐(vlign 竖直方向) 要注意以下几点:   ...

  5. centos7 安装jdk8 bash脚本 并配置环境变量

    #!/bin/bash #安装java脚本 if type -p java; then echo 'java已安装.' exit else echo '开始安装java...' wget --no-c ...

  6. android 自定义命名空间 http://schemas.android.com/apk/res-auto

    XML中用 xmlns="http://schemas.android.com/apk/res-auto" 获取自定义属性值: public static String NAMES ...

  7. EF Code First MySql 主从表设计的一些需要注意的内容

    假如有下面两张表 public class Main { public int Id{get;set;} public string Name{get;set}; public virtual ICo ...

  8. L320 What late-night snacking has to do with sleep

    What late-night snacking has to do with sleep Light-night snacking isn’t just something college stud ...

  9. VS2010,MFC动态按钮和窗体背景图片,以及是静态文字控件透明,并避免静态文字刷新出现的重叠问题

    1.动态按钮的四种动作 1)正常 2)按下 3)滑过 4)失效 在MFC中,4个动作对应着四种位图bmp, 首先,将代表四种状态的位图加载入资源中,将对应的按钮设置为BitmapButton 第二,在 ...

  10. 团队-爬取豆瓣电影TOP250-需求分析

    需求: 1.搜集相关电影网址 2.实现相关逻辑的代码 项目步骤: 1.通过豆瓣网搜索关键字,获取相关地址 2.根据第三方包实现相关逻辑