题目
样例
这题本来用的bfs写了150多行,太长了,结果查了一下数论的做法,结果只要20行就做好了,会数学的果然都是大佬啊!
话不多说直接上数论的代码,再转载一下解释
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int main()
{
int s, n, m;
while (cin >> s >> n >> m, s + m + n){
s /= gcd(n, m);
if (s & 1)
cout << "NO" << endl;
else
cout << s - 1 << endl;
}
return 0;
}
下面是转载的解释: