zoukankan      html  css  js  c++  java
  • ZOJ 3233 Lucky Number 容斥原理

    Lucky Number

    Time Limit: 5 Seconds      Memory Limit: 32768 KB

    Watashi loves M mm very much. One day, M mm gives Watashi a chance to choose a number between low and high, and if the choosen number is lucky, M mm will marry him.

    M mm has 2 sequences, the first one is BLN (Basic Lucky Numbers), and the second one is BUN (Basic Unlucky Numbers). She says that a number is lucky if it's divisible by at least one number from BLN and not divisible by at least one number from BUN.

    Obviously, Watashi doesn't know the numbers in these 2 sequences, and he asks M mm that how many lucky number are there in [lowhigh]?

    Please help M mm calculate it, meanwhile, tell Watashi what is the probability that M mm marries him.

    Input

    The first line of each test case contains the numbers NBLN (1 <= NBLN <= 15), NBUN (1 <= NBUN <= 500), lowhigh (1 <= low <= high <= 1018).

    The second and third line contain NBLN and NBUN integers, respectively. Each integer in sequences BLN and BUN is from interval [1, 32767].

    The last test case is followed by four zero.

    The input will contain no more than 50 test cases.

    Output

    For each test case output one number, the number of lucky number between low and high.

    Sample Input

    2 1 70 81
    2 3
    5
    0 0 0 0
    

    Sample Output

    5
    

    Hint

    The lucky numbers in the sample are 72, 74, 76, 78, 81.

    题意:

      给你两个数组,和一个范围[L,R],找出其中 能至少被A数组中一个数整除 和 不能被B数组中至少一个数整除 的数的个数

    题解:

      将第二个条件转化为,全被B数组中的数整除

      定义第一个条件为P1,第二个条件为P2。

      则要求的是P1&&P2。由于P2条件比较奇怪,至少不能被一个数整除,所以我们考虑其反命题,能被第二组中的所有数整除,则是能被最小公倍数整除。

      那么P1&&P2=P1-P1&&(~P2),这样便可以解决了。

    #include<bits/stdc++.h>
    using namespace std;
    #pragma comment(linker, "/STACK:102400000,102400000")
    #define ls i<<1
    #define rs ls | 1
    #define mid ((ll+rr)>>1)
    #define pii pair<int,int>
    #define MP make_pair
    typedef long long LL;
    const long long INF = 1e18+1LL;
    const double pi = acos(-1.0);
    const int N = 1e4+10, M = 1e3+20,inf = 2e9;
    const LL mod = 1e9+7LL;
    
    LL gcd(LL a, LL b) {
        return b ? gcd(b, a % b) : a;
    }
    LL a[N],b[N],l,r;
    LL solve(LL x,int n,int m) {
        if(x <= 0) return 0;
        LL ret = 0;
        for(int i = 1; i < (1<<n); ++i) {
            int num = 0;
            LL tmp = 1;
            int ok = 1;
            for(int j = 0; j < n; ++j) {
                 if((1<<j)&i) {
                    if(INF / a[j+1] < tmp/gcd(tmp,a[j+1])) {ok = 0;break;}
                    num++,tmp = tmp/gcd(tmp,a[j+1])*a[j+1];
                 }
            }
            if(!ok) continue;
            if(num & 1) {
                ret += x/tmp;
            }
            else ret -= x/tmp;
        }
        LL tmp1 = 1;
        for(int i = 1; i <= m; ++i) {
            if(INF / b[i] < tmp1/gcd(tmp1,b[i])) {
                tmp1 = -1;
                break;
            }
             tmp1 = tmp1/gcd(tmp1,b[i])*b[i];
        }
    
    
        for(int i = 1; i < (1<<n); ++i) {
            int num = 0;
            LL tmp = tmp1;
            if(tmp == -1) break;
            int ok = 1;
            for(int j = 0; j < n; ++j) {
                 if((1<<j)&i) {
                    if(INF / a[j+1] < tmp/gcd(tmp,a[j+1])) {ok = 0;break;}
                    num++,tmp = tmp/gcd(tmp,a[j+1])*a[j+1];
                 }
            }
            if(!ok) continue;
            if(num & 1) {
                ret -= x/tmp;
            }
            else ret += x/tmp;
        }
        return ret;
    }
    int n,m;
    int main() {
        while(scanf("%d%d%lld%lld",&n,&m,&l,&r)!=EOF) {
            if(!n&&!m&&!l&&!r) return 0;
            for(int i = 1; i <= n; ++i) scanf("%lld",&a[i]);
            for(int i = 1; i <= m; ++i) scanf("%lld",&b[i]);
            printf("%lld
    ",solve(r,n,m) - solve(l-1,n,m));
        }
        return 0;
    }
  • 相关阅读:
    复利计算器2.01
    复利计算器2.0
    0429团队3.0
    0428 团队项目合作2.0作业
    "数学口袋精灵"bug
    操作系统-实验2
    博客评论
    复利计算升级
    0408 结对合作
    0406复利计算5.0
  • 原文地址:https://www.cnblogs.com/zxhl/p/7265591.html
Copyright © 2011-2022 走看看