ZOJ 3233 Lucky Number(容斥原理)
2015-10-21 15:25
295 查看
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 [low,
high]?
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),
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 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
Sample Output
Hint
The lucky numbers in the sample are 72, 74, 76, 78, 81.
题意:询问[low,high]中,A集合存在一个数是它的约数,且B集合至少存在一个数不是它的约数的个数。
题解:容斥原理。假设B集合中的所有数都是约数,则满足答案的[low,high]之间的数一定可以整除B中所有数的lcm->bm
这样我们只需要在用容斥原理求[low,high]满足A集合条件的x的时候,减去[low,high]能整除lcm(x,bm)的个数。
求满足[low,high]中A存在存在一个数是他的约数,把每个数当做一个因子,再容斥。
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <bitset>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 16;
const ll inf=1e18+10;
ll l,r;
int n,m;
int a
,b
;
ll bm;
vector<int>vec;
ll gcd(ll a,ll b) {
return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b) {
if(a==-1||b==-1) return -1;
ll gd=gcd(a,b);
if(inf/b<a/gd) return -1;
return a/gd*b;
}
void init(int x) {
vec.clear();
for(int i=2; i*i<=x; i++) {
if(x%i==0) {
vec.push_back(i);
while(x%i==0)x/=i;
}
}
if(x>1)vec.push_back(x);
}
void solve() {
ll sum=0;
for(int i=1; i<(1<<n); i++) {
ll lc=1;
int cnt=0;
for(int j=0; j<n; j++) {
if(i&(1<<j)) {
lc=lcm(lc,a[j]);
cnt++;
}
}
if(lc==-1)lc=inf;
ll tmp=lcm(bm,lc);
if(tmp==-1) tmp=inf;
ll num=(r/lc-r/tmp)-(l/lc-l/tmp);
if(cnt&1)sum+=num;
else sum-=num;
}
printf("%lld\n",sum);
}
int main() {
//freopen("test.in","r",stdin);
while(~scanf("%d%d%lld%lld",&n,&m,&l,&r)) {
if(n==0&&m==0&&l==0&&r==0)break;
l--;
for(int i=0; i<n; i++)scanf("%d",&a[i]);
bm=1;
for(int i=0; i<m; i++) {
ll x;
scanf("%lld",&x);
bm=lcm(bm,x);
}
solve();
}
return 0;
}
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 [low,
high]?
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),
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 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.
题意:询问[low,high]中,A集合存在一个数是它的约数,且B集合至少存在一个数不是它的约数的个数。
题解:容斥原理。假设B集合中的所有数都是约数,则满足答案的[low,high]之间的数一定可以整除B中所有数的lcm->bm
这样我们只需要在用容斥原理求[low,high]满足A集合条件的x的时候,减去[low,high]能整除lcm(x,bm)的个数。
求满足[low,high]中A存在存在一个数是他的约数,把每个数当做一个因子,再容斥。
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <bitset>
#include <vector>
using namespace std;
typedef long long ll;
const int N = 16;
const ll inf=1e18+10;
ll l,r;
int n,m;
int a
,b
;
ll bm;
vector<int>vec;
ll gcd(ll a,ll b) {
return b==0?a:gcd(b,a%b);
}
ll lcm(ll a,ll b) {
if(a==-1||b==-1) return -1;
ll gd=gcd(a,b);
if(inf/b<a/gd) return -1;
return a/gd*b;
}
void init(int x) {
vec.clear();
for(int i=2; i*i<=x; i++) {
if(x%i==0) {
vec.push_back(i);
while(x%i==0)x/=i;
}
}
if(x>1)vec.push_back(x);
}
void solve() {
ll sum=0;
for(int i=1; i<(1<<n); i++) {
ll lc=1;
int cnt=0;
for(int j=0; j<n; j++) {
if(i&(1<<j)) {
lc=lcm(lc,a[j]);
cnt++;
}
}
if(lc==-1)lc=inf;
ll tmp=lcm(bm,lc);
if(tmp==-1) tmp=inf;
ll num=(r/lc-r/tmp)-(l/lc-l/tmp);
if(cnt&1)sum+=num;
else sum-=num;
}
printf("%lld\n",sum);
}
int main() {
//freopen("test.in","r",stdin);
while(~scanf("%d%d%lld%lld",&n,&m,&l,&r)) {
if(n==0&&m==0&&l==0&&r==0)break;
l--;
for(int i=0; i<n; i++)scanf("%d",&a[i]);
bm=1;
for(int i=0; i<m; i++) {
ll x;
scanf("%lld",&x);
bm=lcm(bm,x);
}
solve();
}
return 0;
}
相关文章推荐
- Codeforces Round #198 (Div. 1)
- 4495: Least Prime factor 找到最小质因子P的第N小正整数
- hdu5072 Coprime 2014鞍山现场赛C题 容斥原理+单色三角
- 二维树状数组
- HDU 1695 GCD
- ZOJ 3547 《The Boss on Mars》(容斥定理)
- USACO 2.3.2 Cow Pedigrees
- USACO 2.3.2 Cow Pedigrees
- hdu1695 综合数论 欧拉函数 分解质因子 容斥原理 打印素数表 各种模板
- 1284 2 3 5 7的倍数
- hdu1695 GCD(容斥原理+欧拉函数)
- hdu1796(容斥原理)
- HDU 4135 Co-prime(容斥原理求互质数)
- hdu1796--How many integers can you find--容斥原理
- HDU 4135&HDU 4407--容斥原理--质因子分解
- HDU 5468 Puzzled Elena (2015年上海赛区网络赛A题)
- HDU 1796 容斥原理
- HDU 4407
- hdu 5212(容斥原理)
- ZOJ 3868(容斥原理+快速幂)