HDU 5019 简单数学题

8 篇文章 0 订阅
4 篇文章 0 订阅

这道题是说给定A和B,求第C大的公约数。

我们最长求的就是最大公约数了,也就是通常用的GCD算法。但是现在要求第C大的公约数,我们可以想见如果令第C大的公约数为x,最大公约数为g的话,那么x|g的,为什么呢?

我们可以直观的理解,最大公约数其实就是A和B分别进行素因子分解之后,能取到公共素因子乘起来得到的。而对于任意A、B的公约数,那么肯定包含了部分的最大公约数所包含的素因子,因此x|g。

于是要求第C大的公约数,只需要枚举g的因子就行了,我们知道求一个数的因子情况,是可以进行O(sqrt(n))的枚举的,这道题n的范围就是10^12,所以复杂度内是够的。

但是好久没有写这种卡时限很紧的题了,稍微把判断写多了或者多枚举了一些内容就tle了,确实很无力。还是自己太渣。

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstdlib>

using namespace std;

typedef __int64 LL;

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

int main()
{
    LL a, b, c;
    int T;
    vector<LL> v;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%I64d%I64d%I64d", &a, &b, &c);
        LL temp = gcd(a, b);
        v.clear();
        int cnt = 0;
        for(LL i=1; i*i<=temp; ++i)
        {
            if(temp%i==0)
             {
                 v.push_back(i);
                 if(i*i!=temp)
                    v.push_back(temp/i);
             }

        }
        sort(v.begin(), v.end());
        if(v.size() >= c)
            printf("%I64d\n", v[v.size()-c]);
        else
            printf("-1\n");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
30 Ways to Have Some Computer-Controlled Evil Fun! “The steps are easy to follow…text is precise and understandable…uses very clear pictures and schematics to show what needs doing…Most importantly these projects are fun!”–Boing Boing This wickedly inventive guide shows you how to program and build a variety of projects with the Arduino microcontroller development system. Covering Windows, Mac, and Linux platforms, 30 Arduino Projects for the Evil Genius gets you up to speed with the simplified C programming you need to know–no prior programming experience necessary. Using easy-to-find components and equipment, this do-it-yourself book explains how to attach an Arduino board to your computer, program it, and connect electronics to it to create fiendishly fun projects. The only limit is your imagination! 30 Arduino Projects for the Evil Genius: Features step-by-step instructions and helpful illustrations Provides full schematic and construction details for every project Covers the scientific principles behind the projects Removes the frustration factor–all required parts are listed along with sources Build these and other devious devices: Morse code translator High-powered strobe light Seasonal affective disorder light LED dice Keypad security code Pulse rate monitor USB temperature logger Oscilloscope Light harp LCD thermostat Computer-controlled fan Hypnotizer Servo-controlled laser Lie detector Magnetic door lock Infrared remote Each fun, inexpensive Evil Genius project includes a detailed list of materials, sources for parts, schematics, and lots of clear, well-illustrated instructions for easy assembly. The larger workbook-style layout and convenient two-column format make following the step-by-step instructions a breeze. In December 2011, Arduino 1.0 was released. This changed a few things that have caused the sketches for Projects 10, 27, and 28 in this book to break. To fix this, you will need to get the latest versions of the Keypad and IRRemote libraries. The Keypad library has been updated for Arduino 1.0 by its original creators and can be downloaded from here: http://www.arduino.cc/playground/Code/Keypad Ken Shiriff’s IRRemote library has been updated and can be downloaded from here: http://www.arduinoevilgenius.com/new-downloads Make Great Stuff! TAB, an imprint of McGraw-Hill Professional, is a leading publisher of DIY technology books for makers, hackers, and electronics hobbyists. Book Details Series: Evil Genius Paperback: 208 pages Publisher: McGraw-Hill/TAB Electronics; 1 edition (July 28, 2010) Language: English ISBN-10: 007174133X ISBN-13: 978-0071741330

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值