spoj 6285. Another Game With Numbers(容斥)
来源:互联网 发布:ssh管理系统源码 编辑:程序博客网 时间:2024/06/09 19:09
先收藏一个比较好的博客 http://www.cppblog.com/vici/archive/2011/09/05/155103.html
http://www.spoj.com/problems/SQFREE/
求1~n内不能被一个集合整除的数的个数。
简单的容斥原理,因为集合的个数 k <= 15,状态压缩一下,枚举每一个状态,根据其所含集合元素的个数进行加减。
复杂度(2^k*k*log x)
#include <stdio.h>#include <iostream>#include <map>#include <set>#include <bitset>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL __int64//#define LL long long#define eps 1e-9#define PI acos(-1.0)using namespace std;const int maxn = 100010;long long n;int k;int a[20];long long gcd(long long a, long long b){if(b == 0)return a;elsereturn gcd(b,a%b);}long long lcm(long long a, long long b){return a/gcd(a,b)*b;}int main(){while(~scanf("%lld %d",&n,&k)){for(int i = 0; i < k; i++)scanf("%d",&a[i]);long long ans = 0;for(int i = 1; i < (1<<k); i++){long long res = 1;int cnt = 0;for(int j = 0; j < k; j++){if(i & (1<<j)){res = lcm(res,a[j]);cnt++;}}if(cnt & 1)ans += n/res;elseans -= n/res;}printf("%lld\n",n-ans);}return 0;}
hdu 1796 http://acm.hdu.edu.cn/showproblem.php?pid=1796
这题有坑,输入有可能为0,要把0的去掉。
#include <stdio.h>#include <iostream>#include <map>#include <set>#include <bitset>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL __int64//#define LL long long#define eps 1e-9#define PI acos(-1.0)using namespace std;const int maxn = 100010;LL n;int k,m;int a[20];LL gcd(LL a, LL b){if(b == 0)return a;elsereturn gcd(b,a%b);}LL lcm(LL a, LL b){return a/gcd(a,b)*b;}int main(){while(~scanf("%I64d %d",&n,&m)){ k = 0; for(int i = 0; i < m; i++) { scanf("%d",&a[i]); } for(int i = 0; i < m; i++) { if(a[i]) //a[i]为0的丢弃 a[k++] = a[i]; }LL ans = 0;for(int i = 1; i < (1<<k); i++){LL res = 1;int cnt = 0;for(int j = 0; j < k; j++){if(i & (1<<j)){res = lcm(res,a[j]);cnt++;}}if(cnt & 1)ans += (n-1)/res;elseans -= (n-1)/res;}printf("%I64d\n",ans);}return 0;}
0 0
- spoj 6285. Another Game With Numbers(容斥)
- SPOJ 6285. Another Game With Numbers
- SPOJ GAME3 9842. Yet Another Fancy Game 博弈
- SPOJ 4060 A game with probability
- 【SPOJ TSUM】Triple Sums(FFT+容斥定理)
- spoj 4191. Sky Code(容斥)
- spoj 4168. Square-free integers(容斥)
- Light OJ 1095 Arrange the Numbers (容斥)
- 【POJ】1142 - Smith Numbers(容斥原理)
- Smith Numbers(欧拉函数,容斥原理)
- GYM 101102 J.Divisible Numbers(数论+容斥原理)
- [BZOJ2986]Non-Squarefree Numbers(二分+容斥原理)
- bzoj 2986: Non-Squarefree Numbers (容斥原理)
- SPOJ BALNUM Balanced Numbers (数位dp)
- SPOJ-BALNUM Balanced Numbers (数位DP)
- SPOJ BALNUM - Balanced Numbers(数位DP)
- SPOJ:Balanced Numbers(数位dp)
- Another Graph Game(hdu4647,拆边+贪心)
- 设计模式初探1——策略模式(Strategy Pattern)
- 上海传智程序猿携手设计狮美女雨中烧烤!!
- JSTL中EL标签与Struts2中OGNL标签在JSP页面上的区别
- Struts1—从BeanUtils看struts的实现原理3(自定义BeanUtils的转换器)
- JS 打印功能代码(包括打印预览、打印设置等)
- spoj 6285. Another Game With Numbers(容斥)
- 2014年CPU排名
- 【数据结构】邓玉欣的编程作业indexing
- java MD5加密
- Tomcat配置技巧Top 10
- QuickFix/J 源代码研究
- java之进制计算
- Java Serialization/序列化/反序列化
- 实验2_数据类型与运算_参考答案