转载请注明出处: 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. AngularJS中ng-options简单用法及预选项失败的原因

    刚刚接触AngularJs,记录一下ng-options的使用. 1.构造key-value数据 $scope.types = [ {id:"1",type:"AA&qu ...

  2. dynamic和object浅谈

    要想知道dynamic和object的关系必须先理解它们的含义 C# 4.0提供了一个dynamic 关键字.在MSDN里是这样描述:在通过 dynamic 类型实现的操作中,该类型的作用是绕过编译时 ...

  3. centos mysql 数据存储目录安装位置

    rpm -ql mysql查看安装位置 MYSQL默认的数据文件存储目录为/var/lib/mysql.假如要把目录移到/home/data下需要进行下面几步: 1.home目录下建立data目录 c ...

  4. 安装python3.4

    1.http://www.python.org下载适合自己机型的镜像文件 2.一路“next”到底,安装python到C盘上 3.计算机-属性-高级系统设置-环境变量,将刚刚安装的python路径添加 ...

  5. jquery实现当前页面编辑

    实现效果 代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...

  6. js与webview 常用交互代码

    常用js交互 css常用参数::: 是否允许用户选择元素的内容,选择值包括:     auto:用户可以选择元素内的内容     none:用户不能选择任何内容     text:用户只能选择元素内的 ...

  7. LeetCode_Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...

  8. UDP数据接收服务器

    简介 这是我在做一个要用UDP方式进行数据传输时,自己写的一个多线程的UDP数据接收服务器, 它能将接收到的UDP数据包存成文件,并提供数据包接收时间监测: 还支持键盘命令响应,以将数据写到新的文件, ...

  9. grok 添加字段

    filter { grok { match =>[ "message","%{IPORHOST:clientip} \[%{HTTPDATE:time}\] \&q ...

  10. 匹配“is outside location”

    <pre name="code" class="html">is outside location 怎么匹配? . 匹配除换行外的所有单个字符,通常 ...