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的更多相关文章

  1. 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> ...

  2. ACM HDU 1755 -- A Number Puzzle

    A Number Puzzle Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. Number Puzzle

    Number Puzzle Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a list of integers (A1, A2, .. ...

  4. ZOJ 3435 Ideal Puzzle Bobble

    ZOJ Problem Set - 3435 Ideal Puzzle Bobble Time Limit: 2 Seconds      Memory Limit: 65536 KB Have yo ...

  5. [ZOJ]3541 Last Puzzle (区间DP)

    ZOJ 3541 题目大意:有n个按钮,第i个按钮在按下ti 时间后回自动弹起,每个开关的位置是di,问什么策略按开关可以使所有的开关同时处于按下状态 Description There is one ...

  6. ZOJ 1602 Multiplication Puzzle(区间DP)题解

    题意:n个数字的串,每取出一个数字的代价为该数字和左右的乘积(1.n不能取),问最小代价 思路:dp[i][j]表示把i~j取到只剩 i.j 的最小代价. 代码: #include<set> ...

  7. 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 ...

  8. zoj 2836 容斥原理

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2836 #include <cstdio> #incl ...

  9. zoj Beautiful Number(打表)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2829 题目描述: Mike is very lucky, as ...

随机推荐

  1. Demo学习: FileUpload

    FileUpload 文件上传,学习TUniFileUpload控件的使用 TUniFileUpload主要属性: Filter: 文件类型过滤,这个属性在web模式下是无效的,UniGUI目前版本还 ...

  2. IOS系统框架

    IOS系统框架:ios架构号MAC OS的基础框架相似:ios扮演底层硬件和应用程序的中介,底层为所有应用程序提供基础服务,高层则包含一些复杂巧妙的技术和服务,并且创建的应用程序不能直接访问硬件. C ...

  3. Django配置静态文件(CSS\js)及Django调用JS、CSS、图片等静态文件

    1 新建一项目: root@python:django-admin.py startproject csstest root@python:cd csstest root@python:ls csst ...

  4. Linux进程间通信IPC学习笔记之消息队列(SVR4)

    Linux进程间通信IPC学习笔记之消息队列(SVR4)

  5. 抄书(UVa714)

    Description   Before the invention of book-printing, it was very hard to make a copy of a book. All ...

  6. javaweb 在netbeans 上的部署问题

    最近用netbeans去部署开发 javaweb程序!现总结一下自己的安装开发过程. 第一,在官网上下载netbeans时,有自带的tomcat,可以直接使用这个服务器,在我这儿这个自带的tomcat ...

  7. 论Oracle字符集“转码”过程

    本文将通过实验来演示一下Oracle字符集“转码”的确认过程. 1.实验环境说明 客户端是Windows XP操作系统的SQL*Plus程序,客户端字符集是936(对应Oracle的ZHS16GBK字 ...

  8. GridView ItemCommand

    GridView ItemCommand中取某行某列的值方法,这里提供两个常用的: 一.用CommandArgument属性取值页面如下: <asp:TemplateColumn HeaderT ...

  9. Windows.Andy.Code4App.dll Win8.1/WP8.1通用类库@ver1.0.1

    在上篇 Windows.Andy.Code4App.dll  Win8.1/WP8.1通用类库@ver1.0.0 已经对Win8.1和WP8.1部分扩展通用类库做了说明,这篇继续对通用类库做扩展.写的 ...

  10. dive into python 读笔(2)

    chapter 4 自省, summary: # 用可选和命名参数定义和调用函数 # 用 str 强制转换任意值为字符串形式 # 用 getattr 动态得到函数和其它属性的引用 # 扩展列表解析语法 ...