码迷,mamicode.com
首页 > 其他好文 > 详细

HDU - 4135 Co-prime

时间:2014-07-22 23:05:55      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:style   blog   os   io   2014   for   

题意:求[a,b]与n互质的个数

思路:容斥原理的运用,提取n的素因子,先算出不与n互素的个数,容斥原理奇加偶减

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long 
using namespace std;
const int MAXN = 70;

ll prime[MAXN];
ll a,b,n;

ll make(ll x, int m){
	ll ans = 0;
	ll len = 1<<m;
	for (ll i = 1; i < len; i++){
		ll tmp = 1,flag = 0;
		for (ll j = 0; j < m; j++)
			if (i & (ll)(1<<j)){
				++flag;
				tmp *= prime[j];
			}
		if (flag & 1)
			ans += x/tmp;
		else ans -= x/tmp;
	}
	return ans;
}

int main(){
	int t,cas=1;
	scanf("%d",&t);
	while (t--){
		cin>>a>>b>>n;
		int m = 0;
		for (ll i = 2; i*i <= n; i++)
			if (n && (n%i) == 0){
				prime[m++] = i;
				while (n && (n%i) == 0)
					n /= i;
			}
		if (n > 1)
			prime[m++] = n;
		printf("Case #%d: ",cas++);
		cout << b-make(b,m)-(a-1-make(a-1,m)) << endl;
	}
	return 0;
}



HDU - 4135 Co-prime,码迷,mamicode.com

HDU - 4135 Co-prime

标签:style   blog   os   io   2014   for   

原文地址:http://blog.csdn.net/u011345136/article/details/24726547

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!