codeforces B. Friends and Presents(二分+容斥)
2014-10-25 12:58
465 查看
题意:从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就是二分的条件!
View Code
并且这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就是二分的条件!
#include<iostream> #include<cstring> #include<cstdio> #include<algorithm> using namespace std; int gcd(int x, int y){ return y==0 ? x : gcd(y, x%y); } int lcm(int x, int y){ return x*y/gcd(x, y); } int main(){ long long ld = 1, rd = 100000000000000ll, mid; long long c1, c2, x, y; cin>>c1>>c2>>x>>y; while(ld <= rd){ mid = (ld + rd)>>1; long long s1 = mid/x, s2 = mid/y, s3 = mid/lcm(x, y); if(mid-s1 >= c1 && mid-s2 >= c2 && mid-s3 >= c1+c2) rd = mid-1; else ld = mid+1; } cout<<rd+1<<endl; return 0; }
View Code
相关文章推荐
- Codeforces Round #275 (Div. 2) B. Friends and Presents 二分+数学
- Codeforces Round #275 (Div. 2) --B Friends and Presents
- Codeforces Round #394 (Div. 2) B. Dasha and friends 暴力
- 【Codeforces Round 324 (Div 2)B】【容斥】Kolya and Tanya 环上n个3元组至少有一组和不为6
- Professor GukiZ and Two Arrays CodeForces - 620D (二分好题)
- Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays 二分
- Codeforces Round #404 (Div. 2) D. Anton and School - 2 前缀的后缀、 范德蒙恒等式、容斥
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
- 【贪心+二分】codeforces C. Sagheer and Nubian Market
- Codeforces Round #299 (Div. 1) A. Tavas and Karafs(二分)
- 【Educational Codeforces Round 3 D】【二分答案 贪心排序】Gadgets for dollars and pounds m个物品n天价格买k个的最早天数
- Codeforces Educational Codeforces Round 3 D. Gadgets for dollars and pounds(二分 + 贪心)
- Codeforces Round #404 (Div. 2) -- C. Anton and Fairy Tale(二分)
- Educational Codeforces Round 3 D.Gadgets for dollars and pounds(贪心&&二分)
- Educational Codeforces Round 6 D. Professor GukiZ and Two Arrays(二分 | two pointers)
- Codeforces483B——二分——Friends and Presents
- Educational Codeforces Round 3 D. Gadgets for dollars and pounds 二分+前缀
- Codeforces Round #330 (Div. 2)D. Max and Bike 二分 物理
- Codeforces Round #227 (Div. 2) E. George and Cards set内二分+树状数组
- Vasya and String CodeForces - 676C [二分查找 + 尺取法]