HDU ACM 5019 Revenge of GCD

分析:只需要求出最大公约数,然后枚举最大公约数的因子,把他们保存起来在求第K大的;因为是最大公约数的因子必然是两个数的因子。另外循环变量i和个数cnt都要声明为__int64,否则出错。

#include<iostream>
#include<algorithm>
using namespace std;

__int64 gcd(__int64 x,__int64 y)
{
	__int64 r;

	while(y)
	{
		r=x%y;
		x=y;
		y=r;
	}
	return x;
}

__int64 f[1000005];

int main()
{
	int T;

	__int64 X,Y,K,_gcd,i,cnt;

	scanf("%d",&T);
	while(T--)
	{
		scanf("%I64d %I64d %I64d",&X,&Y,&K);

		_gcd=gcd(X,Y);
		cnt=0;
		for(i=1;i*i<=_gcd;i++)
		{
			if(_gcd%i==0)
			{
				if(i*i==_gcd)
					f[cnt++]=i;
				else
				{
					f[cnt++]=i;
					f[cnt++]=_gcd/i;
				}
			}
		}
		if(K>cnt)
			puts("-1");
		else
		{
			sort(f,f+cnt);
			printf("%I64d\n",f[cnt-K]);
		}
	}
    return 0;
}
时间: 2024-10-12 11:52:04

HDU ACM 5019 Revenge of GCD的相关文章

杭电(hdu)5019 Revenge of GCD

Revenge of GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1724    Accepted Submission(s): 472 Problem Description In mathematics, the greatest common divisor (gcd), also known as the great

HDU 5019 Revenge of GCD

Revenge of GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 430    Accepted Submission(s): 122 Problem Description In mathematics, the greatest common divisor (gcd), also known as the greate

hdu 5019 Revenge of GCD(BestCoder Round #10)

Revenge of GCD                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 668    Accepted Submission(s): 195 Problem Description In mathematics

HDOJ 5019 Revenge of GCD

第k大GCD = GCD/第K大因子 Revenge of GCD Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 877    Accepted Submission(s): 259 Problem Description In mathematics, the greatest common divisor (gcd), also

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 i

HDU ACM 2504 又见GCD

分析:水题,但要注意当c取2*b时,a可能被c整除,此时a继续加b继续判断. #include<iostream> using namespace std; int gcd(int a,int b) { int r; while(b) { r=a%b; a=b; b=r; } return a; } int get(int a,int b) { int c; c=2*b; while(gcd(c,a)!=b) c+=b; return c; } int main() { int n,a,b;

HDU ACM 1098 Ignatius&#39;s puzzle

分析:裴蜀定理,a,b互质的充要条件是存在整数x,y使ax+by=1.存在整数x,y,使得ax+by=c,那么c就是a,b的公约数. 假设存在数a ,因为对任意x方程都成立,则有当x=1时f(x)=18+ka;有因为f(x)能被65整除,所以f(x)=n*65.即18+ka=n*65有整数解则说明假设成立. ax+by = c的方程有整数解的一个充要条件是:c%gcd(a, b) == 0.然后枚举直到(65*n-18)%k == 0. #include<iostream> using nam

HDU ACM 1103 Flo&#39;s Restaurant

分析:借助STL的min_element实现.每次更新最先被占用的桌子,具体见注释. #include<iostream> #include<algorithm> using namespace std; int main() { int A,B,C; char s[10]; int a[102],b[102],c[102]; int curtime,count,ans; int *p; //桌子最先空闲时间 while(cin>>A>>B>>C

hdu acm 1425 sort(哈希表思想)

sort Time Limit: 6000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25803    Accepted Submission(s): 7764 Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且