当前位置: 首页 > news >正文

HDU 5019 Revenge of GCD(数学)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019


Problem Description
In mathematics, the greatest common divisor (gcd), also known as the greatest common factor (gcf), highest common factor (hcf), or greatest common measure (gcm), of two or more integers (when at least one of them is not zero), is the largest positive integer that divides the numbers without a remainder.
---Wikipedia

Today, GCD takes revenge on you. You have to figure out the k-th GCD of X and Y.
 
Input
The first line contains a single integer T, indicating the number of test cases. 

Each test case only contains three integers X, Y and K.

[Technical Specification]
1. 1 <= T <= 100
2. 1 <= X, Y, K <= 1 000 000 000 000
 
Output
For each test case, output the k-th GCD of X and Y. If no such integer exists, output -1.
 
Sample Input

   
3 2 3 1 2 3 2 8 16 3
 
Sample Output

   
1 -1 2
 
Source
BestCoder Round #10


官方题解:



代码例如以下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef __int64 LL;
vector<LL>v;
LL GCD(LL a, LL b)
{
    if(b == 0)
        return a;
    return GCD(b,a%b);
}

int main()
{
    int t;
    LL x, y, k;
    scanf("%d",&t);
    while(t--)
    {
        v.clear();
        scanf("%I64d%I64d%I64d",&x,&y,&k);
        LL tt = GCD(x,y);
        // printf("%I64d\n",tt);
        for(LL i = 1; i*i <= tt; i++)
        {
            if(tt%i == 0)
            {
                v.push_back(i);
                if(i*i != tt)//防止放入两个i
                    v.push_back(tt/i);
            }
        }
        sort(v.begin(), v.end());
        if(k > v.size())
            printf("-1\n");
        else
            printf("%I64d\n",v[v.size()-k]);
    }
    return 0;
}


相关文章:

  • [<事务专题>]
  • Nginx总算支持动态模块了
  • 【MySQL中的锁】
  • Linux在线安装git(亲测成功)
  • [<MySQL优化总结>]
  • yum update
  • Redis是什么?
  • C语言中函数返回值的问题
  • 哈夫曼树
  • Redis有哪五种不同类型的值?应用场景有哪些?
  • jvm重要参数分析
  • 使用redis可能出现的问题
  • phpQuery对数据信息的采集进一步学习
  • Redis底层数据结构
  • 用户口令复杂度策略设置银河麒麟
  • [PHP内核探索]PHP中的哈希表
  • 「面试题」如何实现一个圣杯布局?
  • 0x05 Python数据分析,Anaconda八斩刀
  • CSS 专业技巧
  • Django 博客开发教程 8 - 博客文章详情页
  • JavaScript设计模式与开发实践系列之策略模式
  • PHP 小技巧
  • Rancher如何对接Ceph-RBD块存储
  • storm drpc实例
  • Vue 2.3、2.4 知识点小结
  • Yii源码解读-服务定位器(Service Locator)
  • 微服务框架lagom
  • 一个JAVA程序员成长之路分享
  • Redis4.x新特性 -- 萌萌的MEMORY DOCTOR
  • ​​​​​​​ubuntu16.04 fastreid训练过程
  • #我与Java虚拟机的故事#连载04:一本让自己没面子的书
  • #我与Java虚拟机的故事#连载14:挑战高薪面试必看
  • $(function(){})与(function($){....})(jQuery)的区别
  • (C语言)strcpy与strcpy详解,与模拟实现
  • (Forward) Music Player: From UI Proposal to Code
  • (二)Linux——Linux常用指令
  • (九)信息融合方式简介
  • (免费领源码)Java#ssm#MySQL 创意商城03663-计算机毕业设计项目选题推荐
  • (三)终结任务
  • (转)Oracle 9i 数据库设计指引全集(1)
  • (转)创业家杂志:UCWEB天使第一步
  • (转)机器学习的数学基础(1)--Dirichlet分布
  • .【机器学习】隐马尔可夫模型(Hidden Markov Model,HMM)
  • .desktop 桌面快捷_Linux桌面环境那么多,这几款优秀的任你选
  • .gitignore
  • .libPaths()设置包加载目录
  • .Net CF下精确的计时器
  • .net framework 4.0中如何 输出 form 的name属性。
  • .Net Memory Profiler的使用举例
  • .NET MVC之AOP
  • .NET 应用启用与禁用自动生成绑定重定向 (bindingRedirect),解决不同版本 dll 的依赖问题
  • .NET教程 - 字符串 编码 正则表达式(String Encoding Regular Express)
  • .net实现头像缩放截取功能 -----转载自accp教程网
  • .vollhavhelp-V-XXXXXXXX勒索病毒的最新威胁:如何恢复您的数据?
  • @ComponentScan比较