Co-prime HDU - 4135】的更多相关文章

问a,b区间内与n互质个数,a,b<=1e15,n<=1e9 n才1e9考虑分解对因子的组合进行容斥,因为19个最小的不同素数乘积即已大于LL了,枚举状态复杂度不会很高.然后差分就好了. /** @Date : 2017-09-28 16:52:30 * @FileName: HDU 4135 容斥.cpp * @Platform: Windows * @Author : Lweleth (SoungEarlf@gmail.com) * @Link : https://github.com/…
题目链接: 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…
http://acm.hdu.edu.cn/showproblem.php?pid=4135 求[A,B]内与N互素的数字个数 首先对N分解质因数,对于一个质因数,1-n与它不互素的数字个数是n/(这个质因数),这样可以得到m个集合(m是N分解出的质因数的个数),对这m个集合用容斥原理解出来它们的并集,再用总数去减 这里学习了用位运算求解容斥原理,非常简单,和状压dp中的位运算差不多感觉,注意容斥中奇加偶减 #include <iostream> #include <cstdio>…
Co-prime 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4135 input The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=…
http://acm.hdu.edu.cn/showproblem.php?pid=4135 给定一个数n,求某个区间[a,b]内有多少数与这个数互质. 对于一个给定的区间,我们如果能够求出这个区间内所有与其不互质的数的个数的话,那么互质数的个数也就求出来. 任何与N不互质的数一定是其某一个质因子的倍数,所以我们通过某一个质因子能够确定一个范围内的有多少个数该质因子的倍数的数.但是这并不是所有不互质数的充分条件,对于另外的质因子同样满足能够确定一系列的数,我们要做的就是容斥定理求他们的并集.nu…
Co-prime Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4135 推荐: 容斥原理 Mean: 给你一个区间[l,r]和一个数n,求[l,r]中有多少个数与n互素. analyse: 经典的容斥原理题. 如果题目是说求1~n中有多少个数与n互质,我们一定反应应该是欧拉函数. 但是如果n特别大或者说是求某个给定区间与n互素的个数,这时用欧拉函数就行不通. 容斥做法:首先我们可以在O(sqrt(n))内求出n的所有质因数p…
acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 询问[a,b]中与n互质的数有多少个 [思路] 考虑[1,m]中与n互质的数有多少个,答案就是query(b)-query(a-1) 正难则反,考虑[1,m]中与n不互质的数有多少个 求出n的所有素因子a1,a2,a3 容斥:+m/a1+m/a2+m/a3-m/(a1*a2)-m/(a1*a3)-m/(a2*a3)+m/(a1*a2*a3) [AC] #include<bits/stdc++.h> usi…
[题目链接]:http://acm.hdu.edu.cn/showproblem.php?pid=4135 [题意] 让你求出[a..b]这个区间内和N互质的数的个数; [题解] 利用前缀和,求出[1..a-1]和a[1..b]这两个区间内和N互质的数的个数; 方法是; 从小到大求出N的所有因子xi; 然后从大到小枚举每个因子xi; 可以求出[1..t]内和N的最大公因数为xi的个数设为f[xi]; (减去大于xi且为xi的倍数的y就好,即f[xi]-=f[y],一开始f[xi]=t/xi);…
题目链接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…
Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2307    Accepted Submission(s): 861 Problem Description Given a number N, you are asked to count the number of integers between A and B…
Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5526    Accepted Submission(s): 2209 Problem Description Given a number N, you are asked to count the number of integers between A and B…
Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6869    Accepted Submission(s): 2710 Problem Description Given a number N, you are asked to count the number of integers between A and B…
Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 935    Accepted Submission(s): 339 Problem Description Given a number N, you are asked to count the number of integers between A and B in…
题目链接 题意:问从A到B中与N互素的个数. 题解: 利用容斥原理:先求出与n互为素数的个数. 可以先将 n 进行素因子分解,然后用区间 x 除以 素因子,就得到了与 n 的 约数是那个素因子的个数,然后每次这样求一遍,但是发现有重 复的:举个例子 [1,10] 区间中与 6 互素的个数,应该是 10−(10/2+10/3)+(10/6) 然后利用二进制枚举子集个数,奇数加偶数减. 具体看代码: #include <stdio.h> #include <cstring> #incl…
Problem Description Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than…
Co-prime 第一发容斥,感觉挺有意思的 →_→ [题目链接]Co-prime [题目类型]容斥 &题意: 求(a,b)区间内,与n互质的数的个数. \(a,b\leq 10^{15}\) &题解: 分析:我们可以先转化下:用(1,b)区间与n互质的数的个数减去(1,a-1)区间与n互质的数的个数,那么现在就转化成求(1,m)区间于n互质的数的个数,如果要求的是(1,n)区间与n互质的数的个数的话,我们直接求出n的欧拉函数值即可,可是这里是行不通的!我们不妨换一种思路:就是求出(1,m…
思路:直接用求(b,1)范围内互质的数,(a-1,1)范围内互质的数.再求反 就是敲一下容斥模板 #include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<stack> #include<algorithm> using namespace std; #define clc(a,b) memset(a,b,sizeof(a)) #def…
题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n互素的数的个数 容斥原理 AC代码 #include <cstdio> #include <cmath> #include <cctype> #include <bitset> #include <algorithm> #include <cs…
<题目链接> 题目大意: 给定区间[A,B](1 <= A <= B <= 10 15)和N(1 <=N <= 10 9),求出该区间中与N互质的数的个数. 解题分析: 将求区间[A,B]与N互质的数转化成求[1,B] 区间与N互质的个数  -  [1,A-1]中与N互质的个数.同时,因为直接求区间内与N互质的数不好求,我们从反面入手,求出与N不互质的数,借鉴埃筛的思想,我们先求出N的所有质因子,然后将这些质因子在区间内倍数的个数全部求出(即与N不互质的数),再用…
题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也就是有除1的公因数.那么我们把n质因数分解,就能算出含某些公因数的不互质的个数.因为会重复,所以容斥解决.因为因数个数可能很多(随便算了一个20!> 2e18,所以质因数分解个数不会超过20个),我们可以用二进制来遍历解决. #include<set> #include<map>…
思路: 直接容斥 //By SiriusRen #include <cstdio> using namespace std; #define int long long ; int cases,a,b,n,tp,s[N]; int solve(int r){ ; ;i<(<<tp);i++){ ,t=; ;j<tp;j++) <<j))tmp*=s[j],t++; t=t&?:-; ans+=r/tmp*t; } return r-ans; } si…
题目链接:https://vjudge.net/problem/HDU-4135#author=0 题意:求在区间[a,b]中有多少个数与n互质. 思路:先看数据范围很大,所以不能枚举.因为互质难求,我们可以求不互质的最后减去.那么我们就要求出n的所有因子{p0,p1,........pi},在区间[1,x]中有x/pi个pi,然后利用容斥定理减去质因数公倍数重复计数的部分就可以了. 1 #include<time.h> 2 #include <set> 3 #include &l…
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4141    Accepted Submission(s): 1441 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y)…
http://acm.hdu.edu.cn/showproblem.php?pid=1695 x是[1,b],y是[1,d],求GCD(x,y)=k的对数(x,y无序) 对x,y都除以k,则求GCD(x,y)=1 此时枚举x,问题转化为[1,d]区间内与x互素的数字个数,这个问题是hdu 4135 有一个特殊的地方是x,y无序,对于这点只要保证x始终小于y就可以了 特判k=0 #include <iostream> #include <cstdio> #include <cs…
题意: 让你从区间[a,b]里面找一个数x,在区间[c,d]里面找一个数y.题目上已经设定a=b=1了.问你能找到多少对GCD(x,y)=k.x=5,y=7和y=5,x=7是同一对 题解: 弄了半天才知道我得容斥原理方法卡时间了,我那个复杂度太高了...卧槽了 老版本的这里可以看:HDU - 4135 容斥原理 下面说一下复杂度低的容斥原理的思想 这种方法的基本思想是:先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来,然后再把计数时重复计算的数目排斥出去,使得计算的结果既无遗漏又无…
题意: 给你一个n*m的矩形,在1到m行,和1到n列上都有一棵树,问你站在(0,0)位置能看到多少棵树 题解: 用(x,y)表示某棵树的位置,那么只要x与y互质,那么这棵树就能被看到.不互质的话说明前面已经有树挡住了这棵树 i是[1,m]中的任意一个数 我们可以for循环求在区间[1,n]内有多少数与i互质 求法就是容斥原理,具体见这里:HDU - 4135 容斥原理 代码: 1 /* 2 题意: 3 给你一个n*m的矩形,在1到m行,和1到n列上都有一棵树,问你站在(0,0)位置能看到多少棵树…
容斥原理我初中就听老师说过了,不知道你们有没有听过(/≧▽≦)/ 百度百科说: 在计数时,必须注意没有重复,没有遗漏. 为了使重叠部分不被重复计算,人们研究出一种新的计数方法. 这种方法的基本思想是:先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来,然后再把计数时重复计算的数目排斥出去,使得计算的结果既无遗漏又无重复. 这种计数的方法称为容斥原理. 好标准的说法(#-.-) 那我举个简单的例子 两个集合的容斥原理: 设A, B是两个有限集合 那么 |A + B| = |A| + |…
hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem/3223/ 思路:主要是用了一个Meisell-Lehmer算法模板,复杂度O(n^(2/3)).讲道理,我不是很懂(瞎说什么大实话....),下面输出请自己改 #include<bits/stdc++.h> using namespace std; typedef long long LL;…
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 42600    Accepted Submission(s): 18885 Problem Description A ring is compose of n circles as shown in diagram. Put natural num…
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1973 Prime Path Description The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices…