UVA 11426 GCD - Extreme (II) (欧拉函数)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Problem J
GCD Extreme (II)
Input: Standard Input
Output: Standard Output
Given the value of N, you will have to find the value of G. The definition of G is given below:

Here GCD(i,j) means the greatest common divisor of integer i and integer j.
For those who have trouble understanding summation notation, the meaning of G is given in the following code:
|
G=0; for(i=1;i<N;i++) for(j=i+1;j<=N;j++) { G+=gcd(i,j); } /*Here gcd() is a function that finds the greatest common divisor of the two input numbers*/ |
Input
The
input file contains at most 100 lines of inputs. Each line contains an
integer N (1<N<4000001). The meaning of N is given in the problem
statement. Input is terminated by a line containing a single zero.
Output
For
each line of input produce one line of output. This line contains the
value of G for the corresponding N. The value of G will fit in a 64-bit
signed integer.
Sample Input Output for Sample Input
|
10 100 200000 0
|
67 13015 143295493160
|
Problemsetter: Shahriar Manzoor
Special Thanks: SyedMonowarHossain
设dp[i]=gcd(1,i)+gcd(2,i)+……+gcd(i-1,i);
则ans[n]=dp[2]+dp[3]+……+dp[n].
由此问题已经转化成如何求dp[i]了,即需要求1到i-1所有数与i的gcd的和。
设k为满足gcd(x,i)=j且x<i的正整数的个数,则dp[i]=∑j*k;
同时,由于gcd(x,i)=j等价于gcd(x/j,i/j)=1,也就是phi[i/j];
接下来反过来求,那就不需要分解素因子了
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn=;
int phi[maxn];
ll dp[maxn+];
ll ans[maxn+];
void phi_table()
{
phi[]=;
for(int i=;i<maxn;i++)
{
if(!phi[i])
{
for(int j=i;j<maxn;j+=i)
{
if(!phi[j])phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
phi_table();
for(int i=;i<maxn;i++)
{
for(int j=i*;j<maxn;j+=i)dp[j]+=(long long)i*(long long)phi[j/i];
}
ans[]=dp[];
for(int i=;i<maxn;i++)ans[i]=ans[i-]+dp[i];
int n;
while(cin>>n&&n)
{
cout<<ans[n]<<endl;
}
return ;
}
UVA 11426 GCD - Extreme (II) (欧拉函数)的更多相关文章
- UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...
- UVA 11426 GCD - Extreme (II)(欧拉函数打表 + 规律)
Given the value of N, you will have to find the value of G. The definition of G is given below:Here ...
- uva 11426 GCD - Extreme (II) (欧拉函数打表)
题意:给一个N,和公式 求G(N). 分析:设F(N)= gcd(1,N)+gcd(2,N)+...gcd(N-1,N).则 G(N ) = G(N-1) + F(N). 设满足gcd(x,N) 值为 ...
- UVA 11426 - GCD - Extreme (II) 欧拉函数-数学
Given the value of N, you will have to find the value of G. The definition of G is given below:G =i< ...
- UVA 11426 GCD - Extreme (II) 欧拉函数
分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include ...
- UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)
题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...
- UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)
UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...
- UVA11426 GCD - Extreme (II)---欧拉函数的运用
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA11426 GCD - Extreme (II) —— 欧拉函数
题目链接:https://vjudge.net/problem/UVA-11426 题意: 求 ∑ gcd(i,j),其中 1<=i<j<=n . 题解:1. 欧拉函数的定义:满足 ...
- UVA 11426 - GCD - Extreme (II) (数论)
UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...
随机推荐
- POJ 1001 Exponentiation 模拟小数幂
模拟小数幂 小数点位 pos 非零末位 e 长度 len 只有三种情况 pos > len pos < e e < pos < len #include <iostrea ...
- SQL学习笔记——SQL中的数据查询语句汇总
where条件表达式 --统计函数 Select count(1) from student; --like模糊查询 --统计班上姓张的人数 select count(*) from student ...
- PHP & JAVA 实现 PBKDF2 加密算法
PHP代码: /** * PBKDF2 加密函数 * 参考标准 * @link https://www.ietf.org/rfc/rfc2898.txt * * php官方函数将在php5.5发布 * ...
- 文件系统:介绍一个高大上的东西 - 零基础入门学习Python030
文件系统:介绍一个高大上的东西 让编程改变世界 Change the world by program 接下来我们会介绍跟Python的文件相关的一些十分有用的模块.模块是什么?不知大家对以下代码还有 ...
- 微软企业库的Cache
微软企业库的Cache 通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能.基于微软的企业库,我们的快速创建一个缓存的实现. 新建PrismSamp ...
- Effective Java2读书笔记-类和接口(五)
第21条:用函数对象表示策略 这一条其实也没说啥,就是策略模式.碰到这种场景时,定义一个策略接口,然后不同策略子类实现它,主类包含这个接口的引用就可以了. 第22条:优先考虑静态成员类 嵌套类是指被定 ...
- DJANGO用户名认证一例
现在实例了用户登陆,就自带的功能.. urls.py ~~~~~~~~~~ (r'^login/$', login), (r'^logout/$',logout,{'next_page':'/logi ...
- USB 各型插座插头引脚分布
最近画USB的电路,老是忘记它的引脚排列,每次都要去翻手册,很麻烦,索性整理了一下,以后用着也方便,这些图都来自USB标准上. 注:以下均为插座或插头的前视图,即将插座或插头面向自己. USB A型插 ...
- Windows 8.1 Update1 6610 32位/64位下载、安装和新增功能简评
今天,微软已经确认完成Windows 8.1 2014 Update RTM正式版的开发工作,累计修复99%的已知bug.随后,微软会将Win8.1首个春季更新正式版,即Win8.1 2014 Upd ...
- Windows文件居然有解锁一说,并且还会引起SignTool Error,真是昏倒!
I'm running Windows 7 and when I try to run a batch file, it says, "The publisher could not be ...