Result: Accepted
Time: 83ms
Memory: 3376kB
#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);
}
}
}