poj2480——Longge's problem(欧拉函数)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 9190 | Accepted: 3073 |
Description
"Oh, I know, I know!" Longge shouts! But do you know? Please solve it.
Input
A number N per line.
Output
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(欧拉函数)的更多相关文章
- poj 2480 Longge's problem [ 欧拉函数 ]
传送门 Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7327 Accepted: 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 ...
- poj 2480 Longge's problem 欧拉函数+素数打表
Longge's problem Description Longge is good at mathematics and he likes to think about hard mathem ...
- BZOJ 2705: [SDOI2012]Longge的问题 [欧拉函数]
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 2553 Solved: 1565[Submit][ ...
- Bzoj 2705: [SDOI2012]Longge的问题 欧拉函数,数论
2705: [SDOI2012]Longge的问题 Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 1959 Solved: 1229[Submit][ ...
- 【bzoj2705】[SDOI2012]Longge的问题 欧拉函数
题目描述 Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). 输入 一个整数,为N. 输出 ...
- BZOJ2705: [SDOI2012]Longge的问题(欧拉函数)
题意 题目链接 Sol 开始用反演推发现不会求\(\mu(k)\)慌的一批 退了两步发现只要求个欧拉函数就行了 \(ans = \sum_{d | n} d \phi(\frac{n}{d})\) 理 ...
- [SDOI2012] Longge的问题 - 欧拉函数
求 \(\sum\limits_{i=1}^{n}gcd(i,n)\) Solution 化简为 \(\sum\limits_{i|n}^{n}φ(\dfrac{n}{i})i\) 筛出欧拉函数暴力求 ...
- UVa 10837 A Research Problem 欧拉函数
题意: 给你一个欧拉函数值 phi(n),问最小的n是多少. phi(n) <= 100000000 , n <= 200000000 解题思路: 对于欧拉函数值可以写成 这里的k有可能是 ...
- Bzoj-2705 Longge的问题 欧拉函数
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2705 题意: 求 sigma(gcd(i,n), 1<=i<=n<2^3 ...
随机推荐
- Mysql--产品支持的平台
- Eclipse免reload设置
在使用eclipse开发web项目的时候,有的时候需要通过运行看结果,但是如果项目很大,你改一句代码,也许就要重启一次,或者eclipse自动帮你reload,这样严重的影响开发的效率,这里只要修改一 ...
- VirtualBox从USB设备(PE)启动图文教程
很多朋友都为VBox无法进入U盘启动而苦恼,别懊恼,魔笛这就为你讲解 方法/步骤 1 大家先把U盘插入实体电脑. 单击“确定”打开“磁盘管理”, 键盘上的Win+R组合键,输入diskmgnt.. ...
- Python if判断语句
a=input('输入你的用户名:') if a == "lilei": print('李磊,等你好久了') elif a == "wanghui": prin ...
- 1、redis安装与启动
1.安装包下载 官网上下载:http://www.redis.io/ 安装版本:3.0.7 安装环境:CentOS 下载命令:wget http://download.redis.io/release ...
- Linux监控和安全运维 1.9 zabbix增加客户端监控
1.在客户端安装 zabbix20与服务器相对应. yum install zabbix20-agent 2.修改配置文件 vim /etc/zabbix_agentd.conf Server=127 ...
- 引用rtmp编译报错:rtmp.obj : error LNK2001: 无法解析的外部符号 __imp__timeGetTime@0
如题vs下引用librtmp的时候报错:rtmp.obj : error LNK2001: 无法解析的外部符号 __imp__timeGetTime@0 在link 里加入 winmm.lib 就可以 ...
- AOP的MethodBeforeAdvice
使用Spring自动生成代理类,spring利用的是动态代理机制 接口 Java代码 public interface UserDao { void addUser(); void deleteUse ...
- 解决MySql 数据库 提示:1045 access denied for user 'root'@'localhost' using password yes
今天想用用MySQL 数据库 谁知道老提示 1045 access denied for user 'root'@'localhost' using password yes 最后在csdn 上找到 ...
- elasticsearch must和should组合查询
{"query": { "bool": { "should": [ {"bool": { "must" ...