HDU 4135-Co-prime(容斥原理)

题目链接:传送门

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

思路:用容斥求出[1-b]与n互质的个数—[1-(a-1)]内与n互质的个数。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <cctype>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define maxn 360
#define _ll __int64
#define ll long long
#define INF 0x3f3f3f3f
#define Mod 1000000007
#define pp pair<int,int>
#define ull unsigned long long
#define max(x,y) ( ((x) > (y)) ? (x) : (y) )
#define min(x,y) ( ((x) > (y)) ? (y) : (x) )
using namespace std;
_ll fac[maxn],tot,A,B,N,ans;
void div(int x)
{
	tot=0;
	for(_ll i=2;i*i<=x;i++)
	{
		if(x&&x%i==0)
		{
			fac[tot++]=i;
			while(x&&x%i==0)x/=i;
		}
	}
	if(x>1)fac[tot++]=x;
}
void dfs(_ll num,_ll s,_ll r,_ll n)
{
	if(num==tot)
	{
		if(s&1)ans-=n/r;
		else ans+=n/r;
		return ;
	}
	dfs(num+1,s,r,n);
	dfs(num+1,s+1,r*fac[num],n);
}
int cas=1;
void solve()
{
	div(N);
	ans=0;dfs(0,0,1,B);_ll ans_1_b=ans;
	ans=0;dfs(0,0,1,A-1);_ll ans_1_a=ans;
	printf("Case #%d: %I64d\n",cas++,ans_1_b-ans_1_a);
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%I64d%I64d%I64d",&A,&B,&N);
		solve();
	}
	return 0;
}
时间: 2024-10-12 18:58:47

HDU 4135-Co-prime(容斥原理)的相关文章

HDU 4135 Co-prime(容斥原理 + 基础数论)

传送门 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3695    Accepted Submission(s): 1461 Problem Description Given a number N, you are asked to count the number of integers between A an

hdu 4135 Co-prime【容斥原理】

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1668    Accepted Submission(s): 636 Problem Description Given a number N, you are asked to count the number of integers between A and B

[容斥原理] hdu 4135 Co-prime

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1176    Accepted Submission(s): 427 Problem Description Given a number N, you are a

hdu how many prime numbers 筛选法求素数

/* * hdu How many prime numbers * date 2014/5/13 * state AC */ #include <iostream> #include <cmath> #include <cstdio> using namespace std; bool isPrime(int x) { int sqr=sqrt(x*1.0); for(int i=2;i<=sqr;i++) { if(x%i==0)return false; }

HDU 1016:Prime Ring Problem

Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25156    Accepted Submission(s): 11234 Problem Description A ring is compose of n circles as shown in diagram. Put natural num

容斥原理学习(Hdu 4135,Hdu 1796)

题目链接Hdu4135 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1412    Accepted Submission(s): 531 Problem Description Given a number N, you are asked to count the number of integers betwe

hdu 4135 Co-prime +hdu 2841 Visible Trees(容斥原理)

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2263    Accepted Submission(s): 847 Problem Description Given a number N, you are asked to count the number of integers between A and B

HDU - 4135 Co-prime(容斥原理)

Question 参考 题意找出[a,b]中与n互质的数的个数分析通常我们求1-n中与n互质的数的个数都是用欧拉函数.但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理.先对n分解质因数,分别记录每个质因数, 那么所求区间内与某个质因数不互质的个数就是 m/r(i),假设r(i)是r的某个质因子 假设只有三个质因子, 总的不互质的个数应该为p1+p2+p3-p1*p2-p1*p3-p2*p3+p1*p2*p3. pi代表m/r(i),即与某个质因子不互质的

hdu 4135 容斥原理

又搞了一道容斥原理. 题目:求[1,n]区间对m互质的数有多少个? #include<iostream> #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define LL __int64 const int maxn = 1e5+8; LL a[maxn],cn,numpri[maxn],vis[maxn]

HDU 4135 Co-prime (容斥原理+质因数分解)

这题只要知道质因数的性质就很容易做了.任意一个正整数(除了1)都可以分解成有限个质数因子的乘积. 那么假如两个数互质,那么这两个数肯定至少各有一个对方没有的质因子.所以若一个数跟n不互质,那么这个的数的质因子肯定也都属于n的质因子,那么就用容斥原理求出所有跟n不互质的所有数的个数.然后再用总的减去即可. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue&g