QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#240553#6440. Xingqiu's JokesfjhTL 0ms3624kbC++141.2kb2023-11-05 16:21:002023-11-05 16:21:01

Judging History

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

  • [2023-11-05 16:21:01]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:3624kb
  • [2023-11-05 16:21:00]
  • 提交

answer

#include<bits/stdc++.h> 
using namespace std;
#define fer(i,a,b) for(int i=a;i<=b;i++)
#define fdr(i,a,b) for(int i=a;i>=b;i--)
#define int long long
#define xx first
#define yy second
#define pb push_back
#define ps push
#define db double
#define P pair<int,int>
#define endl '\n'
#define ll long long
const int N=2e5+10;
const int mod=1e9+7;
const int INF=1e18+10;
priority_queue<int,vector<int>,greater<int>> pq;

int n;
vector<int> v;
map<P,int> mp;

void get(int x){
	for(int i=2;i*i<=x;i++){
		if(x%i==0){
			v.pb(i);
			while(x%i==0) x/=i;
		}
	} 
	if(x>1) v.pb(x);
}

int dfs(int a,int c){
	if(a==1) return 0;
	if(c==1) return a-1;
	if(mp[{a,c}]) return mp[{a,c}];
	int res=a-1;
	for(int d:v){
		if(c%d==0){
			int rest=a%d;
			int tmp1=rest+1+dfs(a/d,c/d);
			int tmp2=(d-rest)+1+dfs(a/d+1,c/d);
			res=min({res,tmp1,tmp2});
		}
	}
	return mp[{a,c}]=res;
}

void solve(){
	int a,b;
	cin>>a>>b;
	int c=abs(a-b);
	get(c);
	cout<<dfs(min(a,b),c)<<endl;
}

signed main()
{
	ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
	int tt;
	cin>>tt;
	while(tt--){
		solve(); 
	} 
}





















详细

Test #1:

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

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: