CF75C Modified GCD

This question, seemingly unable to start QAQ

But we can think of is very simple dichotomy it, just over half the energy QAQ

Naturally it is a start and gcd ~~ (surely we will be a) ~ ~

then…

Then that is a half way QAQ

The Code

#include<bits/stdc++.h>
using namespace std;
int A,B,N,number=0;
int divisor[10000];
int gcd(int x,int y)//gcd
{
	if(y==0)return x;
	return gcd(y,x%y);
}
void solve()
{
	int low,high;
	scanf("%d%d",&low,&high);//每次查询的区间
	int _left=1,_right=number,middle,answer=-1;
	while(_left<=_right)//二分
	{
		middle=(_left+_right)/2;//取mid
		if(divisor[middle]>high/*判断是大了还是小了*/)_right=middle-1;else
		{
			_left=middle+1;
			if(divisor[middle]>=low)answer=divisor[middle];//记录下答案
		}
	}
	printf("%d\n",answer);//输出answer
}
int main()
{
	scanf("%d%d",&A,&B);
	int num=gcd(A,B);//记录下gcd后的值
	int Max=(int)(sqrt(num)),i;
	for(i=1;i<=Max;i++)
	if(num%i==0)//取出因数
	{
		divisor[++number]=i;
		divisor[++number]=num/i;
	}
	sort(divisor+1,divisor+number+1);//排一下序,符合单调性
	scanf("%d",&N);
	for(i=1;i<=N;i++)solve();//解决问题QAQ
}

Then
...
just
...
over ...

Published 72 original articles · won praise 22 · views 5537

Guess you like

Origin blog.csdn.net/sxy__orz/article/details/90727765
gcd