【ZOJ】3233【容斥原理】【Lucky Number】

原创 2013年12月03日 11:13:09

Lucky Number
——————————————————————————–

Time Limit: 5Seconds     Memory Limit: 32768 KB
——————————————————————————–

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

M mm has 2 sequences, the first one is BLN (Basic LuckyNumbers), and the second one is BUN (Basic Unlucky Numbers). Shesays that a number is lucky if it’s divisible by at least onenumber from BLN and not divisible by at least one number fromBUN.

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

Please help M mm calculate it, meanwhile, tell Watashi what isthe 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), low, high (1<= low <= high <=1018).

The second and third line contain NBLN and NBUN integers,respectively. Each integer in sequences BLN and BUN is frominterval [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 numberbetween 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.

解题思路:

  命题A:至少能被BLN中的一个数整除

  命题B:至少不能被BUN中的一个数整除

 所以A&&B=A-A&&(~B);

题目就是求[low,high]中符合A&&B的数的个数,用容斥原理求出符合A的个数并减去符合A&&(~B)的个数;

求出BUN中所有数的最小公倍数p,

递归从BLN中取出k个数,求他们的最小公倍数g,

求出p和g的最小公倍数s;

当k为奇数:ans+=high/g-high/s-((low-1)/g-(low-1)/s);

当k为偶数:ans-=high/g-high/s-((low-1)/g-(low-1)/s);(容斥原理)

ans就是最后结果;

时间复杂度:2^15

#include<stdio.h>
#define BG 1000000000000000000LL

#define long_long long long
long_long ans,l,h,p,n;
long_long a[18],b[505];
long_long gcd(long_long a,long_long b)
{
      if (b==0) return a;
     else return gcd(b,a%b);
}
long_long calc(long_long g,long_long k,long_long t)
{
        long_long gg,s;
      if (t==n+1)
       {
         if (k!=0)
         {
                 s=gcd(p,g);
                 if (p/s>BG/g) s=h+1;
                       else
                              s=p/s*g;
                    if (k%2==0)
                               ans-=h/g-h/(s)-((l-1)/g-(l-1)/(s));
                 else
                              ans+=h/g-h/(s)-((l-1)/g-(l-1)/(s));
         }
         return 0;
 }
 gg=gcd(g,a[t]);
     if (g/gg>BG/a[t]) gg=h+1;
  else
              gg=g/gg*a[t];
       calc(gg,k+1,t+1);
   calc(g,k,t+1);
}
int main()
{
    long_long m,i,k;
     while(scanf("%lld%lld%lld%lld",&n,&m,&l,&h)!=EOF)
 {
         if ((n==0)&&(m==0)&&(l==0)&&(h==0)) break;
              for(i=1;i<=n;i++)
                  scanf("%lld",&a[i]);
                for(i=1;i<=m;i++)
                  scanf("%lld",&b[i]);
                p=b[1];
             for(i=2;i<=m;i++)
          {
                 k=gcd(p,b[i]);
                      if (p/k>BG/b[i]) p=h+1;
                    else
                              p=p/k*b[i];
         }
         ans=0;
              calc(1,0,1);
                printf("%lld\n",ans);
  }
}


姐华丽丽的没做出来,就找了大牛写的,嘿嘿;做人要厚道,贴一下那位大牛的地址吧:

http://acm.nbu.edu.cn/SwordHoly/?p=54

ZOJ 3233 Lucky Number(容斥原理)

Lucky Number Time Limit: 5 Seconds      Memory Limit: 32768 KB Watashi loves M mm very mu...

ZJU 3233 - Lucky Number (容斥原理)

题意 找出[low, high]之间数的数量,符合下面两个条件。 至少被一个lucky数整除。至少不被一个unLucky数整除。 思路 大思路是用容斥来统计。 ...

题目1380:lucky number

题目1380:lucky number 题目思路分析以及详细的解析 题目链接地址: http://ac.jobdu.com/problem.php?pid=1380 第一次这么正式的去写这个ACM的...

hdu 4937 Lucky Number 科学暴力

Lucky Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) ...

HDU 4937 Lucky Number 【搜索】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4937 搜索 机智的暴力

LightOJ 1097 - Lucky Number [线段树二分前缀和构造数列]

1097 - Lucky Number PDF (English) Statistics Forum Time Limit: 2 secon...

Codeforces 110A-Nearly Lucky Number(实现)

A. Nearly Lucky Number time limit per test 2 seconds memory limit per test 256 megabytes...

HDU-4937-Lucky Number

这个题比赛的时候没做出来,本来以为枚举时间不多(我开始用的DFS+各种

FOJ 2071 lucky number

定义了lucky number. 就是一个数的任意从左到右选三个数字a,b,c。 若ab>c或者a,b,c三者中存在相同数字,则不是lucky number。 可以证明大于9999的数中不存在l...
  • sqplfh
  • sqplfh
  • 2011年12月12日 00:52
  • 414

【HDU4937】Lucky Number(模拟)

比较简单的题。。。又没做出来T^T
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【ZOJ】3233【容斥原理】【Lucky Number】
举报原因:
原因补充:

(最多只允许输入30个字)