您的位置:首页 > 其它

hdoj-3501-Calculation 2

2016-08-28 11:39 288 查看
Problem Description

Given a positive integer N, your task is to calculate the sum of the positive integers less than N which are not coprime to N. A is said to be coprime to B if A, B share no common positive divisors except 1.

Input

For each test case, there is a line containing a positive integer N(1 ≤ N ≤ 1000000000). A line containing a single 0 follows the last test case.

Output

For each test case, you should print the sum module 1000000007 in a line.

Sample Input

3

4

0

Sample Output

0

2

求1~n里面比n小,但是与n不互素的数的总和

利用公式可求

利用欧拉函数即可求解,1~n比n小且与n互素的数的总和为 sum(n) = n * phi(n) / 2;那么可以先求出1~n-1的总和,然后 减去sum(n)即可。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;

const int mod=1000000007;
typedef long long ll;

ll phi(int n)
{
int m=(int)sqrt(n+0.5);
int ans=n;
for(int i=2;i<=m;i++)
if(n%i==0)
{
ans=ans/i*(i-1);
while(n%i==0) n/=i;
}
if(n>1) ans=ans/n*(n-1);
return ans;
}

int main()
{
ll n;
while(scanf("%lld",&n)!=EOF)
{
if(n==0) break;
ll sum=n*(n-1)/2;
ll res=sum-(n*phi(n)/2);
printf("%I64d\n",res%mod);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: