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

分析:1~m中至少能被列表里其中一个数整除的数的个数;

   容斥即可,注意列表中的数不一定互质,所以要求lcm;

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
using namespace std;
inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
inline void umax(ll &p,ll q){if(p<q)p=q;}
inline void umin(ll &p,ll q){if(p>q)p=q;}
inline ll read()
{
ll x=;int f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,m,k,t,fac[];
int main()
{
int i,j;
while(~scanf("%d%d",&n,&m))
{
rep(i,,n-)scanf("%d",&fac[i]);
int ret=;
rep(i,,(<<n)-)
{
int now=,cnt=;
rep(j,,n-)
{
if(i&(<<j))
{
cnt++;
now=now*fac[j]/gcd(now,fac[j]);
}
}
if(cnt&)ret+=m/now;
else ret-=m/now;
}
printf("%d\n",ret);
}
return ;
}

Number Puzzle的更多相关文章

  1. [ZOJ 2836] Number Puzzle

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

  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. hdu 1755 A Number Puzzle

    这题枚举k节省时间 ;}

  4. HDOJ 1755 - A Number Puzzle 排列数字凑同余,状态压缩DP

    dp [ x ] [ y ] [ z ] 表示二进制y所表示的组合对应的之和mod x余数为z的最小数... 如可用的数字为 1 2 3 4...那么 dp [ 7 ] [ 15 ] [ 2 ] = ...

  5. [全排列]--A Number Puzzle

    标签: ACM Lele 最近上课的时候都很无聊,所以他发明了一个数字游戏来打发时间. 这个游戏是这样的,首先,他拿出几张纸片,分别写上0到9之间的任意数字(可重复写某个数字),然后,他叫同学随便写两 ...

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

  7. uva 227 Puzzle

     Puzzle  A children's puzzle that was popular 30 years ago consisted of a 5x5 frame which contained ...

  8. Uva227.Puzzle

    题目连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. 例题 3-5 谜题 uva227 Puzzle

    A children’s puzzle that was popular years ago consisted of a × frame which contained small squares ...

随机推荐

  1. luogu3469 [POI2008]BLO_Blockade

    题目大意 给一个无向连通图,求对于每一个点,去掉该点后图中连通结点有序对的减少量. 思路 当时想这道题时,我想到:枚举每一个点,在删去它后连通的几个部分中Dfs得到各个部分的点的个数从而得到解,但是我 ...

  2. java 语法 —— final

    final 成员变量的初始化问题: 1. 实现特别的约束和限制 指向一个 static 型私有成员(仅调用一次), public class Coffee { private static long ...

  3. gcd&&exgcd&&斐蜀定理

    gcd就是求a和b最大公约数,一般方法就是递推.不多说,上代码. 一.迭代法 int gcd(int m, int n) { ) { int c = n % m; n = m; m = c; } re ...

  4. 两个向量之间的欧式距离及radial-basis-functions(RBF)

    template <class DataType1, class DataType2>double EuclideanDistance(std::vector<DataType1&g ...

  5. Java 8 实战 P4 Beyond Java 8

    目录 Chapter 13. Thinking functionally Chapter 14. Functional programming techniques Chapter 15. compa ...

  6. BZOJ 4800 折半暴搜

    思路: 把它拆成两半  分别搜一发 两部分分别排好序 用two-pointers扫一遍 就可以了. (读入也要用long long) //By SiriusRen #include <cstdi ...

  7. MySQL数据库中的delete语句

    在MySQL数据库中,只有在数据存在的情况下删除,才会返回受影响的行数.比如大于0的数,如果删除了不存在的数据,则会返回0:

  8. C#之密封类(详解)

    10.3  密封类与密封方法 如果所有的类都可以被继承,那么很容易导致继承的滥用,进而使类的层次结构体系变得十分复杂,这样使得开发人员对类的理解和使用变得十分困难,为了避免滥用继承,C#中提出了密封类 ...

  9. 《CSS Mastery》读书笔记(4)

    第七章 布局   CSS得到一个不好的名声,比较难懂, 一方面由于浏览器的不兼容,另一方面由于网上大量的技巧,每个CSS作者都可以有自己的方法去创建多列布局, 新的CSS开发者只是使用一种方法而不去理 ...

  10. VHDL之code structure

     1 VHDL units VHDL code is composed of at least 3 fundamental sections: 1) LIBRARY declarations: Con ...