B - Calculation 2

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

用个互斥原理就可以了

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<cmath>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
//#define cl clear()
#define pb push_back
#define mm(a,b) memset((a),(b),sizeof(a))
#include<vector>
typedef __int64 ll;
typedef long double ld;
const ll mod=1e9+7;
using namespace std;
vector< int >v;
const double pi=acos(-1.0);
ll cal(ll x,ll n)
{
	if(x==n) return 0;
	int a=(n-1)/x;
	return (x*a+(x*a*(a-1))/2)%mod;
}
ll solve(ll n)
{
	ll x=n;
	for(int i=2;i*i<=x;i++)
	{
	//	cout<<"1"<<endl;
		if(x%i==0)
		{
			v.pb(i);
			while(x%i==0)
			{
				x/=i;
			 } 
		}
	}
	if(x>1) v.pb(x);
	ll sum=0;
	for(int i=1;i<(1<<v.size()) ;i++)
	{
		ll bits=0,ans=1;
		for(int j=0;j<v.size() ;j++)
		{
			if((1<<j)&i)
			{
				bits++;
				ans*=v[j];
			}
		}
		if(bits&1)
		sum=(sum+cal(ans,n))%mod;
		else
		{
			sum=(sum-cal(ans,n));
			while(sum<0)
			sum+=mod;
		}
	}
	return sum;
}
int main()
{
//	freopen("output1.txt", "r", stdin);
	ll n;
	while(1)
	{
		v.clear();
		sf("%I64d",&n);
		if(n==0) return 0;
		pf("%I64d\n",solve(n));
	}
}
posted @ 2018-07-23 16:03  一无所知小白龙  阅读(165)  评论(0编辑  收藏  举报