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. 接口自动化(六)--使用QQ邮箱发送邮件

    接口测试执行完发送一个邮件,这里使用QQ邮箱发送,先要拿到QQ邮箱授权码,方法自行百度 # coding=utf-8 import smtplib from email.mime.text impor ...

  2. PG覆盖率检查

    覆盖率检查 需要gcov和lcov工具,gcov在gcc中自带,lcov需要自行下载安装 重新编译 ./configure --prefix=`pwd`/install --with-perl --w ...

  3. linux中grep工具

    正则表达式 以前我们用grep在一个文件中找出包含某些字符串的行,比如在头文件中找出一个宏定义.其实grep还可以找出符合某个模式(Pattern)的一类字符串.例如找出所有符合xxxxx@xxxx. ...

  4. uva-11044-水题

    #include <iostream> #include<memory.h> #include<stdio.h> using namespace std; int ...

  5. MS SQL 2000 分配权限

    /** 分配权限 **/ use [master]create login [ln-tf\liaobin] from windows;gogrant control server to [ln-tf\ ...

  6. ATL控件签名之后页面还提示“在此页面上的Activex控件和本页上的其他部分及交互可能不安全”

    ATL控件正常签名打包,然后安装之后还是会提示: 没理由啊,签名是花钱搞得正当的签名.后来查了资料才知道这还不够,需要在创建ATL控件的时候继承一个IObjectSafetyImpl 类 知道了原因, ...

  7. Jenkins使用FTP进行一键部署及回滚2(Windows)(项目实践)

     转载:http://www.cnblogs.com/EasonJim/p/6295372.html Jenkins使用FTP进行一键部署及回滚2(Windows)(项目实践) 前提: 这一篇是继上一 ...

  8. springMVC入门程序。使用springmvc实现商品列表的展示。

    1.1 开发环境 本教程使用环境: Jdk:jdk1.7.0_72 Eclipse:mars Tomcat:apache-tomcat-7.0.53 Springmvc:4.1.3 1.2 需求 使用 ...

  9. Spring JMX之一:使用JMX管理Spring Bean

    spring中关于jmx包括几个概念: MBeanExporter: 从字面上很容易理解, 用来将一些spring的bean作为MBean暴露给MBEanServer.MBeanServerFacto ...

  10. 邹欣,现代软件工程讲义:单元测试&回归测试

    http://www.cnblogs.com/xinz/archive/2011/11/20/2255830.html 邹欣, 现代软件工程讲义 2 开发技术 - 单元测试 & 回归测试