zoukankan      html  css  js  c++  java
  • CodeForces#275--DIV 2--A

    A. Counterexample
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Your friend has recently learned about coprime numbers. A pair of numbers {a, b} is called coprime if the maximum number that divides both a and b is equal to one.

    Your friend often comes up with different statements. He has recently supposed that if the pair (a, b) is coprime and the pair (b, c) is coprime, then the pair (a, c) is coprime.

    You want to find a counterexample for your friend's statement. Therefore, your task is to find three distinct numbers (a, b, c), for which the statement is false, and the numbers meet the condition l ≤ a < b < c ≤ r.

    More specifically, you need to find three numbers (a, b, c), such that l ≤ a < b < c ≤ r, pairs (a, b) and (b, c) are coprime, and pair(a, c) is not coprime.

    Input

    The single line contains two positive space-separated integers lr (1 ≤ l ≤ r ≤ 1018; r - l ≤ 50).

    Output

    Print three positive space-separated integers abc — three distinct numbers (a, b, c) that form the counterexample. If there are several solutions, you are allowed to print any of them. The numbers must be printed in ascending order.

    If the counterexample does not exist, print the single number -1.

    Sample test(s)
    input
    2 4
    output
    2 3 4
    input
    10 11
    output
    -1
    input
    900000000000000009 900000000000000029
    output
    900000000000000009 900000000000000010 900000000000000021
    Note

    In the first sample pair (2, 4) is not coprime and pairs (2, 3) and (3, 4) are.

    In the second sample you cannot form a group of three distinct integers, so the answer is -1.

    In the third sample it is easy to see that numbers 900000000000000009 and 900000000000000021 are divisible by three.

    #include <iostream>
    #include <algorithm>
    #include <cmath>
    #include <cstdio>
    
    using namespace std;
    
    long long gcd(long long x, long long y)
    {
        if(!x || !y)    return x > y ? x : y;
    
        for(long long  t; t = x%y; x = y, y = t);
    
        return y;
    }
    
    long long kgcd(long long a, long long b)
    {
        if(a < b)
            swap(a, b);
        if(a == 0)  return b;
    
        if(b == 0)  return a;
    
        if(!(a&1) && !(b&1))
            return kgcd(a>>1, b>>1) << 1;
        else if(!(b & 1))
            return kgcd(a, b>>1);
        else if(! (a & 1))
            return  kgcd(a>>1, b);
        else
            return kgcd(b, a - b);
    }
    
    int main()
    {
        long long l, r;
        while(cin >> l >> r)
        {
            long long a, b, c;
            bool tag = false;
            ///暴力枚举
            for(a = l; a < r; a++)
            {
                for(b = a + 1; b < r; b++)
                {
                    for(c = b + 1; c <= r; c++)
                    {
                        if(kgcd(a, c) != 1 && kgcd(a, b) == 1 && kgcd(b, c) == 1)
                        {
                            tag = true;
                            cout << a << " " << b << " " << c << endl;
                            break;
                        }
                    }
                    if(tag == true)
                        break;
                }
                if(tag == true)
                    break;
            }
    
            if(tag == false)
                cout << -1 << endl;
        }
    
        return 0;
    }
    

      试着又复习了下GCD KGCD

  • 相关阅读:
    成本直降50% | 阿里云发布云原生网关,开启下一代网关新进程
    阿里云容器服务全面升级为 ACK Anywhere,让云的边界拓展至企业需要的每个场景
    云拨测助力节卡机器人 全面优化海外网站性能
    如何加速云原生数据应用?这个开源项目备受关注
    课程升级 | 极速构建知识体系,即学即用 Serverless
    ECS 选款利器!PTS助您快速上云!
    Morphling:云原生部署 AI , 如何把降本做到极致?
    报名领奖|云栖大会,10月19-22日杭州不见不散!
    Dubbo3.0|阿里巴巴服务框架三位一体的选择与实践
    告别Kafka Stream,让轻量级流处理更加简单
  • 原文地址:https://www.cnblogs.com/ya-cpp/p/4049663.html
Copyright © 2011-2022 走看看