CodeForces - 483B Friends and Presents

对于一个范围里的数,分为四种。

1、是x和y的公倍数直接删掉,代码里用cnt记录公倍数个数

2、是x倍数不是y倍数的给第二个人

3、是y倍数不是x倍数的给第一个人

4、剩下的数随便给谁


每次迭代,上一次的总和再加上两个人一共还差多少人。直到符合条件为止。

最终迭代的结果就是答案。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <sstream>
using namespace std;
typedef long long LL;

LL n, m, x, y;

int judge(LL n, LL m, LL sum, LL &tmp)
{
    LL cnt=sum/(x*y);
    if(x==y)
        cnt=sum/x;
    n=max(0LL, n-(sum/y)+cnt);
    m=max(0LL, m-(sum/x)+cnt);
    if(sum-sum/x-sum/y+cnt>=n+m)
    {
        tmp=sum;
        return 1;
    }
    else
    {
        tmp=m+n-(sum-sum/x-sum/y+cnt);
        return 0;
    }
}

int main()
{
    while(cin>>n>>m>>x>>y)
    {
        LL sum=n+m, tmp;
        while(!judge(n, m, sum, tmp))
            sum+=tmp;
        cout<<sum<<endl;
    }
    return 0;
}


做这个题是在训练赛上,第一次写的代码传参里没有n和m。

函数直接对全局变量n和m修改。让我检查了半个多小时。


膝盖先跪给自己了......



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值