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;
    }
  • 相关阅读:
    从 0 → 1,学习Linux该这么开始!
    Web和移动开发的未来
    css-div中文字过多(内容超出div宽度)后自动换行
    js+css--单选按钮,自定义选中的颜色???(性别按钮,男女)
    css-按钮中有图片和文字,怎么才能让文字和图片都中??
    js-点出弹框后(除了点击窗口上的叉子),点其他地方能够关闭窗口???
    css-外面元素的高度,由里面的元素进行撑开(由内部的高度决定)
    js-将传来的数据排序,让(全部)这个小按钮小圈圈,始终排列在最前面
    echart--如何在折线图上添加矩形背景(可以借用bar柱状图的实现效果)
    echart-如何将x轴和y轴的原点进行重合???
  • 原文地址:https://www.cnblogs.com/pk28/p/6012620.html
Copyright © 2011-2022 走看看