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

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中所有的数与n的最大公约数的和。 思路:这题是欧拉函数的应用。假设x与n的最大公约数是a,也就是gcd(x,n)=a,那么我们就可以知道gcd(x/a,n/a)=1,也就是x/a与n/a互质。
这说明在1-n中与 n 的最大公约数为a的数都与n/a互质,所以我们就用欧拉函数求出1-n/a中,与n/a互质的数有多少个,其中,a是n的约数,所以遍历所有n的约数。
这题求的是与n的最大公约数的和sum,所以当a是n的约数时,sum就要加上a*欧拉(n/a); 代码:
 #include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#define eps 1e-7
#define ll long long
#define inf 0x3f3f3f3f
#define pi 3.141592653589793238462643383279
using namespace std;
ll euler(ll n) // 求欧拉函数值
{
ll res = n,a = n;
for(ll i=; i*i<=a; ++i)
{
if(a%i==)
{
res = res/i*(i-);
while(a%i==) a /= i;
}
}
if(a > ) res = res/a*(a-);
return res;
} int main()
{
ll n;
while(scanf("%lld",&n)!=EOF)
{
ll ans=;
for(ll i=; i*i<=n; ++i) //遍历求n的约数
{
if(n%i == )
{
ans += i*euler(n/i); //euler(n/i)求出与 n 的最大公约数为 i 的数有多少,再乘上公约数 i
if(n/i != i)
ans += euler(i)*(n/i); //同理求出与n的最大公约数为n/i的数有多少
}
}
printf("%lld\n",ans);
}
return ;
}
												

poj2480——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. tornado 自定义session (二)

    有了上一步的基础,我们将session改造成一个模块,这样我们就可以通过配置,来使用不同方式(redis,数据库等)的session. 添加一个新目录:TornadoSession conf.py是配 ...

  2. sortingOrder,sortingLayer

    sortingOrder 是sortingLayer内的分级 sortingLayer是抽象的分层,用于决定2D物体绘制的先后顺序. 2D物体分两类:sprite和UI. sprite虽是2D,却可以 ...

  3. Innobackupex MySQL 全备、增备及恢复

    简介: 在这之前都是通过 mysqldump 来备份数据库的,由于是逻辑备份,所以采用这种备份方式数据是很安全的,跨平台.版本都很容易. 凡事有利必有弊,逻辑备份在你数据库比较大时,备份.恢复数据所耗 ...

  4. sql中问号是干什么的??

    第一次在后台 程序中遇到sql语句中的问号: /** * * 方法描述 : 通过账号id更新该账号状态 * @param state 状态 * @param id 账号id */ @Modifying ...

  5. spring中 Bean的装配 Bean后处理器

  6. Bug of VS2015+WDK

    1>  Signability test failed.1>  1>  Errors:1>  22.9.7: DriverVer set to incorrect date ( ...

  7. 【Rsync项目实战一】备份全网服务器数据

    目录 [Rsync项目实战]备份全网服务器数据 [企业案例] 1.1 环境部署 1.2 开始部署backup服务器:Rsync服务端过程: 1.3 开始部署nfs01服务器:Rsync客户端过程: [ ...

  8. 81-POJ-Wall(计算几何)

    http://poj.org/problem?id=1113 Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 411 ...

  9. Linux 系统配置

    centos 7 防火墙和端口配置开启redis端口,修改防火墙配置文件 vi /etc/sysconfig/iptables 加入端口配置 -A RH-Firewall-1-INPUT -m sta ...

  10. Centos6.6升级python2到python3

    系统更新部分: 一.由于系统原有的源无法连接,需要更新为新的源.起初,首选163的源,但是由于更改源以后,无法使用yum等问题,所以直接使用上海交通大学提供的源. 修改前,将原来/etc/yum.re ...