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. 新版台式机安装win7操作系统

    1.进入bios设置-----authentication选项中的secure boot设置为disabled2.在boot options选项中launch csm设置为always 3.在boot ...

  2. nginx+php产生大量TIME_WAIT连接解决办法

    问题:当启动nginx和php-fpm时,使用netstat -tunap查看到大量TIME_WAIT连接 由于不知道原因,害怕是受到攻击,马上killall nginx 和php-fpm 会不会是8 ...

  3. qcow2磁盘加密及libvirt访问

      1.创建qcow2加密磁盘[root@Coc-5 test_encrypt]# qemu-img convert -f qcow2 -O qcow2 -o encryption template_ ...

  4. Ubuntu13.10:密码忘记了怎么办?

    重启ubuntu系统,开机时长按shift按键进入GRUB菜单,选择第二个高级模式. 新版的UBUNTU系统居然启用了GRUNB2.0的内核!虽然传说相当的牛X,但是用起来感觉就是非常看不清楚字体,要 ...

  5. CCNode作为容器实现显示区域剪裁

    一直把ccnode当做ui元素的容器使用,比如一段带下划线的文字,我会在一个ccnode中加入一个label和一个sprite,然后作为一个整体传出. 在主界面聊天的时候遇到一个问题,一段聊天信息需要 ...

  6. 上传项目到git

    …or create a new repository on the command line   echo "# test" >> README.md git ini ...

  7. dbf 命令 及数据类型

    left()函数只能截取左边几位字符,截取中间字符用SUBSTR() SUBSTR(cExpression, nStartPosition [, nCharactersReturned]) dbf u ...

  8. web.xml执行顺序

    出自:http://blog.csdn.net/u010833154/article/details/50697987 引言:启动java web程序的时候,java web会读取配置文件web.xm ...

  9. ELK 日志管理系统,再次尝试记录

    简介: 第二次尝试 ELK 记录... 工作流程: 1.客户端的 Logstash 将日志信息采集到之后传输给 Redis 做消息队列 2.然后服务端的 Logstash 将日志从 Redis 中取出 ...

  10. linux 系统的后台运行

    后台运行:nohup 程序名 & 杀死后台 ps -ef|grep 名称 kill -9 id(查出的程序id)