QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#382794#6440. Xingqiu's JokeshstyleTL 1159ms4036kbC++231.7kb2024-04-08 19:11:322024-04-08 19:11:32

Judging History

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

  • [2024-04-08 19:11:32]
  • 评测
  • 测评结果:TL
  • 用时:1159ms
  • 内存:4036kb
  • [2024-04-08 19:11:32]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
typedef long long ll;
typedef pair<int,int> PII;

int n,m,k;
vector<int> p;
unordered_map<int,int> mp;
//unordered_map<int,> mp;
bool tf[N];
vector<PII> ys;
int ans;


void dfs(int x,int y,int num){
//		cout<<x<<" "<<y<<" "<<num<<endl;
		ans=min(ans,num+min(x,y)-1);
	if(x==1||y==1){
		return;
	}
	int dist=y-x;
	
//	cout<<"--"<<x<<" "<<y<<endl;
	for(auto [u,v]:ys){
//		cout<<dist<<" "<<u<<endl;
		if(dist%u!=0) continue;
		int tt=x%u;
		int num1=x-tt,num2=y-tt;
		int gg=u;
		
		int res=num+tt+1;
//		cout<<num1<<" "<<num2<<" "<<res<<" "<<tt<<" "<<mp[gg]<<endl;
		if(res<ans){
			dfs(num1/gg,num2/gg,res);
		}
		tt=u-tt;
		num1=x+tt,num2=y+tt;
		gg=u;
		res=num+tt+1;
		if(res<ans){
			dfs(num1/gg,num2/gg,res);
		}
	}
}

void dfs1(int x,int sum,int cnt){
	
	if(x>=ys.size()){
		mp[sum]=cnt;
		return;
	}
	int res=1,cc=0;
	for(int i=0;i<=ys[x].second;i++,res*=ys[x].first){
		dfs1(x+1,sum*res,cnt+i);
	}
}

void __(){
	cin>>n>>m;
	ans=min(n,m)-1;
	if(n>m) swap(n,m);
	int cha=m-n;
	ys.clear();
	mp.clear();
	for(int i=0;p[i]<=(m-n)/p[i];i++){
		int cnt=0;
		while(cha%p[i]==0){
			cnt++;
			cha/=p[i];
		}
		if(cnt!=0)
			ys.push_back({p[i],cnt});
	}
	if(cha!=1){
		ys.push_back({cha,1});
	}
	dfs1(0,1,0);
	
	dfs(min(n,m),max(n,m),0);
//	printf("%lld\n",ans);
	cout<<ans<<"\n";
}

int main(){
	
//	ios::sync_with_stdio(0);
//	cin.tie(0);
	for(int i=2;i<=100000;i++){
		if(!tf[i]) p.push_back(i);
		for(int j=0;p[j]<=100000/i;j++){
			tf[i*p[j]]=1;
			if(i%p[j]==0) break;
		}
	}
	
	int _;
	cin>>_;
	while(_--){
		__();
	}
	
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 4036kb

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: 0
Accepted
time: 1159ms
memory: 3836kb

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
...

result:

ok 300 numbers

Test #3:

score: -100
Time Limit Exceeded

input:

300
636099362 669653794
640628976 607074544
492031079 458476647
420324662 386770230
234845232 201290800
416098756 382544324
513320531 546874963
245050186 278604618
688926901 722481333
458533405 424978973
863331691 896886123
638892374 672446806
849746534 816192102
761591596 728037164
369934525 403488...

output:

51
49
48
45
35
45
50
42

result: