• hdu 1796(容斥原理+状态压缩)


    How many integers can you find

    Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 6630    Accepted Submission(s): 1913


    Problem Description
      Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer set is {2,3}, so there is another set {2,3,4,6,8,9,10}, all the integers of the set can be divided exactly by 2 or 3. As a result, you just output the number 7.
     
    Input
      There are a lot of cases. For each case, the first line contains two integers N and M. The follow line contains the M integers, and all of them are different from each other. 0<N<2^31,0<M<=10, and the M integer are non-negative and won’t exceed 20.
     
    Output
      For each case, output the number.
     
    Sample Input
    12 2 2 3
     
    Sample Output
    7
     
    题意:在 1-(n-1) 中能够被输入的数字整除的数字的数量。
    思路:容斥原理+枚举状态,碰到奇数加上(n-1)/lcm(a,b,c..) 碰到偶数减(n-1)/lcm(a,b,c...) 注意0不能取,发现本人一直不是很会用深搜,所以还是用状压了 = =
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    #include <stdlib.h>
    #include <math.h>
    using namespace std;
    int gcd(int a,int b){
        return b==0?a:gcd(b,a%b);
    }
    int lcm(int a,int b){
        return a/gcd(a,b)*b;
    }
    int main()
    {
        int n,m,a[15];
        while(scanf("%d%d",&n,&m)!=EOF){
            int id = 0,num;
            for(int i=0;i<m;i++){
                scanf("%d",&num);
                if(num!=0) a[id++] = num;
            }
            int ans = 0;
            for(int i=1;i<(1<<id);i++){
                int l=1,cnt=0;
                for(int j=0;j<id;j++){
                    if((i>>j)&1){
                        cnt++;
                        l = lcm(l,a[j]);
                    }
                }
                if(cnt&1){
                    ans+=(n-1)/l; ///不包括自身所以n-1
                }else{
                    ans-=(n-1)/l;
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    deepinmind(转)
    不知道数据库中表的列类型的前提下,使用JDBC正确的取出数据(转)
    shell 监控局域网的主机是否up(转)
    IntelliJ Idea中一个编译报错引发的
    Unity插件之NGUI学习(8)—— Table和NGUI尺寸转换为世界坐标系尺寸
    使用php-fpm状态页观察当前的php-fpm状态
    PHP连接Access数据库代码
    HDU 4107 线段树
    Effective C++之‘宁以pass-by-reference-to-const替换pass-by-value’
    xdebug的安装和配置方法
  • 原文地址:https://www.cnblogs.com/liyinggang/p/5539828.html
Copyright © 2020-2023  润新知