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. PG覆盖率检查

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

  2. vconsole使用

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  3. Vim插件之ale,LeaderF,completor.vim(win10)配置

    内容包含 vim-plug,异步插件管理,总之就是下起来快. ale,异步语法检查 LeaderF,快速查找文件 completor.vim vim8的快速补全 markdown预览 common s ...

  4. springboot 中 集成druid ,redis

    1,导入druid jar包 <!--引入drud--> <dependency> <groupId>com.alibaba</groupId> < ...

  5. bootstrop-datatime参数配置

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  6. Linux下patch的制作和应用

    转自:http://blog.chinaunix.net/u3/100239/showart_1984963.html 首先介绍一下diff和patch.在这里不会把man在线文档上所有的选项都介绍一 ...

  7. 使用Log4J监控系统日志邮件警报

    使用Log4J监控系统日志邮件警报 前言 在系统上线后,有时候遇到系统故障,这时候就可以登录服务器查看系统日志来排查问题.但是需要登录服务器,下载查找相关异常日志比较麻烦.而且没有监控的话,也无法实时 ...

  8. JavaScript RegExp.exec() 方法

    定义和用法: exec() 方法用于检索字符串中的正则表达式的匹配. 语法: RegExpObject.exec(string); RegExpObject:必须参数,正则表达式: string:必须 ...

  9. cakephp执行原生sql语句

    $sql = 'select sum(amount) as amount from option_capital where status = 2 and amount > 0 and user ...

  10. Java 设计模式系列(十五)迭代器模式(Iterator)

    Java 设计模式系列(十五)迭代器模式(Iterator) 迭代器模式又叫游标(Cursor)模式,是对象的行为模式.迭代子模式可以顺序地访问一个聚集中的元素而不必暴露聚集的内部表象(interna ...