zoukankan      html  css  js  c++  java
  • ZOJ 2836 Number Puzzle

    Number Puzzle

    Time Limit: 2000ms
    Memory Limit: 65536KB
    This problem will be judged on ZJU. Original ID: 2836
    64-bit integer IO format: %lld      Java class name: Main
     

    Given a list of integers ($A_1, A_2, ..., A_n$), 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 leq N leq 10$) and $M (1 leq M leq 200000000)$, and the second line contains $A_1, A_2, ..., A_n(1 leq A_i leq 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

     

    Source

    Author

    MAO, Yiqiang
     
    解题:容斥,求是集合中某些数的LCM的倍数且不超过m的数的数量
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 20;
     5 int a[maxn],n,m;
     6 LL gcd(LL a,LL b) {
     7     return b?gcd(b,a%b):a;
     8 }
     9 LL LCM(LL a,LL b) {
    10     return a/gcd(a,b)*b;
    11 }
    12 int main() {
    13     while(~scanf("%d%d",&n,&m)) {
    14         for(int i = 0; i < n; ++i)
    15             scanf("%d",a+i);
    16         LL ret = 0;
    17         for(int i = 1; i < (1<<n); ++i) {
    18             int cnt = 0;
    19             LL x = 1;
    20             for(int j = 0; j < n; ++j) {
    21                 if((i>>j)&1) {
    22                     x = LCM(x,a[j]);
    23                     cnt++;
    24                 }
    25             }
    26             if(cnt&1) ret += m/x;
    27             else ret -= m/x;
    28         }
    29         printf("%lld
    ",ret);
    30     }
    31     return 0;
    32 }
    View Code
  • 相关阅读:
    查找大文件的命令
    JavaScript对象参考手册
    Thymeleaf(Java模板引擎)
    C#基础语法补充
    mysql 拾遗提高(函数、事务、索引)
    jQuery总结
    Hibernate (开源对象关系映射框架)
    git的使用命令
    Xpath,XQuery,DTD
    XML DOM(Document Object Model)
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4820218.html
Copyright © 2011-2022 走看看