B. Friends and Presents(Codeforces Round #275(div2)

3470 단어 codeforces
B. Friends and Presents
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You have two friends. You want to present each of them several positive integers. You want to present cnt1 numbers to the first friend andcnt2 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 test(s)
input
3 1 2 3

output
5

input
1 3 2 3

output
4

Note
In the first sample you give the set of numbers {1, 3, 5} to the first friend and the set of numbers {2} to the second friend. Note that if you give set {1, 3, 5} to the first friend, then we cannot give any of the numbers 1, 3, 5 to the second friend.
In the second sample you give the set of numbers {3} to the first friend, and the set of numbers {1, 2, 4} to the second friend. Thus, the answer to the problem is 4.
이분 찌꺼기는 이분을 또 무릎을 꿇고 l과 r의 관계 o(╯□╰)o를 구분하지 못한다. 나는 l와 r를 모두 추측해 보았다.이 문제는 뜻밖에도 l과 r까지 통과할 수 있다.
이 문제는 중간 결과를 매거해 보면 a주기가 x-1이고 b주기가 y-1이다. i가 y의 배수일 때만 a로 할 수 있고 i가 x의 배수일 때만
b로 하여금 x, y의 배수를 계산할 수 있을 때 두 개 모두 얻을 수 없다. a의 총 수량을 빼면 a만 얻을 수 있고, b의 총 수량을 빼면 b만 얻을 수 있다. 나머지는
취한 것과 작은 것은 두 개 모두 얻을 수 있는데, 이 값이 바로 유효값이다.
코드:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
long long cnt1,cnt2,x,y;
long long gcd(long long a,long long b)
{
    return b==0?a:gcd(b,a%b);
}
bool judge(long long v)
{
    long long t1,t2,t3;
    t1=v/x;
    t2=v/y;
    t3=v/(x*y/gcd(x,y));
    long long temp1,temp2,temp3;
    temp1=max((long long)0,cnt1-t2+t3);//t2-t3    a  ,cnt1-   a  ,    a   
    temp2=max((long long)0,cnt2-t1+t3);//t1-t3    b  ,cnt2-   b  ,    b   
    temp3=max((long long)0,v-t1-t2+t3);//x,y    
    if(temp3>=temp1+temp2)//a,b           a,b    
    return true;
    else
    return false;
}
int main()
{
    scanf("%I64d%I64d%I64d%I64d",&cnt1,&cnt2,&x,&y);
    long long l=1,r=2000000000;
    long long u=0;
    while(l<r)
    {
        int m=(l+r)>>1;
        if(judge(m))
        {
            u=m;
            r=m;
        }
        else
        {
            l=m+1;
        }
    }
   // long long ans;
   // if(judge(r))
   // ans=r;
 //   else
  //  ans=l;
    printf("%I64d
",u); return 0; }

좋은 웹페이지 즐겨찾기