BestCoder10 1002 Revenge of GCD(hdu 5019) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019
题目意思:给出 X 和 Y,求出 第 K 个 X 和 Y 的最大公约数。 例如8 16,它们的公约数依次为1 2 4 8,那么第 3 个 GCD(X, Y) = 2,也就是从后往前数第3个公共因子。
TLE思路:求出 X 的所有因子(从小到大开始存储),再从后往前枚举这些因子,检查是否也为 Y 的因子,统计到第 K 个数就是答案了......以为可以顺利通过,原来数据量还是非常大滴!!!
正确思路:先求出 X 和 Y 的最大公约数 r,然后再求出 r 的 所有因子,从后往前数第 K 个就是答案了。这样做的依据是, r 的所有因子一定为 X 和 Y 的公共因子,也就符合题目要求啦~~~
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; typedef __int64 ll;
const ll maxn = 1e6 + ;
ll X, Y, K, ansi;
ll com_factor[maxn]; ll gcd(ll x, ll y)
{
while (y)
{
ll r = x % y;
x = y;
y = r;
}
return x;
} int main()
{
int T, cnt;
while (scanf("%d", &T) != EOF)
{
while (T--)
{
scanf("%I64d%I64d%I64d", &X, &Y, &K);
if (X < Y)
swap(X, Y);
ll r = gcd(X, Y);
cnt = ;
for (ll i = ; i*i <= r; i++)
{
if (r % i == )
{
com_factor[++cnt] = i;
if (r/i != i)
com_factor[++cnt] = r/i;
}
}
sort(com_factor+, com_factor+cnt+);
reverse(com_factor+, com_factor+cnt+);
printf("%I64d\n", cnt < K ? - : com_factor[K]);
}
}
return ;
}
BestCoder10 1002 Revenge of GCD(hdu 5019) 解题报告的更多相关文章
- BestCoder10 1001 Revenge of Fibonacci(hdu 5018) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018 题目意思:给出在 new Fibonacci 中最先的两个数 A 和 B(也就是f[1] = A ...
- BestCoder8 1002 Revenge of Nim(hdu 4994) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4994 题目意思:有 n 个 heap(假设从左至右编号为1-n),每个 heap 上有一些 objec ...
- BestCoder9 1003 Revenge of kNN(hdu 4995) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4995 题目意思:在一个一维坐标轴上,给出位置 xi 和值 vi,对于 M 次询问,每次询问给出inde ...
- BestCoder4 1002 Miaomiao's Geometry (hdu 4932) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932 题目意思:给出 n 个点你,需要找出最长的线段来覆盖所有的点.这个最长线段需要满足两个条件:(1 ...
- BestCoder16 1002.Revenge of LIS II(hdu 5087) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5087 题目意思:找出第二个最长递增子序列,输出长度.就是说,假如序列为 1 1 2,第二长递增子序列是 ...
- BestCoder18 1002.Math Problem(hdu 5105) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5105 题目意思:给出一个6个实数:a, b, c, d, l, r.通过在[l, r]中取数 x,使得 ...
- BestCoder17 1002.Select(hdu 5101) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5101 题目意思:给出 n 个 classes 和 Dudu 的 IQ(为k),每个classes 都有 ...
- BestCoder6 1002 Goffi and Squary Partition(hdu 4982) 解题报告
题目链接:http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?pid=1002&cid=530 (格式有一点点问题,直接粘 ...
- BestCoder22 1002.NPY and arithmetic progression(hdu 5143) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5143 题目意思:给出 1, 2, 3, 4 的数量,分别为a1, a2, a3, a4,问是否在每个数 ...
随机推荐
- ThinkPHP多表联合查询的常用方法
1.原生查询示例: $Model = new Model(); $sql = 'select a.id,a.title,b.content from think_test1 as a, think_t ...
- bzoj 1493 暴力
我们可以枚举每个点,然后求出这个点到其余点最小消耗的代价,求出比t小的且距离最大的更新答案. /**************************************************** ...
- 洛谷P2633 王后万岁
题目描述 byteland的王后深受百姓爱戴.为了表达他们的爱,国民们打算占领一个新的国家,并以王后的名字命名.这个国家有n座城市.城市之间有双向道路连接,且每两个城市之间有且仅有一条道路.每座城市对 ...
- 洛谷P2507 [SCOI2008]配对
题目背景 四川NOI2008省选 题目描述 你有 n 个整数Ai和n 个整数Bi.你需要把它们配对,即每个Ai恰好对应一个Bp[i].要求所有配对的整数差的绝对值之和尽量小,但不允许两个相同的数配对. ...
- C#文件复制功能
目的是将用户自定义文件复制到指定文件夹并且能查看该文件,下面是个人做的源码: sing System; using System.Collections.Generic; using System.C ...
- 两个大的整数的运算(java)
import java.math.BigInteger; public class BigInt { BigInteger m1; BigInteger m2; BigInteger m3; BigI ...
- jsp------实现MD5加密
index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UT ...
- mysql zip 版本配置方法
-\bin 指 C:\Program Files\MySQL\MySQL Server 5.6\bin 1.增加环境变量 "PATH"-"-\bin" 2.修改 ...
- 转 web项目中的web.xml元素解析
转 web项目中的web.xml元素解析 发表于1年前(2014-11-26 15:45) 阅读(497) | 评论(0) 16人收藏此文章, 我要收藏 赞0 上海源创会5月15日与你相约[玫瑰里 ...
- C# 根据IP地址获取城市
using System; using System.IO; using System.Net; using System.Text; using System.Web.Script.Serializ ...