fzu1969 GCD Extreme 类似于uva10561

Description

Given the value of N, you will have to find the value of G. The meaning of G is given in the following code

G=0; 
for(i=1;i&ltN;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 20000 lines of inputs. Each line contains an integer N (1<N <1000001). 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

Source

Contest for 2010 lecture II

题意:给出数字n,求对于所有满足1<= i < j <= n 的所有数对,(i,j)所对应的gcd(i,j)之和。

思路:设f(n) = gcd(1,n)+gcd(2,n)+gcd(3,n)+。。。+gcd(n-1,n)。则所求答案s(n)=f(2)+f(3)+f(4)+。。。+f(n)。

注意到所有的gcd(x,n)的值都是n的约数,所以可以按照这个约数来进行分类。用g(n,i)来表示满足gcd(x,n)=i且x<n的正整数x的个数。则f(n)={i×g(n,i)|i为n的约数}。注意,g(n,i)=phi(n,i)。

如果对于每个n都枚举i,按照数据范围来看肯定会超时,我们不如反着来,先枚举i,再找i的倍数n。这样时间复杂度会进一步减少。

 1 /*
 2  * Author:  Joshua
 3  * Created Time:  2014年09月07日 星期日 19时26分30秒
 4  * File Name: fzu1969.cpp
 5  */
 6 #include<cstdio>
 7 #include<cstring>
 8 typedef long long LL;
 9 #define maxn 1000005
10 int phi[maxn],a[maxn],f[maxn];
11 LL s[maxn];
12 bool p[maxn];
13 int tot=0;
14
15 void pri()
16 {
17     memset(p,1,sizeof(p));
18     for (int i=2;i<maxn;++i)
19         if (p[i])
20         {
21             for (int j=i<<1;j<maxn;j+=i)
22                 p[j]=false;
23         }
24     for (int i=2;i<maxn;++i)
25         if (p[i]) a[++tot]=i;
26 }
27
28 void phi_table()
29 {
30     for (int i=2;i<maxn;++i)
31     {
32         phi[i]=i;
33         int temp=i;
34         for (int j=1;j<=tot;++j)
35         {
36             if (temp==1) break;
37             if (p[temp])
38             {
39                 phi[i]/=temp;
40                 phi[i]*=temp-1;
41                 break;
42             }
43             if (temp % a[j] == 0)
44             {
45                 phi[i]/=a[j];
46                 phi[i]*=a[j]-1;
47                 while (temp%a[j]==0) temp/=a[j];
48             }
49         }
50     }
51 }
52
53 void solve()
54 {
55     pri();
56     phi_table();
57     for (int i=1;i<maxn;++i)
58         for (int j=i;j<maxn;j+=i)
59             f[j]+=i*phi[j/i];
60     for (int i=1;i<maxn;++i)
61         s[i]+=s[i-1]+f[i];
62 }
63
64 int main()
65 {
66     int n;
67     solve();
68     while (scanf("%d",&n) && n)
69         printf("%lld\n",s[n]);
70     return 0;
71 }
时间: 2024-10-16 04:33:21

fzu1969 GCD Extreme 类似于uva10561的相关文章

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)

spoj 3871. GCD Extreme 欧拉+积性函数

3871. GCD Extreme Problem code: GCDEX Given the value of N, you will have to find the value of G. The meaning of G is given in the following code G=0; for(k=i;k< N;k++) for(j=i+1;j<=N;j++) { G+=gcd(k,j); } /*Here gcd() is a function that finds the g

spoj 3871 gcd extreme

1 题目大意给出一个n,求sum(gcd(i,j),0<i<j<=n); 2 可以明显的看出来s[n]=s[n-1]+f[n]; 3 f[n]=sum(gcd(i,n),0<i<n); 4 现在麻烦的是求f[n] 5 gcd(x,n)的值都是n的约数,则f[n]= 6 sum{i*g(n,i),i是n的约数},注意到gcd(x,n)=i的 7 充要条件是gcd(x/i,n/i)=1,因此满足条件的 8 x/i有phi(n/i)个,说明gcd(n,i)=phi(n/i). 9

【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})\] 后面那一堆

SP3871 GCDEX - GCD Extreme

SP3871 GCDEX - GCD Extreme 题目让我们求 \[\sum_{i=1}^n\sum_{j=i+1}^{n}gcd(i,j)\] \[\sum_{i=1}^n\sum_{j=1}^{i-1}gcd(i,j)\] 设\(g(n) = \sum_{i=1}^{n-1}gcd(i,n)\) \[\sum_{i=1}^ng(i)\] 考虑如何快速求\(g\) \[g(n)=\sum_{i=1}^{n-1}gcd(i,n)\] 设\(gcd(i,n)==d\) 换个方向枚举 \[g(n

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),这

UVa11424 GCD - Extreme (I)

直接两重循环O(n^2)算gcd……未免太耗时 枚举因数a和a的倍数n,考虑gcd(i,n)==a的i数量(i<=n) 由于gcd(i,n)==a等价于gcd(i/a,n/a)==1,所以满足gcd(i,n)==a的数有phi[n/a]个 打出欧拉函数表,枚举因数,计算出每个n的f[n]=gcd(1,n)+gcd(2,n)+gcd(3,n)+...+gcd(n-1,n) 然后求f[n]的前缀和,回答询问. 1 /*by SilverN*/ 2 #include<iostream> 3 #

USACO GCD Extreme(II)

题目大意:求gcd(1,2)+gcd(1,3)+gcd(2,3)+...+gcd(n-1,n) ----------------------------------------------------------------- 设f(i)=gcd(1,n)+...+gcd(n-1,n),则答案S(n)=f(2)+...+f(n) 如何求f 设g(n,i)表示满足gcd(x,n)=1且x<n的x个数,则f(n)=sum{i*g(n,i):i|n} gcd(x,n)=i的充要条件是x/i和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