hdu 3501 数学题

 http://acm.hdu.edu.cn/showproblem.php?pid=3501

 一道数学题,不是很难,老题了。但从中学到了不少东西。

    1:if gcd(n,i) == 1     then gcd(n,n-i) == 1

           so sum(n) = n * phi(n) / 2;  //sum(n): 比n小的和n互质的数的和   phi(n): 比n小的跟n互质的数的个数

    2:n = P1^n1 * P2^n2 * P3^n3 *…………* Pk^nk   //P1,P2,Pk都是比n小的质数

         而phi(n) = (P1-1)P1^n1 * (P2-1)P2^n2 *…………* (Pk-1)Pk^nk

         实际上phi(n) 可以用欧拉函数求出!

代码
#include <iostream>
#include
<cmath>
using namespace std;

__int64 phi(__int64 x)
{
__int64 i,res
= x;
for(i=2;i<(__int64)sqrt(x*1.0)+1;i++)
{
if(x%i==0)
{
res
= res / i * (i-1);
while(x % i == 0)
x
/= i;
}
}
if(x > 1)
res
= res / x * (x - 1);

return res;
}

int main()
{
__int64 n;
while(scanf("%I64d",&n)!=EOF && n!=0)
{
__int64 num
= n - 1 - phi(n);
num
= (num * n / 2) % 1000000007;
while(num<0)
num
+= 1000000007;

printf(
"%I64d\n",num);
}
return 0;
}

原文地址:https://www.cnblogs.com/silencExplode/p/1883125.html