QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#758876 | #6440. Xingqiu's Joke | surenjamts# | TL | 1ms | 3556kb | C++20 | 2.0kb | 2024-11-17 20:13:03 | 2024-11-17 20:13:05 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define mk make_pair
#define S second
#define F first
map<int, int> fact;
vector<int> d;
int a, b;
int num_itr = 0;
int ans;
void DP() {
unordered_map<int, map<int, int>> dp;
set<int> q;
q.insert(b - a);
dp[b - a][a] = 0;
while (!q.empty()) {
auto itr = q.end();
itr--;
int t = *itr;
q.erase(itr);
for (auto &[num, cost] : dp[t]) {
ans = min(ans, num - 1 + cost);
}
for (int it : d) {
if (t % it == 0) {
for (auto &[num, cost] : dp[t]) {
int cost1 = num % it + 1;
int cost2 = it - (num % it) + 1;
// Update dp[t / it][num / it]
if (!dp[t / it].count(num / it)) {
dp[t / it][num / it] = cost1 + cost;
} else {
dp[t / it][num / it] = min(dp[t / it][num / it], cost1 + cost);
}
// Update dp[t / it][num / it + 1]
if (!dp[t / it].count(num / it + 1)) {
dp[t / it][num / it + 1] = cost2 + cost;
} else {
dp[t / it][num / it + 1] = min(dp[t / it][num / it + 1], cost2 + cost);
}
q.insert(t / it);
}
}
}
}
}
void solve(){
ans = 1e9;
cin >> a >> b;
if(a > b) swap(a, b);
int dif = b - a;
if(a == 1 || b == 1){
cout << "0\n";
return;
}
if(dif == 0){
cout << "1\n";
return;
}
for(int i = 2; i * i <= dif; i++){
while(dif % i == 0) fact[i]++, dif /= i;
}
if(dif > 1) fact[dif]++;
for(auto it : fact) d.push_back(it.F);
DP();
cout << ans << '\n';
}
int32_t main(){
int t;
cin >> t;
while(t--) solve();
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3556kb
input:
5 4 7 9 8 32 84 11 35 2 1
output:
2 7 5 4 0
result:
ok 5 number(s): "2 7 5 4 0"
Test #2:
score: -100
Time Limit Exceeded
input:
300 223528529 446621399 755625805 978718675 257717538 480810408 277875777 54782907 963091558 739998688 282469431 505562301 38752451 261845321 474429597 697522467 67026335 290119205 874629705 651536835 301964126 78871256 316864911 93772041 545942587 322849717 640801578 417708708 683070559 906163429 9...
output:
12 19 15 17 16 17 13 19 15 17 15 15 18 17 17 19 14 18 15 16 17 18 19 15 17 17 17 12 19 16 12 19 16 15 13 18 17 18 16 15 14 17 14 16 15 14 19 18 16 14 16 17 16 18 14 16 17 14 18 17 16 19 19 18 17 16 16 14 23 14 14 18 18 20 16 16 17 17 12 18 19 17 16 18 16 15 18 12 15 18 16 12 19 18 15 18 17 14 13 16 ...