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: 90ms
Memory: 1712kB
In contest: 1276

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <set> 
using namespace std;
int i,j,t;
int gcd(int a,int b)
{
	int x,y;
	if(a>b)
		swap(a,b);
	while(1)
	{
		x=b%a;
		if(x==0)
		{
			return a;
		}
		else
		b=a;
		a=x;
	}
}
int main()
{
	int n,q,a,b;
	scanf("%d %d",&n,&q);
	while(q--)                  
	{
		scanf("%d %d",&a,&b);
		if(a>b)
			swap(a,b);
		t=gcd(a,b);
		int z=9999;
		if((a%2==1&&b%2==0)||(a%2==0&&b%2==1))
			z=2;
		if(a%2==1&&b%2==1)
		{
			z=2;
		}
		if(b%2==0&&a%2==0&&((b/a>=4)||(b-a>=4)))
			z=2;
		if(a==2)
			z=2;
		int mina;
		mina=min(z,t);
		mina=min(mina,abs(a-b));
		printf("%d\n",mina);
	}
}