zoukankan      html  css  js  c++  java
  • Codeforces Beta Round #67 (Div. 2)C. Modified GCD

    C. Modified GCD
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    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

    题目大意:找出a b的公因子  然后给几个查询,查询在x y之间的最大公因子 输出。

    先把因子找出来,然后二分找一下最大的.

    其实找因子 一共有两种方法(就我所知)   对于10^18这种级别的 用质因子那种方法找, 对于10^9这种级别的 用sqrt(n)的方法枚举因子的方法去找.....

    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/10/30 10:11:46
    File Name     :cf67c.cpp
    ************************************************ */
    #include <bits/stdc++.h>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    vector<int>v;
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        int a,b,x,y,n;
        cin>>a>>b;
        a=__gcd(a,b);
        b=sqrt(a);
        v.clear();
        for(int i=1;i<=b;i++){
            if(a%i==0){
                if(i*i==a)v.push_back(i);
                else v.push_back(i),v.push_back(a/i);
            }
        }
        sort(v.begin(),v.end());
    
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>x>>y;
            int pos=upper_bound(v.begin(),v.end(),y)-v.begin()-1;
            if(x>v[pos])puts("-1");
            else cout<<v[pos]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    mq/mysql/redis/nginx常见服务&工具安装
    中断——中断描述符表的定义和初始化(二) (基于3.16-rc4)
    中断——中断描述符表的定义和初始化(一) (基于3.16-rc4)
    linux内核中与进程相关的数据结构(基于linux3.16-rc4)
    内核链表的应用
    ubuntu下做柯老师lab19-lab20实验问题总结
    在ns2.35中添加myevalvid框架
    在ns2.35下完成柯老师lab18实验
    windows编程socket问题
    如何高效利用GitHub
  • 原文地址:https://www.cnblogs.com/pk28/p/6012620.html
Copyright © 2011-2022 走看看