Codeforces 75 C Modified GCD

原创 2016年08月29日 20:34:07

http://codeforces.com/problemset/problem/75/C

C. Modified GCD
time limit per test2 seconds
memory limit per test256 megabytes

Problem description
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it’s an easy task to calculate the GCD between two positive integers.

A common divisor for two positive numbers is a number which both numbers are divisible by.

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.

You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.

Input
The first line contains two integers a and b, the two integers as described above (1 ≤ a, b ≤ 109). The second line contains one integer n, the number of queries (1 ≤ n ≤ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 ≤ low ≤ high ≤ 109).

Output
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.

Examples
input
9 27
3
1 5
10 11
9 11
output
3
-1
9

题目大意:输入两个数,然后有n个询问,询问为一个区间,求这个区间内两个数的公约数的最大值。

题目类型:二分,数论

大概思路:
1.预处理两个数所有的公约数。
2.对数组进行二分,比较和y的大小,找出比y小的公约数中的最大的那一个,把它和左区间进行比较,如果比左区间大,该值就是答案,如果比左区间小,那么就是没有答案。

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;

int m[1000005];

int gcd(int a,int b);
int search(int y,int t);

int main()
{
    int a,b;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        int i,t=0,x;
        x=gcd(a,b);
        for(i=1;i<=sqrt(x);i++)   //不能为i<=x,遍历会超时
        {
            if(x%i==0)
               {
                   m[t++]=i;
                   m[t++]=x/i;
               }
        }
        sort(m,m+t);     //排序

        int n,z,y,c;
        scanf("%d",&n);
        while(n--)
        {
            int tag=0;
            scanf("%d%d",&z,&y);
            if(y<m[0]||z>m[t-1])    //特判
                tag=1;
            else
            {
                c=search(y,t);

                if(z>m[c])
                    tag=1;
            }
            if(tag)
                printf("-1\n");
            else
                printf("%d\n",m[c]);

        }

    }
    return 0;
}

int gcd(int a,int b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}

int search(int y,int t)
{
    int left=0,right=t,mid;
    while(left+1<right)
    {
        mid=(left+right)/2;
        if(y>=m[mid])
            left=mid;
        else
            right=mid;
    }
    return left;
}

无聊……
这里写图片描述

版权声明:本文为博主原创文章,未经博主允许不得转载。

【CodeForces】75C - Modified GCD(快速求公约数,upper_bound函数)

C. Modified GCD time limit per test 2 seconds memory limit per test 256 megabytes i...
  • wyg1997
  • wyg1997
  • 2016年05月24日 18:57
  • 410

文章标题 codeforces 75C:Modified GCD (二分+最大公因数)

Modified GCDDescriptionWell, here is another math class task. In mathematics, GCD is the greatest co...

C. Modified GCD(二分加搜索约数)

C. Modified GCD time limit per test  2 seconds memory limit per test   256 megabytes 题目...

CodeForces - 803C Maximal GCD

Maximal GCDYou are given positive integer number n. You should create such strictly increasing seque...
  • WwyY9
  • WwyY9
  • 2017年08月01日 18:46
  • 173

Codeforces 583C GCD Table 模拟 map

原题见CF 583C对于一个数组a[n],定义一个n*n的gcd矩阵,b[i][j]=gcd(a[i], a[j]).现在乱序给出矩阵内的全部元素,求原数组a[n].如果已知原数组得到gcd矩阵,则可...

Codeforces Round #323 (Div. 2)C. GCD Table

C. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
  • ZZ_AC
  • ZZ_AC
  • 2015年10月06日 00:26
  • 314

Mike and gcd problem CodeForces - 798C

Mike has a sequence A = [a1, a2, ..., an] of length n. He considers the sequence B = [b1, b2, ...,...

Codeforces 583C/582A 暴力+gcd

A. GCD Table time limit per test 2 seconds memory limit per test 256 megabytes input standard inp...

Codeforces #323 C. GCD Table (脑洞题、Induction)

题意: 给出乱序的GCD table的n2个元素,恢复原来的序列给出乱序的GCD\ table的n^2个元素, 恢复原来的序列 分析: 我们让答案a1≤ a2 ≤ ... ≤ an.很显然g...
  • lwt36
  • lwt36
  • 2015年10月04日 15:50
  • 367

【Codeforces 798 C】+ gcd

C. Mike and gcd problem time limit per test2 seconds memory limit per test256 megabytes inputstan...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces 75 C Modified GCD
举报原因:
原因补充:

(最多只允许输入30个字)