QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#231710#6440. Xingqiu's Jokesundage#WA 137ms3436kbC++171.2kb2023-10-29 15:50:542023-10-29 15:50:54

Judging History

你现在查看的是最新测评结果

  • [2023-10-29 15:50:54]
  • 评测
  • 测评结果:WA
  • 用时:137ms
  • 内存:3436kb
  • [2023-10-29 15:50:54]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define int long long


void solve() {
	int a, b;
	cin >> a >> b;
	if (a > b) {
		swap(a, b);
	}
	int t = b - a;
	vector<int>cnt;
	for (int i = 2; i <= t; i++) {
		if (t % i == 0) {
			cnt.push_back(i);
			while (t % i == 0) {
				t /= i;
			}
		}
	}
	if (t != 1) {
		cnt.push_back(t);
	}
	map<pair<int, int>, int>mp;
	int ans = a - 1;
	function<int(int, int, int)>dfs1 = [&](int a, int b, int tt) {
		if (a == 1) {
			return (int)0;
		}
		if (tt >= ans) {
			return (int)0;
		}
		if (mp[ {a, b}]) {
			return mp[ {a, b}];
		}
		mp[ {a, b}] = 1e18;
		for (int i = 0; i < cnt.size(); i++) {
			int x = a % cnt[i];
			int u;
			if (x == a) {
				u = x - 1;
				ans = min(ans, tt + x - 1);
			} else {
				u = x + 1 + dfs1((a - x) / cnt[i], (b - x) / cnt[i], tt + x + 1);
			}
			u = min(u, dfs1(((cnt[i] - x) + a) / cnt[i], ((cnt[i] - x) + b) / cnt[i], tt + cnt[i] - x + 1) + cnt[i] - x + 1);
			mp[ {a, b}] = min(mp[ {a, b}], u);
			ans = min(ans, u + tt);
		}
		return mp[ {a, b}];
	};
	dfs1(a, b, 0);
	cout << ans << endl;
	return;
}




signed main() {
	ios::sync_with_stdio(false);
	int tt = 1;
	cin >> tt;
	while (tt--) {
		solve();
	}
}




Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3412kb

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
Wrong Answer
time: 137ms
memory: 3436kb

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:

4
4
5
6
6
5
4
6
4
5
5
5
5
6
6
5
6
5
5
5
5
5
5
5
6
5
5
6
7
5
6
4
5
4
6
5
6
5
6
4
6
6
5
5
5
6
5
4
5
4
5
6
5
6
5
5
5
4
5
6
4
6
5
5
5
4
5
6
6
5
6
6
5
5
4
6
5
5
4
5
6
4
5
6
6
4
6
4
4
6
5
5
5
4
6
5
6
4
7
5
5
5
6
6
5
6
5
5
5
5
5
5
5
5
5
5
5
5
4
4
5
5
5
5
5
5
6
4
4
5
6
4
4
5
5
5
5
5
6
4
6
4
4
5
5
6
6
5
5
4
...

result:

wrong answer 1st numbers differ - expected: '12', found: '4'