QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#497739#6440. Xingqiu's JokepagohiaTL 0ms3652kbC++141.3kb2024-07-29 16:47:222024-07-29 16:47:22

Judging History

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

  • [2024-07-29 16:47:22]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3652kb
  • [2024-07-29 16:47:22]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int, int>pii;
typedef pair<long, long>pll;
int n, x, y;
int minn = 0x3ffff;

int nowx, nowy;
int check(int a, int b)
{
	if (a == 1 || b == 1) return 1;
	
	else return 0;
}
bool isPrime(int num) {
	if (num <= 1)
		return false;
	for (int i = 2; i * i <= num; ++i) {
		if (num % i == 0)
			return false;
	}
	return true;
}
int gcd(int a, int b) {
	while (b != 0) {
		int temp = a % b;
		a = b;
		b = temp;
	}
	return a;
}
void dfs(int a, int b, int step)
{
	if (check(a, b))
	{
		minn = min(minn, step);
		return;
	}

	if (step > minn)
	{
		return;
	}
	dfs(a - 1, b - 1, step + 1);
	int gcdValue = gcd(a, b);
	int flag = 1;
	for (int i = 2; i <= gcdValue; ++i) {
		if (gcdValue % i == 0 && isPrime(i) && flag == 1)
		{
			dfs(a / i, b / i, step + 1);
			flag = 0;
		}

	}
	dfs(a + 1, b + 1, step + 1);
}

void solve()
{
	int a, b;
	int step = 0;
	cin >> a >> b;
	if ( a == 1 || b == 1)
	{
		cout << 0 << endl;
		 return;
	}
	else if (a == b)
	{
		cout << a - 1 << endl;
		return;
	}
	minn = 0x3ffff;
	dfs(a, b, step);
	cout << minn << endl;


}

int main()
{
	int tcase;
	cin >> tcase;
	while (tcase--)
		solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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:


result: