Revenge of GCD HDU5019
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
________________
题意:
给出x , y 求 第k大的公约数(x,y,k <= 1e12)。
看到这题数据范围直接吓尿,一开始写了个 求最大公约数后枚举约数,意料之中的T了。
然后想到,第k大的公约数,其实就是最大公约数的第k个因子。
如果枚举最大公约数的因子,最大公约数可能有1e12那么大,显然会t.
那么可以考虑只 枚举一半的因子(因为1e12的约数最多 2* sqrt(1e12) 也就是2e6左右( 这里有点问题,我看别人有用1e6存的,我不知道我想的对不对) )
这样,可以用数组存起来,只要
for( int i = 1 ; i <=sqrt( gcd (x,y) ) ; i++)
{
if( gcd(x,y) % i == 0 )
array[cnt++] = i ;
if( gcd (x,y) / i == 0 )
array[cnt++] = gcd(x,y) / i ;
}
}
然后对这个数组排序后从大到小取就可以了。别人的代码A了自己的T了一下午。
然后又看到别人的思路
先在 1 - sqrt( gcd(x,y) ) 中找,
第n个小的约数所对应的 gcd(x,y) / n 也就是第n个大的约数。
找不到继续在
( gcd(x,y) ) - 1 区间中找 ,找到的也就是第n个大的约数 ( 这里我说不太清楚,不太好理解,大概可以理解为gcd(x,y)先找一边的约数再找另一边)
代码如下:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long int ll ;
ll gcd ( ll a , ll b )
{
if ( b == 0 ) return a ;
else
gcd(b , a % b );
}
int main()
{
int t ;
cin>> t;
for( int z = 1 ; z <= t ; z++)
{
llx,y,k;
cin>> x >> y >> k ;
llt = gcd(x,y);
llnum = 0 , v;
for( int i = 1 ; i <= sqrt(t) ;i++)
{
if( t % i == 0 )
{
num++;
v=t/i;
}
if( num == k )
break ;
}
if( num == k )
{cout<< v << endl ; continue;}
else
for( int i = sqrt(t) ; i >=1 ; i--)
{
if( i*i == t)
continue;
if( t % i == 0 )
{
num++;
v=i;
}
if( num == k)
break ;
}
if( num == k )
cout<< v << endl ;
else cout << "-1" << endl ;
}
return 0 ;
}
——————
不知道为什么T了一下午,照猫画虎对着别人思路都T,重新好几次还T。然后晚上饭后一怒之下又重写一遍莫名AC。。
![]()
Revenge of GCD HDU5019的更多相关文章
- HDOJ 5019 Revenge of GCD
Revenge of GCD In mathematics, the greatest common divisor (gcd), also known as the greatest common ...
- 数学--数论--HDU 5019 revenge of GCD
Revenge of GCD Problem Description In mathematics, the greatest common divisor (gcd), also known as ...
- HDU 5019 Revenge of GCD(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 Problem Description In mathematics, the greatest ...
- BestCoder10 1002 Revenge of GCD(hdu 5019) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 题目意思:给出 X 和 Y,求出 第 K 个 X 和 Y 的最大公约数. 例如8 16,它们的公 ...
- HDU 5019 Revenge of GCD
题解:筛出约数,然后计算即可. #include <cstdio> #include <algorithm> typedef long long LL; LL a1[10000 ...
- hdu 5018 Revenge of GCD
题意: 给你两个数:X和Y .输出它们的第K大公约数.若不存在输出 -1 数据范围: 1 <= X, Y, K <= 1 000 000 000 000 思路: 它俩的公约数一定是gcd ...
- hdu 5019(第K大公约数)
Revenge of GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Objective-C三种定时器CADisplayLink / NSTimer / GCD的使用
OC中的三种定时器:CADisplayLink.NSTimer.GCD 我们先来看看CADiskplayLink, 点进头文件里面看看, 用注释来说明下 @interface CADisplayLin ...
- iOS 多线程之GCD的使用
在iOS开发中,遇到耗时操作,我们经常用到多线程技术.Grand Central Dispatch (GCD)是Apple开发的一个多核编程的解决方法,只需定义想要执行的任务,然后添加到适当的调度队列 ...
随机推荐
- java 子类、父类中静态代码块、字段,非静态代码块、字段以及构造函数的初始化顺序和次数
一个类中的数据初始化顺序是面试官非常喜欢出的面试题之一,本文用一个实例来介绍java中子类.父类中静态代码块.字段,非静态代码块.字段以及构造函数的执行顺序和次数. 一.包结构
- Mac下Android studio搭建Android开发环境【新手】
上学期用的还是windows,这学期新入手了mac,突然也想装个Android studio来玩玩.安装过程中出现了问题,记录如下. 先是装安卓studio的下载链接,不推荐去官网装,这里提供一个挺好 ...
- php文件上传及头像预览
php文件上传原理是通过form表单的enctype="multipart/form-data"属性将文件临时放到wamp文件夹中的tmp目录下,再通过后台php 程序将文件保存在 ...
- C#通过外部别名,解决DLL冲突问题
今天遇到一个有两个DLL文件,命名空间,部分类名与部分方法名一样,但是方法的功能实现不一样.调用方法时,无法调用指定DLL的指定方法.在网上找了好多,简单总结一下. 1.首先添加引用,不细说. 2.右 ...
- itoa()函数和atoi()函数
1.int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明.● itoa():将 ...
- Could not locate executable E:\SoftWave\Hadoop-2.2.0\bin\winutils.exe in the Hadoop binaries解决办法
需要下载windows版本 bin目录下的文件,替换hadoop目录下原来的bin目录下的文件.下载网址是: https://github.com/srccodes/hadoop-common-2.2 ...
- HDU 1269 迷宫城堡(DFS)
迷宫城堡 Problem Description 为了训练小希的方向感,Gardon建立了一座大城堡,里面有N个房间(N<=10000)和M条通道(M<=100000),每个通道都是单向的 ...
- Vultr日本vps搭建ss/ssr/openvpn免流教程
每个月的手机流量不够用,运营商流量套餐价格偏高,怎么才能省钱?你在淘宝上,搜索手机免流,可找到很多奸商销售免流套餐,一块钱可买1GB流量,免流原理是什么?自己能搞吗? 手机免流原理 手机运营商中国电信 ...
- Ubuntu 16.04 Django安装和配置
之前有安装和配置过,换了台电脑,再安装和配置,忽然发现差不多都忘记了,这里记录下已备之后查阅. sudo apt-get install python-pip sudo apt-get install ...
- 记录下 js各种证件的正则验证
身份证 /(^\d{15}$)|(^\d{17}([0-9]|X)$)/ 护照 /^[a-zA-Z0-9]{3,21}$/ /^(P\d{7})|(G\d{8})$/ 军官证或士兵证 ...