您的位置:首页 > 其它

D - **GCD-Exreme(欧拉函数+数学分析,gcd转换) UVA - 11426

2017-10-22 21:36 435 查看
Given the value of N, you will have to nd the value of G.The denition of G is given below:

G=i<N∑i=1jN∑j=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 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 Gwill t in a 64-bit signed integer.

Sample Input

10

100

200000

0

Sample Output

67

13015

143295493160

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int maxn =4000001;
int phi[maxn];
ll ans[maxn];
/******
思路分析:
令sn为前n相的和。。。
sn=s(n-1)+gcd(1,n)+gcd(2,n)+gcd(3,n)+....gcd(n-1,n);
令(Ps : 自己写展开后会发现这个规律)
fn=gcd(1,n)+...gcd(n-1,n);
设x属于[1,n-1]
gcd(x,n)=i; 可知:gcd(x,n)=i等价与gcd(x/i,n/i)*i;
令g(n,i)为gcd(x,n)=i的个数,又有可知的:gcd(x,n)==gcd(x/i,n/i)*i,(欧拉函数,小于等于n与n互质的数)
所以g(n,i)==i*phi(n/i);
所以 fn=sum(i*phi(n/i));
令j=n/i;
则
f(n)=f(i*j)=sum(i*phi[j]);
sn=f2+f3+f4+....+fn;
******/
/*************
推荐博客:http://blog.sina.com.cn/s/blog_77dc9e080101iao9.html
推荐博客:http://www.cnblogs.com/ITUPC/p/4912971.html

*************/
//求欧拉函数
void find_phi()
{
ll i,j,k;
for (i=0;i<maxn;i++)
phi[i]=i;
for (i=2;i<maxn;i++)
{
if(phi[i]==i)
{
for (j=i;j<maxn;j+=i)
phi[j]=phi[j]/i*(i-1);
}
}
}
void solve ()
{
memset(ans,0,sizeof(ans));
ll i,j,k;
for (i=2;i<maxn;i++)
for (j=1;j*i<maxn;j++)
{
ans[i*j]+=j*phi[i];
}
///更新为sn
for (i=3;i<maxn;i++)
ans[i]+=ans[i-1];

}

int main ()
{
int n;
find_phi();
solve();
while (~scanf("%d",&n))
{
if(!n)
break;
cout<<ans
<<endl;
//        ll a =0;
//        int i;
//        for (i=1;i<=n;i++)
//        a+=ans[i];
//        cout<<a<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: