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

UVA 11426 - GCD - Extreme (II)

题目链接

题意:给定N,求∑i<=ni=1∑j<nj=1gcd(i,j)的值。

思路:lrj白书上的例题,设f(n) = gcd(1, n) + gcd(2, n) + ... + gcd(n - 1, n).这样的话,就可以得到递推式S(n) = f(2) + f(3) + ... + f(n) ==> S(n) = S(n - 1) + f(n);.

这样问题变成如何求f(n).设g(n, i),表示满足gcd(x, n) = i的个数,这样f(n) = sum{i * g(n, i)}. 那么问题又转化为怎么求g(n, i),gcd(x, n) = i满足的条件为gcd(x / i, n / i) = 1,因此只要求出欧拉函数phi(n / i),就可以得到与x / i互质的个数,从而求出gcd(x , n) = i的个数,这样整体就可以求解了

代码:

#include <stdio.h>
#include <string.h>

const int N = 4000005;

int n;
long long phi[N], s[N], f[N];

int main() {
	phi[1] = 1;
	for (int i = 2; i < N; i++) {
		if (phi[i]) continue;
  		for (int j = i; j < N; j += i) {
  			if (!phi[j]) phi[j] = j;
  			phi[j] = phi[j] / i * (i - 1);
  		}
 	}
 	for (int i = 1; i < N; i++) {
 		for (int j = i * 2; j < N; j += i) {
 			f[j] += phi[j / i] * i;
		}
  	}
  	s[2] = f[2];
  	for (int i = 3; i < N; i++)
  		s[i] = s[i - 1] + f[i];
	while (~scanf("%d", &n) && n) {
		printf("%lld\n", s[n]);
 	}
	return 0;
}

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

时间: 2024-08-25 09:43:02

UVA 11426 - GCD - Extreme (II) (数论)的相关文章

UVA 11426 GCD - Extreme (II) (数论|欧拉函数)

题意:求sum(gcd(i,j),1<=i<j<=n). 思路:首先可以看出可以递推求出ans[n],因为ans[n-1]+f(n),其中f(n)表示小于n的数与n的gcd之和 问题转化为了求f(n),因为小于n的数与n的gcd一定是n的因数, 所以f(n)可以表示为sum(i)*i,其中sum(i)表示所有和n的gcd为i的数的数量,我们要求满足gcd(a, n) = i,的个数,可以转化为求gcd(a/i, n/i) = 1的个数, 于是可以发现sun(i) = phi(n/i),这

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

Given the value of N, you will have to ?nd the value of G. The de?nition of G is given below:G =i<N∑i=1j∑≤Nj=i+1GCD(i, j)Here GCD(i, j) means the greatest common divisor of integer i and integer j.For those who have trouble understanding summation no

UVA 11426 GCD - Extreme (II)

题目大意: 求出 我们可以通过求∑(1<=i<=N)∑(1<=j<=N)gcd(i,j) 然后减去 i , j相同的情况,最后因为 i , j 互换取了两次所以除以2 上述式子等于 ∑(1<=i<=N)∑(1<=j<=N)∑(d|gcd(i,j))phi[d]     phi[d]  是欧拉函数 ∑phi[d]∑∑(1<=i<=N/d)∑(1<=j<=N/d) #include <cstdio> #include <

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

题意:,给定N,求G. 分析: 1.G = f(2) + f(3) + ... + f(n).其中,f(n) = gcd(1, n) + gcd(2, n) + ... + gcd(n - 1, n). 2.设g(n, i)表示gcd(x, n) = i的个数(x < n),则f(n) = sum{i * g(n, i)}. 3.g(n, i)的求法: (1)因为gcd(x, n) = i,可得gcd(x / i, n / i) = 1,且x / i < n / i. (2)因为gcd(x /

UVa 11426 GCD - Extreme (II) (欧拉函数应用&#183;O(N*logN))

题意  令  G(n) = sum{gcd(i, j) | 0 < i < n, i < j <= n}  给你一个n  输出G(n) 令 F(n) = sum{gcd(i, n) | 0 < i < n}  那么有递推式 G(n) = G(n-1) + F(n) , G(0)  = 0  也就是说只用求出F(n) 就能递推求出 G(n)了  而求F(n)就比较容易了 对于i  设 x < i , gcd(x,i) = 1 即x, n 互质 则  gcd(2*x,

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

分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include <vector> #include <cmath> #include <map> #include <queue> #include <algorithm> #include <cstring> using namespace std;

UVA 11426 gcd求和

UVA 11426 gcd求和 O - GCD - Extreme (II) Time Limit:10000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 11426 Description Problem JGCD Extreme (II)Input: Standard Input Output: Standard Output Given the value of N, y

【UVa11426】GCD - Extreme (II)(莫比乌斯反演)

[UVa11426]GCD - Extreme (II)(莫比乌斯反演) 题面 Vjudge 题解 这.. 直接套路的莫比乌斯反演 我连式子都不想写了 默认推到这里把.. 然后把\(ans\)写一下 \[ans=\sum_{d=1}^nd\sum_{i=1}^{n/d}\mu(i)[\frac{n}{id}]^2\] 令\(T=id\) 然后把\(T\)提出来 \[ans=\sum_{T=1}^n[\frac{n}{T}]^2\sum_{d|T}d\mu(\frac{T}{d})\] 后面那一堆

UVa 11426 (欧拉函数 GCD之和) GCD - Extreme (II)

题意: 求sum{gcd(i, j) | 1 ≤ i < j ≤ n} 分析: 有这样一个很有用的结论:gcd(x, n) = i的充要条件是gcd(x/i, n/i) = 1,因此满足条件的x有phi(n/i)个,其中Phi为欧拉函数. 所以枚举i和i的倍数n,累加i * phi(n/i)即可. 1 #include <cstdio> 2 typedef long long LL; 3 4 const int maxn = 4000000; 5 6 int phi[maxn + 10]