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<N
∑
i=1
j
∑
≤N
j=i+1
GCD(i, j)
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
10
100
200000
0
Sample Output
67
13015
143295493160
题意:给出n,求∑(i!=j) gcd(i,j) (1<=i,j<=n)
题解:s(n)=s(n-1)+gcd(1,n)+gcd(2,n)+……+gcd(n-1,n);
设f(n)=gcd(1,n)+gcd(2,n)+……+gcd(n-1,n)。
gcd(x,n)=i是n的约数(x<n),按照这个约数进行分类。设满足gcd(x,n)=i的约束有g(n,i)个,则有f(n)=sum(i*g(n,i))。
而gcd(x,n)=i等价于gcd(x/i,n/i)=1,因此g(n,i)等价于phi(n/i).phi(x)为欧拉函数。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll; const int N=+; ll phi[N+] , f[N+];
void phi_table() {
for(int i = ;i <= N; i++) phi[i] = ;
phi[] = ;
for(int i = ; i <= N; i++) {
if(!phi[i]) {
for(int j = i; j <= N; j += i) {
if(!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i-);
}
}
}
}
ll s[N+],n;
int main() {
phi_table();
for(int i = ; i <= N; i++) {
for(int j = i + i; j <= N; j += i) {
f[j] += i * phi[j / i];
}
}
for(int i = ; i <= N; i++) s[i] = s[i-] + f[i];
while(~scanf("%lld",&n)) {
if(!n) break;
printf("%lld\n", s[n]);
}
return ;
}
代码
UVA 11426 - GCD - Extreme (II) 欧拉函数-数学的更多相关文章
- UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...
- 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 ...
- 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) 值为 ...
- UVA 11426 GCD - Extreme (II) 欧拉函数
分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include ...
- 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) 此 ...
- UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)
UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...
- UVA11426 GCD - Extreme (II)---欧拉函数的运用
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA11426 GCD - Extreme (II) —— 欧拉函数
题目链接:https://vjudge.net/problem/UVA-11426 题意: 求 ∑ gcd(i,j),其中 1<=i<j<=n . 题解:1. 欧拉函数的定义:满足 ...
- UVA 11426 - GCD - Extreme (II) (数论)
UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...
随机推荐
- Java Break和continue实现goto功能
continue实验 1 public class test { static int i =0; public static void main(String[] args) { lable1: w ...
- ubuntu-设置分辨率
xrandr -s 1440x900 -r 60 前提是,分辨率选项中有对应的设置选项.
- WPF黑色背景下常用控件样式
平时工作用 自己整理的 代码等找到合适的上传空间在进行同步
- Dapper中数据表的字段(列)与实体属性不一致时,如何手动配置它们之间的映射?
NET[C#]Dapper中数据表的字段(列)与实体属性不一致时,如何手动配置它们之间的映射? 问题描述 比如有如下的数据表结构:Person: person_id int first_name va ...
- <a>和<table>标签的应用
今天介绍一下html中最重要的标签 标签分为 1.一般标签 如<img> <b></b>等 2.功能标签 如<a></a> 3.实体 如&a ...
- (转载)activity外部调用startActivity的new task异常解析
activity外部调用startActivity的new task异常解析 泡在网上的日子 / 文 发表于2013-09-07 12:45 第1314次阅读 异常,android,activity ...
- JavaScript私有方法
some concepts: Java is from Sun Microsystem Inc., and JavaScript, called LiveScript before, is from ...
- CSS字体代码
宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 Ming ...
- 洛谷P1120 小木棍 [数据加强版]搜索
玄学剪支,正好复习一下搜索 感觉搜索题的套路就是先把整体框架打出来,然后再一步一步优化剪枝 1.从maxv到sumv/2枚举长度(想一想,为什么) 2. 开一个桶,从大到小开始枚举 3. 在搜索中,枚 ...
- HTML5新特性之文件和二进制数据的操作 Blob对象
HTML5新特性之文件和二进制数据的操作 1.Blob对象 2.FileList对象 3.File对象 4.FileReader 对象 5.URL对象