题目连接   http://acm.hdu.edu.cn/showproblem.php?pid=1796 处男容斥原理  纪念一下  TMD看了好久才明白DFS... 先贴代码后解释 #include<cstdio> #include<cstring> using namespace std; #define LL long long #define N 11 LL num[N],ans,n; int m,cnt; LL gcd(LL a,LL b) { int t; while…
题意: 给你一个数n,找出来区间[1,n]内有多少书和n不互质 题解: 容斥原理 这一道题就让我真正了解容斥原理的实体部分 "容斥原理+枚举状态,碰到奇数加上(n-1)/lcm(a,b,c..) 碰到偶数减(n-1)/lcm(a,b,c...)" 这个是lcm(a,b,c,,,)可不是他们的乘积.. 注意了... 还有这道题输入会有0 代码: 1 #include<stdio.h> 2 #include<string.h> 3 #include<iostr…
How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7439    Accepted Submission(s): 2200 Problem Description   Now you get a number N, and a M-integers set, you shoul…
How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6630    Accepted Submission(s): 1913 Problem Description   Now you get a number N, and a M-integers set, you shoul…
容斥原理练习题,忘记处理gcd 和 lcm,wa了几发0.0. #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; ll Num[]; ll gcd(ll a,ll b) { ? a : gcd(b,a%b); } int main() { ll N, M; while(scanf("%lld%lld",&am…
HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 The Lottery 一模一样. 前置技能和其一样,但是需要注意的有一下几点: 1. m个数字中可能有0 2. 要用long long 代码总览 #include <cstdio> #include <algorithm> #include <cstring> #incl…
题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 } + sum{ 整除三个的数 }………………所以是奇加偶减,而整除 k 个数的数可以表示成 lcm(A1,A2,…,Ak) 的倍数的形式.所以算出最小公倍数, //HDU 1796 #include <cstdio> #include <iostream> #include <…
题意: 让你从区间[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)位置能看到多少棵树…
题目传送:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=20918&pid=1002 Problem Description   Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers…
How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5664    Accepted Submission(s): 1630 Problem Description   Now you get a number N, and a M-integers set, you shoul…
How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6434    Accepted Submission(s): 1849 Problem Description   Now you get a number N, and a M-integers set, you shou…
How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5556    Accepted Submission(s): 1593 Problem Description   Now you get a number N, and a M-integers set, you shou…
题意: 给一个N.然后给M个数,问1~N-1里面有多少个数能被这M个数中一个或多个数整除. 思路: 首先要N-- 然后对于每一个数M 事实上1~N-1内能被其整除的 就是有(N-1)/M[i]个 可是会出现反复 比方 例子 6就会被反复算 这时候我们就须要容斥原理了 加上一个数的减去两个数的.. 这里要注意了 两个数以上的时候 是求LCM而不是简单的相乘! 代码: #include "stdio.h" #include "string.h" #include &qu…
题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举n中有多少m中元素的个数,在结合LCM考虑容斥. #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #include<algorithm> using namespa…
How many integers can you find Problem Description   Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-intege…
题目链接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…
容斥原理!! 这题首先要去掉=0和>=n的值,然后再使用容斥原理解决 我用的是数组做的…… #include<iostream> #include<stdio.h> #include<algorithm> #include<iomanip> #include<cmath> #include<string> #include<vector> #define ll __int64 using namespace std;…
How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description   Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that…
How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6710    Accepted Submission(s): 1946 Problem Description   Now you get a number N, and a M-integers set, you shou…
<题目链接> 题目大意: 给你m个数,其中可能含有0,问有多少小于n的正数能整除这个m个数中的某一个. 解题分析: 容斥水题,直接对这m个数(除0以外)及其组合的倍数在[1,n)中的个数即可,因为可能会重复计算,所以在叠加的时候进行容斥处理,下面用的是位运算实现容斥. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namesp…
思路:二进制解决容斥问题,就和昨天做的差不多.但是这里题目给的因子不是质因子,所以我们求多个因子相乘时要算最小公倍数.题目所给的因数为非负数,故可能有0,如果因子为0就要删除. 代码: #include<set> #include<map> #include<cmath> #include<queue> #include<cstdio> #include<cstring> #include<iostream> #inclu…
题意:给定一个数 n,和一个集合 m,问你小于的 n的所有正数能整除 m的任意一个的数目. 析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n+1) * n个数的最小公倍数. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdl…
E - The Boss on Mars Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4059 Description On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger boss. Due…
Calculation 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2181    Accepted Submission(s): 920 Problem Description Given a positive integer N, your task is to calculate the sum of the positiv…
GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5310    Accepted Submission(s): 1907 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)…
Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2577    Accepted Submission(s): 1102 Problem Description There are many trees forming a m * n grid, the grid starts from (1,1). Farm…
状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示什么都不取得概率,p(x1)表示的是取x1的概率,最后要加一因为有又多拿了一次.整理一下就可以了. #include <cstdio> ; <<Maxn],p[Maxn]; int n; int main() { while (scanf("%d",&n)!…
题意: 1 //一组数据 3 3 //数字为1-3,3次运算 2 2 3 //将2号位变成3 1 1 3 4 //计算1-3号位上与4互质的数的和 1 2 3 6 好题,需要重复练习 #include<stdio.h> #include<iostream> #include<map> #include<set> #include<algorithm> #include<string.h> #include<stdlib.h>…
Frogs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 4843    Accepted Submission(s): 1605 Problem Description There are m stones lying on a circle, and n frogs are jumping over them.The stones…