zoukankan      html  css  js  c++  java
  • ZOJ 3233 Lucky Number

    Lucky Number

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

    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.

     

    Source

    Author

    OUYANG, Jialin
     
    解题:容斥求给出的区间$[lowdots high]$中有多少个数,在A序列至少有一个是他的因子,在B序列里至少有一个不是他的因子。
     
    我们可以先求出在A序列中至少有一个是他的因子的数的个数,减去至少有一个是他的因子的数的个数,且B里面的数都是他的因子的数的个数
     
    判q < 0 是判断是否溢出
     
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 const int maxn = 1010;
     5 LL a[maxn],b[maxn],low,high,n,m;
     6 LL LCM(LL a,LL b){
     7     return a/__gcd(a,b)*b;
     8 }
     9 int main(){
    10     ios::sync_with_stdio(false);
    11     cin.tie(0);
    12     while(cin>>n>>m>>low>>high,n||m||low||high){
    13         for(int i = 0; i < n; ++i) cin>>a[i];
    14         LL q = 1;
    15         for(int j = 0; j < m; ++j){
    16             cin>>b[j];
    17             q = LCM(q,b[j]);
    18         }
    19         LL ret = 0;
    20         for(int i = 1; i < (1<<n); ++i){
    21             LL p = 1;
    22             int cnt = 0;
    23             for(int j = 0; j < n; ++j){
    24                 if((i>>j)&1){
    25                     cnt++;
    26                     p = LCM(p,a[j]);
    27                 }
    28             }
    29             if(q < 0){
    30                 if(cnt&1) ret += high/p - (low-1)/p;
    31                 else ret -= high/p - (low - 1)/p;
    32                 continue;
    33             }
    34             LL s = LCM(p,q);
    35             if(cnt&1) ret += high/p - (low - 1)/p - (high/s - (low-1)/s);
    36             else ret -= high/p - (low - 1)/p - (high/s - (low-1)/s);
    37         }
    38         printf("%lld
    ",ret);
    39     }
    40     return 0;
    41 }
    View Code
  • 相关阅读:
    Ninject Lazy Load问题
    log4net 极简配置
    log4net 使用指南,最常遇到的问题整理。。。
    【应聘】阿里巴巴Java面试题目
    【Unity3D】自动寻路(Nav Mesh Agent组件)
    【unity3D】鼠标控制camera的移动、放大(俯视浏览效果、LOL摄像机移动效果)
    【Unity3D】枪战游戏—弹孔设置
    【Unity3D】枪战游戏—发射子弹、射线检测
    【Unity3D】Unity自带组件—完成第一人称人物控制
    软件工程【第5章】- 需求工程与需求设计
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4820368.html
Copyright © 2011-2022 走看看