[ZOJ 2836] Number Puzzle
Number Puzzle
Time Limit: 2 Seconds Memory Limit: 65536 KB
Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers that are not greater than M and dividable by any integer from the given list.
Input
The input contains several test cases.
For each test case, there are two lines. The first line contains N (1 <= N <= 10) and M (1 <= M <= 200000000), and the second line contains A1, A2, ..., An(1 <= Ai <= 10, for i = 1, 2, ..., N).
Output
For each test case in the input, output the result in a single line.
Sample Input
3 2 2 3 7 3 6 2 3 7
Sample Output
1 4
入门容斥原理,见代码
#include <iostream>
#include <cstdio>
using namespace std;
#define ll long long
#define N 10 int n,m;
int a[N];
int gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
int lcm(ll a,ll b){
return a/gcd(a,b)*b;
}
////写法1/////
int solve()
{
int res=;
for(int i=;i<(<<n);i++)
{
int cnt=;
ll LCM=;
for(int j=;j<n;j++)
{
if(i&(<<j)) cnt++,LCM=lcm(LCM,a[j]);
}
if(cnt&) res+=m/LCM;
else res-=m/LCM;
}
return res;
}
////写法2/////
int ans;
void dfs(int pos,int LCM,int cnt)
{
if(cnt)
{
if(cnt&) ans+=m/LCM;
else ans-=m/LCM;
}
for(int i=pos+;i<n;i++)
dfs(i,lcm(LCM,a[i]),cnt+);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=;i<n;i++) scanf("%d",&a[i]);
//printf("%d\n",solve());
ans=;
dfs(-,,);
printf("%d\n",ans);
}
return ;
}
[ZOJ 2836] Number Puzzle的更多相关文章
- ZOJ 2836 Number Puzzle 题解
题面 lcm(x,y)=xy/gcd(x,y) lcm(x1,x2,···,xn)=lcm(lcm(x1,x2,···,xn-1),xn) #include <bits/stdc++.h> ...
- ACM HDU 1755 -- A Number Puzzle
A Number Puzzle Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Number Puzzle
Number Puzzle Time Limit: 2 Seconds Memory Limit: 65536 KB Given a list of integers (A1, A2, .. ...
- ZOJ 3435 Ideal Puzzle Bobble
ZOJ Problem Set - 3435 Ideal Puzzle Bobble Time Limit: 2 Seconds Memory Limit: 65536 KB Have yo ...
- [ZOJ]3541 Last Puzzle (区间DP)
ZOJ 3541 题目大意:有n个按钮,第i个按钮在按下ti 时间后回自动弹起,每个开关的位置是di,问什么策略按开关可以使所有的开关同时处于按下状态 Description There is one ...
- ZOJ 1602 Multiplication Puzzle(区间DP)题解
题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...
- ZOJ 3908 Number Game ZOJ Monthly, October 2015 - F
Number Game Time Limit: 2 Seconds Memory Limit: 65536 KB The bored Bob is playing a number game ...
- zoj 2836 容斥原理
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2836 #include <cstdio> #incl ...
- zoj Beautiful Number(打表)
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2829 题目描述: Mike is very lucky, as ...
随机推荐
- Delphi摄像头操作
/*Title:Delphi摄像头操作 *Author:Insun *Blog:http://yxmhero1989.blog.163.com *From:www.4safer.com */ 为了笔耕 ...
- Demo学习: DownloadDemo
DownloadDemo 学习文件下载 1. 几个获取临时路径的函数: UniServerModule.TempFolderURL //当前程序路径下"Temp"文件夹: Uni ...
- FPGA串口波特率简析
以前用单片机,一直都是直接用就行,设置波特率时,直接写9600就行,一直没有仔细考虑过,今天打算用FPGA写个串口程序时才知道,原来根本就是没弄明白.一下是我的一些见解.如果诸位看官觉得不对,欢迎指正 ...
- NOSQL Mongo入门学习笔记 - MongoDB的安装(一)
手上的工作不是很忙,所以来学习学习很久就像接触的MongoDb,无奈前段时间工作时间都比较多.记录在这里供以后参考 环境: Centos 7 64位 开始: 1. 在官网下载Mongo : wget ...
- C#的历史及IDE总结
原Borland公司的首席研发设计师安德斯·海爾斯伯格(Anders Hejlsberg)在微軟開發了Visual J++ 1.0,很快的Visual J++由1.1版本升級到6.0版.SU ...
- java第五课:方法
方法交换位置的三个步骤:1.把第一个盒子里的东西拿出来,放到一边2.把第二个盒子里的东西放到第一个盒子里3.捡起刚刚放到一边的东西,放到第二个盒子里 值传递:实际参数将内部保存的值,复制给方法的参数. ...
- 1061: [Noi2008]志愿者招募 - BZOJ
Description 申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管.布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募一批短期志愿者.经过估算,这个项目需要N 天才能完 ...
- 用JavaScript获取页面上被选中的文字的技巧
这里介绍的一个小技巧是如何用JavaScript获取页面上被选中的文字的方法.最关键的JavaScript API是: event.selection = window.getSelection(); ...
- 云风的BLOG❳可靠 UDP 传输
http://mp.weixin.qq.com/s?__biz=MzA3NjYxOTA0MQ==&mid=405432715&idx=1&sn=2e40ceafd4b298e1 ...
- linux 深入检测io详情的工具iopp
1.为什么推荐iopp iotop对内核及python版本都有一定要求,有时候无法用上,这时候就可以使用iopp作为替代方案.在有些情况下可能无法顺利使用iotop,这时候就可以选择iopp了.它的作 ...