codeforces 483B Friends and Presents 解题报告
题目链接: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 解题报告的更多相关文章
- Codeforces Educational Round 92 赛后解题报告(A-G)
Codeforces Educational Round 92 赛后解题报告 惨 huayucaiji 惨 A. LCM Problem 赛前:A题嘛,总归简单的咯 赛后:A题这种**题居然想了20m ...
- codeforces 476C.Dreamoon and Sums 解题报告
题目链接:http://codeforces.com/problemset/problem/476/C 题目意思:给出两个数:a 和 b,要求算出 (x/b) / (x%b) == k,其中 k 的取 ...
- CodeForces 483B Friends and Presents
Friends and Presents Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I ...
- Codeforces Round #382 (Div. 2) 解题报告
CF一如既往在深夜举行,我也一如既往在周三上午的C++课上进行了virtual participation.这次div2的题目除了E题都水的一塌糊涂,参赛时的E题最后也没有几个参赛者AC,排名又成为了 ...
- Codeforces 483B - Friends and Presents(二分+容斥)
483B - Friends and Presents 思路:这个博客写的不错:http://www.cnblogs.com/windysai/p/4058235.html 代码: #include& ...
- codeforces 507B. Amr and Pins 解题报告
题目链接:http://codeforces.com/problemset/problem/507/B 题目意思:给出圆的半径,以及圆心坐标和最终圆心要到达的坐标位置.问最少步数是多少.移动见下图.( ...
- codeforces 500B.New Year Permutation 解题报告
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 ...
- codeforces B. Xenia and Ringroad 解题报告
题目链接:http://codeforces.com/problemset/problem/339/B 题目理解不难,这句是解题的关键 In order to complete the i-th ta ...
- codeforces 462C Appleman and Toastman 解题报告
题目链接:http://codeforces.com/problemset/problem/461/A 题目意思:给出一群由 n 个数组成的集合你,依次循环执行两种操作: (1)每次Toastman得 ...
随机推荐
- __name__和__main的含义
if __name__ == "__main__": main() This module represents the (otherwise anonymous) scope i ...
- 【poj3177】 Redundant Paths
http://poj.org/problem?id=3177 (题目链接) 题意 给出一个n个节点m条边的无向图,求最少连几条边使图中没有桥. Solution 我们可以发现,用最少的边使得图中没有桥 ...
- codevs1358 棋盘游戏
题目描述 Description 这个游戏在一个有10*10个格子的棋盘上进行,初始时棋子位于左上角,终点为右下角,棋盘上每个格子内有一个0到9的数字,每次棋子可以往右方或下方的相邻格子移动,求一条经 ...
- a[1000][1000]程序崩溃
1000 * 1000是大于65536的.如果不是需求需要,没必要开辟如此之多的空间.因为这些空间实在栈上申请的(如果是局部变量),栈的空间是有限的并且是宝贵的,所以呢,开辟太多的空间而不适用很可能会 ...
- EasyUI datagrid 格式化显示数据
http://blog.163.com/ppy2790@126/blog/static/103242241201512502532379/ 设置formatter属性,是一个函数,格式化函数有3个参数 ...
- PHP经典算法
php经典算法 .冒泡算法,排序算法,由于在排序过程中总是小数往前放,大数往后放,相当于气泡往上升,所以称作冒泡排序 $array = array(a,f,c,b,e,h,j,i,g); functi ...
- tp框架做留言板
首先是登录的LoginController.class.php 代码内容是 <?php namespace Admin\Controller; use Think\Controller; cla ...
- Servlet之Filter详细讲解
Filter,过滤器,顾名思义,即是对数据等的过滤,预处理过程.为什么要引入过滤器呢?在平常访问网站的时候,有时候发一些敏感的信息,发出后显示时 就会将敏感信息用*等字符替代,这就是用过滤器对信息进行 ...
- 锋利的jQuery-3--.css()获取和设置元素的数字属性
$('p').css({"fontSize": "30px", "backgroundColor": "#666"}); ...
- [LeetCode] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...