QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#624165#6440. Xingqiu's Jokewwww22#TL 3ms4516kbC++201.2kb2024-10-09 15:08:072024-10-09 15:08:07

Judging History

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

  • [2024-10-09 15:08:07]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:4516kb
  • [2024-10-09 15:08:07]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define fo(i,a,b) for(ll (i)=(a);(i)<=(b);(i)++)
#define fd(i,a,b) for(ll (i)=(a);(i)>=(b);(i)--)
using namespace std;
const int N=2e5+10;
int n,a,b;
map<pair<int,int>,int> mp;
int vis[N];
int pr[N];
int cnt=0;
void init()
{
	
	for(int i=2;i<N;i++)
	{
		if(vis[i])continue;
		pr[cnt]=i;
		cnt++;
		for(int j=1;j<N/i;j++)
		{
			vis[j*i]=1;
		}
	}
}

bool pri(int x)
{
	for(int i=2;i*i<=x;i++)
	{
		if(x%i==0)
		{
			return false;
		}
	}
	return true;
}

int find(int a,int b)
{
	if(a==1)return 0;
	if(b==1)return a-1;
	int ans=a-1;
	for(int i=0;i<cnt;i++)
	{
		if(b%pr[i]==0)
		{
			ans=min(ans,1+a%pr[i]+find(a/pr[i],b/pr[i]));
			ans=min(ans,1+(pr[i]-a%pr[i])%pr[i]+find(a/pr[i]+1,b/pr[i]));
		}
	}
	if(pri(b))
	{
		ans=min(ans,1+a%b+find(a/b,1));
		ans=min(ans,1+(b-a%b)%b+find(a/b+1,1));
	}
	mp[make_pair(a,b)]=ans;
	return ans;
}


void solve()
{
	cin>>a>>b;
	if(a>b)swap(a,b);
	b=b-a;
	cout<<find(a,b)<<"\n";
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	init();
//	for(int i=0;i<cnt;i++)
//	{
//		cout<<pr[i]<<" ";
//	}
//	cout<<"\n";
	int T=1;
	cin>>T;
	while(T--) solve();
	return 0;
} 

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 4516kb

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: