CodeForces 483B Friends and Presents
2015-08-30 20:18
323 查看
Friends and Presents
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 483B
Description
You have two friends. You want to present each of them several positive integers. You want to present cnt1 numbers to the first friend and cnt2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
Input
The only line contains four positive integers cnt1, cnt2, x, y (1 ≤ cnt1, cnt2 < 109; cnt1 + cnt2 ≤ 109; 2 ≤ x < y ≤ 3·104) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
Output
Print a single integer — the answer to the problem.
Sample Input
Input
Output
Input
Output
View Code
Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 483B
Description
You have two friends. You want to present each of them several positive integers. You want to present cnt1 numbers to the first friend and cnt2 numbers to the second friend. Moreover, you want all presented numbers to be distinct, that also means that no number should be presented to both friends.
In addition, the first friend does not like the numbers that are divisible without remainder by prime number x. The second one does not like the numbers that are divisible without remainder by prime number y. Of course, you're not going to present your friends numbers they don't like.
Your task is to find such minimum number v, that you can form presents using numbers from a set 1, 2, ..., v. Of course you may choose not to present some numbers at all.
A positive integer number greater than 1 is called prime if it has no positive divisors other than 1 and itself.
Input
The only line contains four positive integers cnt1, cnt2, x, y (1 ≤ cnt1, cnt2 < 109; cnt1 + cnt2 ≤ 109; 2 ≤ x < y ≤ 3·104) — the numbers that are described in the statement. It is guaranteed that numbers x, y are prime.
Output
Print a single integer — the answer to the problem.
Sample Input
Input
3 1 2 3
Output
5
Input
1 3 2 3
Output
4
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; LL cnt1, cnt2, x, y; bool check(LL v) { LL f1,f2,both,others,ff1,ff2,gf1,gf2; f1 = v / x; f2 = v / y; both = v / (x*y); others = v - f1 - f2 + both; ff1 = f1 - both; ff2 = f2 - both; gf1 = (cnt1 - ff2 >= 0 ? cnt1 - ff2 : 0); gf2 = (cnt2 - ff1 >= 0 ? cnt2 - ff1 : 0); return (gf1 + gf2 <= others); } int main() { while (scanf("%I64d%I64d%I64d%I64d", &cnt1, &cnt2, &x, &y) != EOF) { LL l=1, r=1e18; while (l<r) { LL m=(l+r)/2; if (check(m)) r=m; else l=m+1; } printf("%I64d\n",r); } return 0; }
View Code
相关文章推荐
- Android开发详解之onTouch和onClick详解
- poj 2010 Moo University - Financial Aid(优先队列(最小堆)+ 贪心 + 枚举)
- CodeForces 490C Hacking Cypher
- virtualbox谨记:win7上只有4.3.x的版本支持ubuntu14.04.3虚拟机安装Oracle Rac,其他的版本3.x和5.0.2(至2015-08-30)均不可以
- linux 磁盘管理工具
- C语言基础知识之(十五):指针和函数
- java并发编程第六章(3)使用阻塞式线程安全列表
- ALTERA DE2 之 verilog HDL 学习笔记01 程序的并行
- Windows 多进程通信API总结
- .NET六大剑客:栈、堆、值类型、引用类型、装箱和拆箱
- 字符串最后一个单词的长度(华为在线训练)
- CodeForces 483C Diverse Permutation
- android-屏幕适配(一)
- Alien Order
- Linux进程间通信---有名管道 代码实现
- RESTEasy常用注解
- CodeForces 478C Table Decorations
- C语言基础知识之(十四):指针和字符数组、字符串数组
- CodeForces #318 (div1) B.Bear and Blocks
- Ubuntu 10.04 速配指南