转载请注明出处: 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) (欧拉函数)的更多相关文章

  1. UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...

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

  3. 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) 值为 ...

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

  5. UVA 11426 GCD - Extreme (II) 欧拉函数

    分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include ...

  6. 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) 此 ...

  7. UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)

    UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...

  8. UVA11426 GCD - Extreme (II)---欧拉函数的运用

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA11426 GCD - Extreme (II) —— 欧拉函数

    题目链接:https://vjudge.net/problem/UVA-11426 题意: 求 ∑ gcd(i,j),其中 1<=i<j<=n . 题解:1. 欧拉函数的定义:满足 ...

  10. UVA 11426 - GCD - Extreme (II) (数论)

    UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...

随机推荐

  1. hdu 1263 水果

    Problem Description 夏天来了~~好开心啊,呵呵,好多好多水果~~ Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样J ...

  2. 最小生成树prim算法———模板

    codevs.cn 最优布线问题 #include<cstdio>#include<cstring> bool u[101]; int g[101][101],minn[101 ...

  3. C++Primer笔记(3)

    标准库类型string表示可变长的字符序列,使用前先包含string头文件.(哈哈,终于可以逃脱C语言中的str函数系列了.)因为是标准库的一部分,所以string被定义在命名空间std中.所以你懂该 ...

  4. php json_encode数据格式化2种格式[]和{}

    在php中,json格式化数据后,会出现2种形式数据: 1.当array是一个从0开始的连续数组时,json_encode的结果是一个由[]括起来的字符串 $arr = array('a' , 'b' ...

  5. php开启伪静态(2转)

    打开apache的配置文件httpd.conf找到#LoadModule rewrite_module modules/mod_rewrite.so把前面#去掉.没有则添加,但必选独占一行,使apac ...

  6. 【ROC曲线】关于ROC曲线、PR曲线对于不平衡样本的不敏感性分析说引发的思考

    ROC曲线 在网上有很多地方都有说ROC曲线对于正负样本比例不敏感,即正负样本比例的变化不会改变ROC曲线.但是对于PR曲线就不一样了.PR曲线会随着正负样本比例的变化而变化.但是没有一个有十分具体和 ...

  7. 去掉java反编译(JD-GUI)生成的源文件中注释

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  8. HashMap非线程安全分析

    通过各方资料了解,HashMap不是线程安全的,但是为什么不是线程安全的,在什么情况下会出现问题呢? 1. 下面对HashMap做一个实验,两个线程,并发写入不同的值,key和value相同,最后再看 ...

  9. POJ_Fibonacci POJ_3070(矩阵快速幂入门题,附上自己写的矩阵模板)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10521   Accepted: 7477 Descri ...

  10. 【POJ3299】Humidex(简单的数学推导)

    公式题中已经给出,直接求解即可. #include <iostream> #include <cstdlib> #include <cstdio> #include ...