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

题目意思:给定一个数n,求1-n的所有数与这个数的gcd之和。
解题思路:先引入欧拉函数概念,φ(n) :1到n-1有几个和x互质的数。
我们可以得知那些与n互质的数的gcd一定为1,也就是欧拉函数。而那些不与n互质的数gcd(i, n) = d ,d为n的约数。而这些约数必然是n的因子。

那么我们便可以想一想有没有什么方法可以直接将d相同的都找出来,我一瞬间想到的是筛法来筛去,但是筛法在面临很大的数的时候也会时间超限,所以需要换一种思路。我们会发现n的因子数很少啊,是不是可以直接去枚举因子呢?那么我就要判断gcd(i,n)中具有相同d的那些数,gcd(i,n)=d,i/d和n/d必须是互质的,也就是gcd(i/d,n/d)=1,而这个式子是什么?这就是求i/d和n/d互质的i在[1,n]里有几个,就等价于 1/d,2/d,...,n/d里面有几个和n/d互质,即φ(n/d)。接着求和约数为d的有φ(n/d)个,所以就是d*φ(n/d),同时把约数为n/d的加上去,i*i==n特判一下。

以12为例:1,2,3,4,5,6,7,8,9,10,11,12。首先我们用欧拉函数将与12互质的1,5,7,11剔除,12特殊也剔除。这时候的ans=1+1+1+1+12=16。

这时候剩下2,3,4,6,8,9,10这些数与12的gcd没有计算。

接着我们枚举12的因子,ans+=2*eular(6)     2*2=4 (约数为2的有2个)

ans+=3*eular(4)     3*2=6   (约数为3的有2个)

ans+=4*eular(3)     4*2=8   (约数为4的有2个)

ans+=6*eular(2)     6*1=6    (约数为6的有1个)

完全和真实情况相同

原数:2,3,4,6,8,9,10

与12的公约数:2,3,4,6,4,3,2

是不是两个2,两个3,两个4,一个6?

 #include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long int
using namespace std;
ll eular(ll n)
{
ll i,ret=n;
for(i=; i<=sqrt(n); i++)
{
if(n%i==)
{
ret=ret/i*(i-);
while(n%i==)
{
n/=i;
}
}
}
if(n>)
{
ret=ret/n*(n-);
}
return ret;
}
int main()
{
ll n,num,i,j;
ll ans;
while(scanf("%lld",&n)!=EOF)
{ ans=eular(n)+n;
for(i=;i<=sqrt(n);i++)
{
if(n%i==)
{
if(i*i==n)
{
ans=ans+eular(i)*i;
}
else
{
ans=ans+eular(i)*(n/i);
ans=ans+eular(n/i)*i;
}
}
}
printf("%lld\n",ans);
}
return ;
}

Longge's problem(欧拉函数应用)的更多相关文章

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

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

  2. 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 ...

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

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

  4. BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 2553  Solved: 1565[Submit][ ...

  5. Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论

    2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 1959  Solved: 1229[Submit][ ...

  6. 【bzoj2705】[SDOI2012]Longge的问题 欧拉函数

    题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). 输入 一个整数,为N. 输出 ...

  7. BZOJ2705: [SDOI2012]Longge的问题(欧拉函数)

    题意 题目链接 Sol 开始用反演推发现不会求\(\mu(k)\)慌的一批 退了两步发现只要求个欧拉函数就行了 \(ans = \sum_{d | n} d \phi(\frac{n}{d})\) 理 ...

  8. [SDOI2012] Longge的问题 - 欧拉函数

    求 \(\sum\limits_{i=1}^{n}gcd(i,n)\) Solution 化简为 \(\sum\limits_{i|n}^{n}φ(\dfrac{n}{i})i\) 筛出欧拉函数暴力求 ...

  9. UVa 10837 A Research Problem 欧拉函数

    题意: 给你一个欧拉函数值 phi(n),问最小的n是多少. phi(n) <= 100000000 , n <= 200000000 解题思路: 对于欧拉函数值可以写成 这里的k有可能是 ...

  10. Bzoj-2705 Longge的问题 欧拉函数

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2705 题意: 求 sigma(gcd(i,n), 1<=i<=n<2^3 ...

随机推荐

  1. 手把手教你创建私有podspec

    本文来自 网易云社区 . CocoaPods是iOS非常好用的类库管理工具,可以非常方便的管理和更新项目中使用到的第三方库,以及将自己项目中的公共组件交由它管理. 工作中比较常用到的是通过CocoaP ...

  2. 歌词解析&class

    class song_song: def __init__(self,lrc_file): # 定义两个字典一个列表备用 self.song_file = lrc_file self.song_lrc ...

  3. js实现把textarea通过换行或者回车把多行数字分割成数组,并且去掉数组中空的值。

    删除数组指定的某个元素 var msg = " ";  //textarea  文本框输入的内容 var emp = [ ];   //定义一个数组,用来存msg分割好的内容 1. ...

  4. CentOS6编译安装gcc高版本

    编译安装gcc高版本 因CentOS中gcc版本仅有4.4,故编译安装gcc高版本. 安装依赖库(如果你已安装过gcc低版本,可跳过这步) yum install glibc-static libst ...

  5. IOTutility 一个轻量级的 IOT 基础操作库

    IOTutility 一个轻量级的 IOT 基础操作库 Base utility for IOT devices, networking, controls etc... IOTutility 的目的 ...

  6. espcomm_send_command: didn't receive command response | espcomm_send_command(FLASH_DOWNLOAD_BEGIN) failed |arduino wemos d1 无法上传

    espcomm_send_command: didn't receive command response espcomm_send_command(FLASH_DOWNLOAD_BEGIN) fai ...

  7. Python学习:15.Python面向对象(二、继承的各种情况)

    一.什么是继承 继承是一种创建类的方法,在python中,一个类可以继承来自一个或多个父.原始类称为基类或超类. #创建父类 class Parent1: pass class Parent2: pa ...

  8. aircrack-ng 破解无线网络

    1.科普当今时代,wifi 已成为我们不可缺少的一部分,上网.看视频.玩游戏,没有 wifi 你就等着交高额的流量费吧,本来我想单独的写 wpa 破解和 wps 破解,后来觉得分开写过于繁琐,索性合并 ...

  9. gcd 模板

    声明 给 x,y 两个数,求 x,y 的最大公因数. 辗转相除法,直接套!!! function gcd(x,y:longint):longint; begin then exit(x) else e ...

  10. IDEA新建Web项目

    file->New project->输入项目名(例如这里输入HelloWeb) 选择JDK为合适版本->next ->finish即可 在新建的项目HelloWorld上右击 ...