QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#843133 | #9868. GCD | Monika | RE | 0ms | 0kb | C++17 | 1.2kb | 2025-01-04 16:57:08 | 2025-01-04 16:57:11 |
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(a,b,c) for (a = b; a <= c; a ++)
#define rrep(a,c,b) for (a = c; a >= b; a --)
#define endl '\n'
typedef pair<int,int> pii;
const int N = 5005;
const int inf = 1e9 + 10;
int g[N][N];
int a, b;
void solve(){
cin >> a >> b;
int i, j;
rep(i,0,a) rep(j,0,a){
g[i][j] = inf;
}
queue <pii> q;
q.push({0, 0});
g[0][0] = 0;
while (1){
auto [x, y] = q.front(); q.pop();
if (x == a){
cout << g[x][y] + 1 << endl;
return;
}
// cout << x << " , " << y << endl;
int gcd = __gcd(a - x, b - y);
if (x + gcd > a || y + gcd > a) continue;
if (g[x][y + gcd] > g[x][y] + 1){
g[x][y + gcd] = g[x][y] + 1;
q.push({x, y + gcd});
}
if (g[x + gcd][y] > g[x][y] + 1){
g[x + gcd][y] = g[x][y] + 1;
q.push({x + gcd, y});
}
}
}
signed main(){
ios::sync_with_stdio(0); cout.tie(0); cin.tie(0);
int T = 1;
cin >> T;
while (T --){
solve();
}
system("PAUSE");
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
3 3 4 12 20 114 514