题解 CF787A 【The Monster】
这道题和数论似乎有些关系,但我怎么想也想不出公式(我才小学5年级),于是只好模拟了,两层循环,代码如下,707ms:
#include <iostream>
using namespace std;
int main()
{
int a, b, c, d;
cin >> a >> b >> c >> d;
int x1 = b, x2 = d;
for(int i = 0; i <= 105; i++)
{
x1 = b + a * i;
for(int j = 0; j <= 105; j++)//注意循环从0开始,因为可能b和d相等,从1开始的话第一人第一次的b和第二人第一次的d就没了……
{
x2 = d + c * j;
if(x1 == x2)
{
cout << x1 << endl;
return 0;
}
}
}
cout << "-1\n";
return 0;
}

浙公网安备 33010602011771号