题意:从1....v这些数中找到c1个数不能被x整除,c2个数不能被y整除!
并且这c1个数和这c2个数没有相同的!给定c1, c2, x, y, 求最小的v的值!
思路: 二分+容斥,二分找到v的值,那么s1 = v/x是能被x整除的个数
s2 = v/y是能被y整除数的个数,s3 = v/lcm(x, y)是能被x,y的最小公倍数
整除的个数!
那么 v-s1>=c1 && v-s2>=c2 && v-s3>=c1+c2就是二分的条件!
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 using namespace std; 6 7 int gcd(int x, int y){ 8 return y==0 ? x : gcd(y, x%y); 9 } 10 11 int lcm(int x, int y){ 12 return x*y/gcd(x, y); 13 } 14 15 int main(){ 16 long long ld = 1, rd = 100000000000000ll, mid; 17 long long c1, c2, x, y; 18 cin>>c1>>c2>>x>>y; 19 while(ld <= rd){ 20 mid = (ld + rd)>>1; 21 long long s1 = mid/x, s2 = mid/y, s3 = mid/lcm(x, y); 22 if(mid-s1 >= c1 && mid-s2 >= c2 && mid-s3 >= c1+c2) rd = mid-1; 23 else ld = mid+1; 24 } 25 cout<<rd+1<<endl; 26 return 0; 27 }
时间: 2024-12-25 20:51:39