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=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 ?le 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 ?t 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=4000000+10;

ll phi[N+5] , f[N+5];
void phi_table() {
  for(int i = 2;i <= N; i++) phi[i] = 0;
  phi[1] = 1;
  for(int i = 2; 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-1);
        }
    }
  }
}
ll s[N+5],n;
int main() {
    phi_table();
    for(int i = 1; i <= N; i++) {
        for(int j = i + i; j <= N; j += i) {
            f[j] += i * phi[j / i];
        }
    }
    for(int i = 1; i <= N; i++) s[i] = s[i-1] + f[i];
    while(~scanf("%lld",&n)) {
        if(!n) break;
        printf("%lld\n", s[n]);
    }
    return 0;
}

代码

时间: 2024-08-24 10:39:13

UVA 11426 - GCD - Extreme (II) 欧拉函数-数学的相关文章

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;

UVA11426 GCD - Extreme (II)---欧拉函数的运用

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=473&problem=2421&mosmsg=Submission+received+with+ID+13800900 Given the value of N, you will have to ?nd the value of G. The de?nition

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)

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) (数论|欧拉函数)

题意:求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) (欧拉函数)

题意:,给定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 10837 - A Research Problem(欧拉函数)

UVA 10837 - A Research Problem 题目链接 题意:给定phi(n),求最小满足的最小的n 思路:phi(n)=pk11(p1?1)?pk22(p2?1)?pk33(p3?1)....(p为质数),因此对于给定phi(n),先把满足条件phi(n)%(p?1)=0的素数全找出来,在这些素数基础上进行暴力搜索,枚举哪些素数用与不用,求出最小值.这样做看似时间复杂度很高,但是实际上,由于每次多选一个素数之后对于值是呈指数上升的,所以实际组合出来的情况并不会太多,因此是可行的

uva 10837 - A Research Problem(欧拉函数+暴力)

题目链接:uva 10837 - A Research Problem 题目大意:给定一个phin,要求一个最小的n,欧拉函数n等于phin 解题思路:欧拉函数性质有,p为素数的话有phip=p?1;如果p和q互质的话有phip?q=phip?phiq 然后根据这样的性质,n=pk11(p1?1)?pk22(p2?1)???pkii(pi?1),将所有的pi处理出来,暴力搜索维护最小值,虽然看上去复杂度非常高,但是因为对于垒乘来说,增长非常快,所以搜索范围大大被缩小了. #include <cs