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. 关于mysql 间隙锁

    前段时间系统老是出现update死锁,很是纠结.经过排查发现是间隙锁!间隙锁是innodb中行锁的一种, 但是这种锁锁住的却不止一行数据,他锁住的是多行,是一个数据范围.间隙锁的主要作用是为了防止出现 ...

  2. python beautifulsoup爬虫

    爬虫这个听起来很 hack 的名字,是我学习 python 的诱因.当 python 基础学习到一定程度(基本语法,数据类型掌握) 就可以开启自己的小爬虫了.毕竟实践才是提高的最快途径.废话说完了,下 ...

  3. 错误:HttpServlet was not found on the Java

    我们在用Eclipse进行Java web开发时,可能会出现这样的错误:The superclass javax.servlet.http.HttpServlet was not found on t ...

  4. IOS 上架要求视频及屏幕截屏

    客户提供上架的资料 1.IOS 上架要求视频演示,录制一段视频,上传到优酷,需要url连接. 2.手机截屏,每个尺寸5张.5s/6/6p *5=15张.截屏图片分辨率. iPhone4s手机 3.5I ...

  5. LUA table.sort的问题,数组与表的区别

    t = { [] = , [] = , [] = , [] = , } t1 = { , , , , } t2 = { 'a', 'b','d','c', } function cmp(v1, v2) ...

  6. centos 使用windows7 存储

    1. 在Windows7上创建一个带密码的用户,如disk 2. 创建一个文件夹,如 D:\centos-disk2 3. 选中此文件夹,点击上方的  共享 -> 特定用户, 添加disk用户, ...

  7. python之daemon线程

    [python之daemon线程] A thread can be flagged as a “daemon thread”. The significance of this flag is tha ...

  8. Android给ListView添加一个入场动画

    动画是一个App体现良好交互的一种手段,通常的我们会看到很多App的ListView的Item都有一个入场动画例如: 可以看到,当进入界面加载ListView的Item的时候有一个向左滑动显示,并且淡 ...

  9. for 续6

    ---------siwuxie095             for 实际运用样例(/f 的使用不列出来):     for %%i in (*) do echo %%i 显示当前目录下 ,所有非文 ...

  10. 9-eclispe中右键BuildPath没有了

    Eclipse 右上角的代码视图,选择Java就好了!