LightOJ
来源:互联网 发布:淘宝刷单兼职靠谱吗 编辑:程序博客网 时间:2024/06/13 05:49
Cicada is an insect with large transparent eyes and well-veined wings similar to the “jar flies”. The insects are thought to have evolved 1.8 million years ago during the Pleistocene epoch. There are about 2,500 species of cicada around the world which live in temperate tropical climates.
These are all sucking insects, which pierce plants with their pointy mouthparts and suck out the juices. But there are some predators (like birds, the Cicada Killer Wasp) that attack cicadas. Each of the predators has a periodic cycle of attacking Cicadas. For example, birds attack them every three years; wasps attack them every 2 years. So, if Cicadas come in the 12th year, then birds or wasps can attack them. If they come out in the 7th year then no one will attack them.
So, at first they will choose a number N which represents possible life-time. Then there will be an integer M indicating the total number of predators. The next M integers represent the life-cycle of each predator. The numbers in the range from 1 to N which are not divisible by any of those M life-cycles numbers will be considered for cicada’s safe-emerge year. And you want to help them.
Input
Input starts with an integer T (≤ 125), denoting the number of test cases.
Each case contains two integers N (1 ≤ N < 231) and M (1 ≤ M ≤ 15). The next line contains M positive integers (fits into 32 bit signed integer) denoting the life cycles of the predators.
Output
For each test case, print the case number and the number of safe-emerge days for cicada.
Sample Input
2
15 3
2 3 5
10 4
2 4 5 7
Sample Output
Case 1: 4
Case 2: 3
题意:
给出n和m个数,问小于等于n的所有中有多少个数不是这m个数的倍数。m<=15
思路:
首先我们想可以筛,比如对于任意一个数 我们知道在n中有 n/a[i] 个数是a[i]的倍数。但是如果这样把m个数扫一遍肯定会有很多重复的数,所以容斥一下。比如选了两个数,我们就要减去他们最小公倍数能够组成的数
所以ANS=n-(ans=从m中选一个数得到的倍数的个数-选两个数得到的倍数+选三个数得到的倍数。。。。)
一共有2^m种情况,因为m很小 所以压缩成1~2^m 对于每一种状态 判断当前状态由多少个数组成,奇+ 偶-
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;int f[2]={-1,1};int num[20];long long LCM(long long a,long long b){ long long x= __gcd(a,b); return a*b/x;}int main(){ freopen("in.txt","r",stdin); int T; scanf("%d",&T); int n,m,x; int cas=0; while(T--){ scanf("%d%d",&n,&m); for(int i=0;i<m;i++) scanf("%d",&num[i]); int maxn=1<<m; long long ans=0; for(int i=1;i<maxn;i++){ long long tmp=1; int t=0; for(int j=0;(1<<j)<=i;j++){ if(i & (1<<j)){ t++; tmp=LCM(tmp,num[j]); } }// cout<< ans+=f[t & 1]*n/tmp; } printf("Case %d: %lld\n",++cas,n-ans); } return 0;}
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- [LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- LightOJ
- 【Solr】深入浅出Solr(二)——搭建单机服务
- eclipse基础设置 提高开发速度和质量
- QS Network 【zoj-1586】【最小生成树】
- zoj1002 Fire Net
- 8月5日小结
- LightOJ
- 关于MPLS 技术及MPLS VPN的学习
- 度度熊与邪恶大魔王(2017"百度之星"程序设计大赛
- Matplotlib基础知识
- 51nod1109(01组成的n的倍数)
- Ubuntu 搭建强大的 IDE —— Vim + Vundle + 插件(上)
- 动态规划——Tour
- 接口测试—-中篇,通过django实现一个非常简单的记事本接口
- Java初学者必看