Start: Jun, 28, 2019 08:42:00
2019杭州师范大学第一届程序设计竞赛新生赛
End: Jun, 28, 2019 11:42:00
Time elapsed:
Time remaining:

Problem_ID: D
Result: Accepted
Time: 103ms
Memory: 1712kB
In contest: 1276

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

int n,q,s,t;
int Min;

int gcd(int a,int b){
	if(b == 0) return a;
	else return gcd(b,a%b);
}
int main(){
	cin>>n>>q;
	int m;
	while(q--){
		scanf("%d %d",&s,&t);
		if(s > t){
			m = t;
			t = s;
			s = m;
		}
		Min = t-s;
		if(s > 1){
			Min = min(gcd(s+1,t)+1,Min);
			Min = min(gcd(s-1,t)+1,Min);
			Min = min(gcd(s,t+1)+1,Min);
			Min = min(gcd(s,t-1)+1,Min);	
		}
		printf("%d\n",Min);
	}
	return 0;
}