【第22期】观点:IT 行业加班,到底有没有价值?

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...

Objective-C在ARC下结合GCD的单例模式和宏模版

单例模式在iOS开发过程中经常用到,苹果提供过objective c单例的比较官方的写法: static MyGizmoClass *sharedGizmoManager = nil; + (MyGizmoClass*)sharedManager { @synchronized(self) { if (sharedGizmoManager == nil) { [[self alloc] init]; // assignment not done here

CodeForces 75C Modified GCD 【二分+数论】

题目链接 先求出a和b的最大公约数,找出其所有的因数——sqrt(n)的复杂度,涨姿势了。 然后就是判断所有的因数有没有落在low,high区间里面了——二分即可(upper_bound)...

GM8283C 完整资料下载, 可以替换TI的 SN75LVDS83,适用于车在视频转换

GM8283C的封装是TSSOP-56的,以下LVDS可替换的型号都是TSSOP-56的 厂家 型号 TI: SN75LVDS83 THINE: THC63LVDM83C/83R/83D DOESTEK: DTC34LM85A NationalSemiconductor: DS90C385完整的资料(GM8232 Datasheet pdf )可以从我的资料下载 或者在www.a

Codeforces 75C (二分+数学)

先求两个数的所有公因数,再二分查找在不在
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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