对于一个范围里的数,分为四种。
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修改。让我检查了半个多小时。
膝盖先跪给自己了......