QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#802977 | #9868. GCD | ucup-team5234# | ML | 0ms | 0kb | C++23 | 1.9kb | 2024-12-07 15:27:23 | 2024-12-07 15:27:24 |
answer
#include<bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
template<class T> using V = vector<T>;
template<class T> using VV = V<V<T>>;
template<class T> using VVV = V<VV<T>>;
template<class T> using VVVV = VV<VV<T>>;
#define rep(i,n) for(ll i=0ll;i<n;i++)
#define REP(i,a,n) for(ll i=a;i<n;i++)
const long long INF = (1LL << 60);
const long long mod99 = 998244353;
const long long mod107 = 1000000007;
const long long mod = mod99;
#define eb emplace_back
#define be(v) (v).begin(),(v).end()
#define all(i,v) for(auto& i : v)
#define UQ(v) sort(be(v)), v.erase(unique(be(v)), v.end())
#define LB(x,v) (lower_bound(be(v),(x))-(v).begin())
#define UB(x,v) (upper_bound(be(v),(x))-(v).begin())
#define dout() cout << fixed << setprecision(20)
#define randinit() srand((unsigned)time(NULL))
template<class T, class U> bool chmin(T& t, const U& u) { if (t > u){ t = u; return 1;} return 0; }
template<class T, class U> bool chmax(T& t, const U& u) { if (t < u){ t = u; return 1;} return 0; }
void solve(){
VV<ll> v(5001);
REP(i,2,5001){
for(ll j=i;j<=5000;j+=i) v[j].eb(i);
}
V<map<ll, ll>> mp(5001);
auto dfs = [&](auto &&dfs, ll a, ll b) -> ll {
if(a == 0 or b == 0) return 1;
if(mp[a].count(b)) return mp[a][b];
ll g = gcd(a, b);
ll ret = INF;
chmin(ret, dfs(dfs, a-g, b)+1);
ll y = INF;
for(auto x:v[a]){
ll c = b%x;
if(c % g) continue;
chmin(y, c);
}
if(y != INF) chmin(ret, dfs(dfs, a, b-y) + y/g);
return mp[a][b] = ret;
};
ll t;
cin >> t;
rep(i,t){
ll a,b;
cin >> a >> b;
cout << dfs(dfs, a, b) << '\n';
}
}
int main(){
cin.tie(nullptr);
ios::sync_with_stdio(false);
int t=1;
// cin >> t;
rep(i,t) solve();
}
详细
Test #1:
score: 0
Memory Limit Exceeded
input:
3 3 4 12 20 114 514