码迷,mamicode.com
首页 > 其他好文 > 详细

CodeChef GCD2

时间:2015-02-21 14:20:41      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

GCD2

 
Problem code: GCD2
 

All submissions for this problem are available.

Frank explained its friend Felman the algorithm of Euclides to calculate the GCD
of two numbers. Then Felman implements it algorithm

int gcd(int a, int b)
{
	if (b==0)
		return a;
	else
		return gcd(b,a%b);
}

and it proposes to Frank that makes it
but with a little integer and another integer that has up to 250 digits.

Your task is to help Frank programming an efficient code for the challenge of Felman.

Input

The first line of the input file contains a number representing the number of lines to follow.
Each line consists of two number A and B (0 <= A <= 40000 and A <= B < 10^250).

Output

Print for each pair (A,B) in the input one integer representing the GCD of A and B.

Example

Input:
2
2 6
10 11


Output:
2
1

 

求一个大数 , 一个<=4W整数的GCD 。。

枚举整数的约数, 模拟大数除法 ,用大数除去这些约数,判一下余数是否为0

 

技术分享
#include <bits/stdc++.h>

using namespace std;
const int N = 100010;
int A,BB[N],num[N],tot;
char s[300];
vector<int>B;
inline int gcd( int a , int b ) { return b == 0 ? a : gcd(b,a%b); }

bool check( int n ) {
    int c = 0 ;
    for( int i = 0 ; i < B.size() ; ++i ) {
        c = c * 10 + B[i];
        c %= n ;
    }
    if( c == 0 ) return true ;
    return false;
}

void Run() {
    scanf("%d %s",&A,s);
    tot = 0;
    int n = strlen(s) ;
    if( A == 1 ) { puts("1"); return ; }
    else if( A == 0 ) { puts(s); return ; }
    B.resize(n);
    for( int i = 0 ; i < strlen(s) ; ++i ) B[i] = s[i] - 0;
    for( int i = 1 ; i <= A ; ++i ) if( A % i == 0 ) {
        num[tot++] = i ;
    }
    for( int i = tot-1 ; i >= 0 ; --i ) if( check(num[i]) ) {
        printf("%d\n",num[i]);
        return ;
    }
}
int main()
{
    //freopen("in","r",stdin);
    int _ , cas = 1 ;
    scanf("%d",&_);
    while(_--)Run();
}
View Code

 

CodeChef GCD2

标签:

原文地址:http://www.cnblogs.com/hlmark/p/4296952.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!