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);
}(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

10

100

200000

0

Sample Output

67

13015

143295493160

题目大意:就是求G[n]

 
G[n] = gcd(1,2) + gcd(1,3) + ... + gcd(1,n-1) + gcd(1,n)
         +gcd(2,3) + gcd(2,4) + ... + gcd(2,n-1) + gcd(2,n)
         +...
         +gcd(n-2,n)+ gcd(n-1,n);
 
这样我们可以得到一个规律:
 
G[n] = G[n - 1] + b[n]( 其中b[n]就是上面红色部分的和b[n] = gcd(1,n) + gcd(2,n) + ... + gcd(n-1,n)  )
 
b[n] = gcd(1,n) + gcd(2,n) + ... + gcd(n-1,n) 
b[n]的加数我们可以统另为gcd(b, n);
 
设a[xi] 表示使gcd(b,n) = xi成立的b的个数
那么b[n] = a[xi]*xi = a[x1]*x1 + a[x2]*x2 + a[x3]*x3 +...+a[xs]*xs
我们就要求a[xi]
 
gcd(b,n) = xi可以转化为求有多少个b/xi使gcd(b/xi,n/xi)=xi/xi = 1,要求b/xi的个数就相当于求与n/xi互质的数有多少个(即求n/xi的欧拉函数)因为只有互质的两个数气最大公约数才是1
那么我们最终求得就是b[n] = a[n/xi] *1 * xi (i = 1,2,3,...)
 
 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm> using namespace std; const int N = ;
typedef long long ll; ll a[N], b[N], G[N]; int main()
{
ll n;
for(ll i = ; i < N ; i++)
a[i] = i;//初始化
a[] = ;
for(ll i = ; i < N ; i++)
{
if(a[i] == i)
{
a[i] -= a[i] / i;
for(ll j = i * ; j < N ; j += i)
a[j] -= a[j] / i;
}
}//欧拉函数打表
for(ll i = ; i < N ; i++)
{
for(ll j = i * ; j < N ; j += i)
b[j] += a[j / i] * i;
}//b[n]打表(这里b的下标应含因子i)
G[] = ;
for(ll i = ; i < N ; i++)
G[i] = G[i - ] + b[i];//G[n]打表
while(scanf("%lld", &n), n)
{
printf("%lld\n", G[n]);
}
return ;
}
 
 

UVA 11426 GCD - Extreme (II)(欧拉函数打表 + 规律)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. springboot 测试 有注入HttpSession的bean

    question: nested exception is java.lang.IllegalStateException: No thread-bound request found: Are yo ...

  2. YII cookie和session的使用方法

    设置cookie://首先新建cookie$cookie = new CHttpCookie(‘mycookie’, ‘this is my cookie’);//定义cookie的有效期$cooki ...

  3. JS 位数不够自动左补0

    var mycode = "11"; mycode = (Array(4).join(0) + parseInt(mycode)).slice(-4);//0011 mycode1 ...

  4. 基于HALCON的双目立体视觉系统实现

    双目立体视觉是机器视觉的一种重要形式,它是基于视差原理并由多幅图像获取物体三维几何信息的方法.双目立体视觉系统一般由双摄像机从不同角度同时获得被测物的两幅数字图像,或由单摄像机在不同时刻从不同角度获得 ...

  5. 取出资源文件中的bitmap,并将其保存到TMemoryStream中,从资源里载入图象而不丢失调色板

    从资源里载入图象而不丢失调色板 procedure loadgraphic(naam:string);var  { I've moved these in here, so they exist on ...

  6. Graphics.BlitMultiTap解析

    [Graphics.BlitMultiTap解析] 上述代码的四个偏移,表示利用此4个偏移,生成4张纹理单位.下面每一个SetTexture,默认会调用一个纹理单位. 而在Shader中,Unity会 ...

  7. 在制MO未取到FP

    原因:今天在制MO未取进去原因为业务人员维护验货客户尾缀时维护ZZ导致,需更新为C开头即可 SELECT * FROM IN_SFCHEADER WHERE MO_ID in('00110051832 ...

  8. android在开发过程中的数据库存储位置

    1

  9. Axure 原型图 (转)

    Axure RP是很有名的一个界面原型设计工具,可以灵活快捷的对C/S.B/S程序设计原型. 近期我要开发一个Android客户端,也打算使用Axure RP设计原型. 下载地址:http://pan ...

  10. C语言文件实现学生成绩管理

    C语言实现学生成绩管理 项目简介 用C语言的链表及文件操作实现学生成绩的管理,实现主要的添加.修改.删除.查询的主要功能,并在程序关闭时将数据存储在二进制的文件中并加密.下一次打开程序,先解密二进制文 ...