题目链接:http://codeforces.com/problemset/problem/483/B

题目意思:有两个 friends,需要将 cnt1 个不能整除 x 的数分给第一个friend,cnt2 个不能整除 y 的数分给第二个friend。x 和 y 都是素数来的。要求求出最小的 v,v 表示可以从1,2,...,v 中取数。

开始我做这条题的时候是用很常规的方法做的,结果可想而知,WA,MLE,TLE。只能看题解啦,不会嘛~~~题解真是非常清晰、明白、易懂。

等我用中文来解释下吧。要用到二分搜索!因为它符合一个条件,如果 v 这个数符合分配给两个人的所有条件,那么 v+1 就更加可以啦~~~所以二分是一个好选择,还有数据量太大啦,1e18 ! 正常做肯定超时!

首先给出一幅本人呕心沥血画的一幅东西:

设几个变量 f1,f2,both,others,f1',f2',v。

v:二分枚举的数,取值是 1 ~ 1e18,图中的全集也~~~

f1:能被 x 除尽的个数,v/f1

f2: 能被 y 除尽的个数,v/f2

both:同时被 x 和 y 除尽的个数。由于 x 和 y 都是素数,所以就相当于能被 x*y 整除。v/(x*y)

others: 既不能被 x 也不能被 y 整除的个数。v - f1 - f2 + both (容斥原理的精髓,both 被减了两次,所以最终要加回一次)

f1':能分配给 第二个人(除不尽 y)但又不是从others里面选择的数。f1' = f1 - both

f2': 能分配给 第一个人(除不尽 x)但又不是从others里面选择的数。f2' = f2 - both

然后给出的 cnt1 和 cnt2

cnt1 = f2' + others

cnt2 = f1' + others

那么最终要判断的是 cnt1 - f2' + cnt2 - f1' 是否  <= others 了。因为从 others 里面取的数都符合分给两个人的条件。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; typedef __int64 LL;
LL cnt1, cnt2, x, y; bool check(LL v)
{
LL f1 = v / x;
LL f2 = v / y;
LL both = v / (x*y);
LL others = v - f1 - f2 + both;
LL ff1 = f1 - both; // second
LL ff2 = f2 - both; // first LL gf1 = (cnt1 - ff2 >= ? cnt1 - ff2 : ); // 注意这个相减有可能为负数,所以要判断下
LL gf2 = (cnt2 - ff1 >= ? cnt2 - ff1 : ); return (gf1 + gf2 <= others);
} int main()
{
while (scanf("%I64d%I64d%I64d%I64d", &cnt1, &cnt2, &x, &y) != EOF)
{
LL l = , r = 1e18;
while (l < r)
{
LL m = (l+r) >> ;
if (check(m))
r = m;
else
l = m + ;
}
printf("%I64d\n", r);
}
return ;
}

codeforces 483B Friends and Presents 解题报告的更多相关文章

  1. Codeforces Educational Round 92 赛后解题报告(A-G)

    Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...

  2. codeforces 476C.Dreamoon and Sums 解题报告

    题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...

  3. CodeForces 483B Friends and Presents

     Friends and Presents Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  4. Codeforces Round #382 (Div. 2) 解题报告

    CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...

  5. Codeforces 483B - Friends and Presents(二分+容斥)

    483B - Friends and Presents 思路:这个博客写的不错:http://www.cnblogs.com/windysai/p/4058235.html 代码: #include& ...

  6. codeforces 507B. Amr and Pins 解题报告

    题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...

  7. codeforces 500B.New Year Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...

  8. codeforces B. Xenia and Ringroad 解题报告

    题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...

  9. codeforces 462C Appleman and Toastman 解题报告

    题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...

随机推荐

  1. ubuntu14.10建立热点wifi分享给手机

    http://jingyan.baidu.com/article/363872ecd8f35d6e4ba16f97.html ubuntu14.10建立热点wifi分享给手机

  2. POJ 3273 Monthly Expense

    传送门 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John is an astounding accounting wiza ...

  3. Nutch2.x 演示抓取第一个网站

    http://www.micmiu.com/opensource/nutch/nutch2x-crawl-first-website/?utm_source=tuicool&utm_mediu ...

  4. JDK,JRE,JVM区别与联系(ZZ)

    http://www.cnblogs.com/hencehong/p/3252166.html 我们开发的实际情况是:我们利用JDK(调用JAVA API)开发了属于我们自己的JAVA程序后,通过JD ...

  5. ExtJS入门教程04,这是一个超级好用的grid

    今天进行extjs入门教程的第四篇:grid. 来一份grid尝尝 小伙伴们都知道extjs的grid功能强大,更清楚功能强大的东西用起来必然会复杂.今天我们就从最简单的grid开始讲解. 先来一个最 ...

  6. js中数组以及for循环的使用

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> < ...

  7. rockmongo用法

    .简单查询 //xid=560870 and type=video { , "type": "video" } //查询数组中的数据 array( " ...

  8. vc++ 6.0下Glut的配置 及 Glut 框架介绍

    2014-04-08  16:18:30 一.配置Glut 学习来源: http://blog.sina.com.cn/s/blog_5f0cf7bd0100c9oa.html 亲测可行. Glut的 ...

  9. Python socket编程之二:【struct.pack】&【struct.unpack】

    import struct """通过 socket 的 send 和 recv 只能传输 str 格式的数据""" "" ...

  10. Web Service 元数据注释(JSR 181)

    Web Service 元数据注释(JSR 181) @WebService 1.serviceName: 对外发布的服务名,指定 Web Service 的服务名称:wsdl:service.缺省值 ...