HDU 3501 Calculation 2(欧拉函数+容斥原理)

Calculation 2

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5357    Accepted Submission(s): 2205


 

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

 

 

Author

GTmac

 

 

Source

2010 ACM-ICPC Multi-University Training Contest(7)——Host by HIT

 

 

Recommend

zhouzeyong

 

 

【思路】

把N分解一下,然后求出所有N的因数,再容斥原理得到最终答案,注意要求的是比N小的与其不互质的数的和,所以最后得减N,注意1要特判一下。因为N <= 1e9,我们令不同质因数从小到达相乘,到29时已达6e9,所以N的不同的质因数个数非常少,这个算法的复杂度仅仅是O(2 ^ 9),完成任务。

然而事实证明,我做数论题目又是在搞笑了。

更好的做法:若gcd(N, i) = 1,那么gcd(N, N - i) = 1,所以比N小的与N互质的数构成了\frac{\varphi (N)}{2}对,每对的和为N,我们只需要把与N互质的所有数的和求出来然后\sum_{i = 1}^{N}减之即可。很简洁的结论!

 

【代码】

//******************************************************************************
// File Name: HDU_3501.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: 2018年07月29日 星期日 11时08分29秒
//******************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;

const int MOD = 1e9 + 7;

ll n;
vector<int> p;

ll qpow(ll a, ll b, ll c)
{
	ll ans = 1;
	while (b) {
		if (b & 1) ans = ans * a % c;
		a = a * a % c;
		b >>= 1;
	}
	return ans;
}

void get_factors(ll n)
{
	p.clear();
	for (int i = 2; (ll)i * i <= n; i++) {
		if (n % i == 0) {
			p.push_back(i);
			while (n % i == 0) n /= i;
		}
	}
	if (n > 1) p.push_back(n);
}

vector<int> get_all(int bg, int i)
{
	vector<int> ans;
	if (i == 1) {
		for (int j = bg; j <= p.size(); j++) ans.push_back(p[j - 1]);
		return ans;
	}
	for (int j = bg; j <= p.size() - i + 1; j++) {
		vector<int> a = get_all(j + 1, i - 1);
		for (int k = 0; k < a.size(); k++) ans.push_back(p[j - 1] * a[k]);
	}
	return ans;
}

int main()
{
	ll inv_2 = qpow(2, MOD - 2, MOD);
	while (scanf("%lld", &n) == 1 && n) {
		if (n == 1) {
			printf("0\n");
			continue;
		}
		get_factors(n);
		ll ans = 0;
		for (int i = 1; i <= p.size(); i++) {
			vector<int> a = get_all(1, i);
			for (int j = 0; j < a.size(); j++) {
				ans = (ans + ((ll)(i & 1 ? 1 : -1) * ((ll)a[j] * (1 + n / a[j]) % MOD * (n / a[j]) % MOD * inv_2 % MOD)) % MOD + MOD) % MOD;
			}
		}
		ans = (ans - n + MOD) % MOD;
		printf("%lld\n", ans);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值