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;

代码:

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cmath>
  5. #include <algorithm>
  6. #include <climits>
  7. #include <cstring>
  8. #include <string>
  9. #include <set>
  10. #include <bitset>
  11. #include <map>
  12. #include <queue>
  13. #include <stack>
  14. #include <vector>
  15. #define rep(i,m,n) for(i=m;i<=n;i++)
  16. #define mod 1000000007
  17. #define inf 0x3f3f3f3f
  18. #define vi vector<int>
  19. #define pb push_back
  20. #define mp make_pair
  21. #define fi first
  22. #define se second
  23. #define ll long long
  24. #define pi acos(-1.0)
  25. #define pii pair<int,int>
  26. #define sys system("pause")
  27. const int maxn=1e5+;
  28. using namespace std;
  29. inline ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
  30. inline ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
  31. inline void umax(ll &p,ll q){if(p<q)p=q;}
  32. inline void umin(ll &p,ll q){if(p>q)p=q;}
  33. inline ll read()
  34. {
  35. ll x=;int f=;char ch=getchar();
  36. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  37. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  38. return x*f;
  39. }
  40. int n,m,k,t,fac[];
  41. int main()
  42. {
  43. int i,j;
  44. while(~scanf("%d%d",&n,&m))
  45. {
  46. rep(i,,n-)scanf("%d",&fac[i]);
  47. int ret=;
  48. rep(i,,(<<n)-)
  49. {
  50. int now=,cnt=;
  51. rep(j,,n-)
  52. {
  53. if(i&(<<j))
  54. {
  55. cnt++;
  56. now=now*fac[j]/gcd(now,fac[j]);
  57. }
  58. }
  59. if(cnt&)ret+=m/now;
  60. else ret-=m/now;
  61. }
  62. printf("%d\n",ret);
  63. }
  64. return ;
  65. }

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. Flume 读取JMS 消息队列消息,并将消息写入HDFS

    利用Apache Flume 读取JMS 消息队列消息.并将消息写入HDFS,flume agent配置例如以下: flume-agent.conf #name the  components on ...

  2. C# 正则表达式 和 JAVA表达式是想通的

    正则表达式语法 也许有人会说,现在需要正则表达式去验证什么的话,直接在网上找不久一大片吗?还需要学什么啊! 是的,现在在网上找确实是一找一大片,但是,有时候我们也遇到这样的情况,就是我们在网上找的复制 ...

  3. kafka的topic和分区策略——log entry和消息id索引文件

    Topic在逻辑上可以被认为是一个在的queue,每条消费都必须指定它的topic,可以简单理解为必须指明把这条消息放进哪个queue里. 为了使得Kafka的吞吐率可以水平扩展,物理上把topic分 ...

  4. ubuntu 12.10 禁用触摸板

    1. 打开终端,输入 sudo rmmod psmouse 禁用触摸板,输入 sudo modprobe psmouse 恢复触摸板 2.syndaemon -i 10 -d >/dev/nul ...

  5. leetcode数组相关

    目录 4寻找两个有序数组的中位数 11盛最多水的容器,42接雨水 15三数之和,16最接近的三数之和,18四数之和 26/80删除排序数组中的重复项, 27移除元素 31下一个排列 53最大子序和 5 ...

  6. XML案例(使用JAXP进行SAX解析)

    1.Book.java package cn.itcast.sax; public class Book { private String name; private String author; p ...

  7. 多文件上传ajax jquery

    jquery的ajaxSubmit()和多文件上传 <%@ page language="java" import="java.util.*" pageE ...

  8. Spring Boot (19) servlet、filter、listener

    servlet.filter.listener,在spring boot中配置方式有两种:一种是以servlet3开始提供的注解方式,另一种是spring的注入方式. servlet注解方式 serv ...

  9. NoSQL概念

    NoSQL是非关系型数据库,即not only sql,key/value键值对存储. 现有Nosql DB产品:Redis/MongoDB/Memcached等等. SQL Sever是关系型数据库 ...

  10. Jquery 获取父页面下指定iframe里的指定元素

    var div1=$("#iframe1",window.parent.document).contents().find("#div1");