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 f[n]=sum{i*phi(n/i),1=<i<n}
10 因此可以搞个for循环对i循环,只要i<n,f[n]+=i*phi(n/i);
11
12
13 #include <iostream>
14 #include <cstring>
15 using namespace std;
16 #define Max 1000000
17
18 long long phi[Max+5],ans[Max+5];
19 int prime[Max/3];
20 bool flag[Max+5];
21
22 void init()
23 {
24 long long i,j,num=0;
25 memset(flag,1,sizeof(flag));
26 phi[1]=0;
27 for(i=2;i<=Max;i++)//欧拉筛选
28 {
29 if(flag[i])
30 {
31 prime[num++]=i;
32 phi[i]=i-1;
33 }
34 for(j=0;j<num && prime[j]*i<=Max;j++)
35 {
36 flag[i*prime[j]]=false;
37 if(i%prime[j]==0)
38 {
39 phi[i*prime[j]]=phi[i]*prime[j];
40 break;
41 }
42 else phi[i*prime[j]]=phi[i]*(prime[j]-1);
43 }
44 }
45 //for(i=1;i<=10;i++)
46 // cout<<phi[i]<<endl;
47
48 ans[0] =0;
49 for(i=1;i*i<=Max;i++){
50 ans[i*i] += i*phi[i];
51 for(j =i+1;j*i<=Max;j++)
52 ans[i*j] += i*phi[j]+j*phi[i];
53 }
54 for(int i=1;i<=Max;i++)
55 ans[i] += ans[i-1];
56 }
57
58 int main(){
59 init();
60 long long n;
61 while(cin>>n&&n){
62
63 cout<<ans[n]<<endl;
64 }
65 }

spoj 3871 gcd extreme

时间: 2024-12-21 06:21:21

spoj 3871 gcd extreme的相关文章

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

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)

【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

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