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: 83ms
Memory: 3376kB
In contest: 1276

#include<cstdio>
#include<cmath>
#include<string.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<string>
#include<stdlib.h>
using namespace std;
const int p=0x3f3f3f3f;
#define maxn 100005
long long a[maxn],b[maxn];
int n,m,k,x,y,l,r;
char s[maxn];
int gcd(int a,int b)
{
	return b==0?a:gcd(b,a%b);
}
int cmp(int a,int b)
{
	return a>b;
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		for(int i=0;i<m;i++)
		{
			scanf("%d%d",&l,&r);
			int sum=0;
			if(gcd(l,r)==1)
				{
					printf("1\n");
					continue;
			}
			for(int j=r;j>=l;j--)
			{
				if(gcd(j,r)==1)
				{
					sum++;
					r=j;
				}
				if(gcd(r,l)==1)
				{
					sum++;
					break;
				}
			}
			printf("%d\n",sum);
		}
	}
}