hdu 2504 又见gcd(gcd)
2015-03-25 08:33
274 查看
又见GCD
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12117 Accepted Submission(s): 5146
Problem Description
有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b。若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c。
Input
第一行输入一个n,表示有n组测试数据,接下来的n行,每行输入两个正整数a,b。
Output
输出对应的c,每组测试数据占一行。
Sample Input
2 6 2 12 4
Sample Output
4 8
Source
《ACM程序设计》短学期考试_软件工程及其他专业
题目大意:求取他t=a/b的值,然后找到与t互质的最小的数即可
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; int n,a,b; int gcd ( int a , int b ) { return b?gcd(b,a%b):a; } int main ( ) { while ( ~scanf ( "%d" , &n ) ) { while ( n-- ) { scanf ( "%d%d" , &a , &b ); int t = a/b; if ( b == 1 ) printf ( "%d\n" , a == 2 ? 3:2 ); else { int i = 2; while ( i*b < 100001 ) { if ( gcd ( i , t ) == 1 ) break; else i++; } printf ( "%d\n" , i*b ); } } } }
相关文章推荐
- HDU 2504 又见GCD
- 【hdu 2504】又见GCD(数学相关)
- hdu 2504 又见GCD
- HDU 2504 又见GCD
- 又见gcd(hdu-2504)
- hdu 2504 又见GCD
- hdu 2504 又见gcd
- hdu 2504 又见GCD
- HDU 2504 又见GCD (最大公因数+暴力)
- HDU 2504.又见GCD
- C - 又见GCD HDU - 2504
- hdu 2504 又见gcd
- hdu-2504-又见GCD
- hdu 2504 又见Gcd
- 又见GCD(hdu 2504)
- HDU 2504 GCD
- hdu 2504 又见GCD
- hdu2504又见GCD
- hdu 2504 又见gcd
- HDU 2504 又见GCD