4427. Greatest Common Divisors

来源:互联网 发布:动漫图片制作软件 编辑:程序博客网 时间:2024/06/07 03:02

4427. Greatest Common Divisors

Constraints

Time Limit: 5 secs, Memory Limit: 256 MB

Description

A common divisor for two positive numbers is a number which both numbers are divisible by. It's easy to calculate the greatest common divisor between tow numbers. But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low<=d<=high. It is possible that there is no common divisor in the given range.

Input

The first line contains an integer T (1<=T<=10)- indicating the number of test cases. 

For each case, there are four integers a, b, low, high (1<=a,b<=1000,1<=low<=high<=1000) in one line.

 

Output

For each case, print the greatest common divisor between a and b in given range, if there is no common divisor in given range, you should print No answer(without quotes).

Sample Input

9 27 1 5 

9 27 10 11 

9 27 9 11 

Sample Output

3

No answer

9

Problem Source

2011年新手赛Warm Up by AcFast

// Problem#: 4427

// Submission#: 1984637

// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License

// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/

// All Copyright reserved by Informatic Lab of Sun Yat-sen University

#include<iostream>

#include<string>

#include<cmath>

using namespace std;

int main(){

    int T,i;

    cin>>T;

    for(i=0;i<T;i++){

        int a, b, low, high;

        cin>>a>>b>>low>>high;

        int x,temp=0;

        for(x=low;x<=high;x++){

            if((a%x)==0&&(b%x)==0){

                temp=x;

            }

        }

        if(temp==0){

            cout<<"No answer"<<endl;

        }

        else{

            cout<<temp<<endl;

        }

    }

    return 0;

}                                 

原创粉丝点击