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 = ;
int A,BB[N],num[N],tot;
char s[];
vector<int>B;
inline int gcd( int a , int b ) { return b == ? a : gcd(b,a%b); } bool check( int n ) {
int c = ;
for( int i = ; i < B.size() ; ++i ) {
c = c * + B[i];
c %= n ;
}
if( c == ) return true ;
return false;
} void Run() {
scanf("%d %s",&A,s);
tot = ;
int n = strlen(s) ;
if( A == ) { puts(""); return ; }
else if( A == ) { puts(s); return ; }
B.resize(n);
for( int i = ; i < strlen(s) ; ++i ) B[i] = s[i] - '';
for( int i = ; i <= A ; ++i ) if( A % i == ) {
num[tot++] = i ;
}
for( int i = tot- ; i >= ; --i ) if( check(num[i]) ) {
printf("%d\n",num[i]);
return ;
}
}
int main()
{
//freopen("in","r",stdin);
int _ , cas = ;
scanf("%d",&_);
while(_--)Run();
}

CodeChef GCD2的更多相关文章

  1. scau 2015寒假训练

    并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...

  2. 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树

    3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec  Memory Limit: 256 MBSubmit: 1288  Solved: 490 ...

  3. 【BZOJ4260】 Codechef REBXOR 可持久化Trie

    看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...

  4. codechef 两题

    前面做了这场比赛,感觉题目不错,放上来. A题目:对于数组A[],求A[U]&A[V]的最大值,因为数据弱,很多人直接排序再俩俩比较就过了. 其实这道题类似百度之星资格赛第三题XOR SUM, ...

  5. codechef January Challenge 2014 Sereja and Graph

    题目链接:http://www.codechef.com/JAN14/problems/SEAGRP [题意] 给n个点,m条边的无向图,判断是否有一种删边方案使得每个点的度恰好为1. [分析] 从结 ...

  6. BZOJ3509: [CodeChef] COUNTARI

    3509: [CodeChef] COUNTARI Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 339  Solved: 85[Submit][St ...

  7. CodeChef CBAL

    题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...

  8. CodeChef FNCS

    题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...

  9. codechef Prime Distance On Tree(树分治+FFT)

    题目链接:http://www.codechef.com/problems/PRIMEDST/ 题意:给出一棵树,边长度都是1.每次任意取出两个点(u,v),他们之间的长度为素数的概率为多大? 树分治 ...

随机推荐

  1. time模块的time方法、perf_counter方法和process_time方法的区别

    1. time.time()方法 返回自纪元以来的秒数作为浮点数,但是时期的具体日期和闰秒的处理取决于使用的平台.比如:在Windows和大多数Unix系统上,纪元是1970年1月1日00:00:00 ...

  2. tf.matmul / tf.multiply

    import tensorflow as tfimport numpy as np 1.tf.placeholder placeholder()函数是在神经网络构建graph的时候在模型中的占位,此时 ...

  3. 微服务+DDD代码结构例子

    这是一个基本的微服务+DDD演示例子: 基于 Spring Boot 1.5.6 , Spring Cloud Edgware.SR4 Version 微服务 + DDD,个人觉得应该是首先是从微服务 ...

  4. pycharm中能运行,但是往往py都要放到服务器上去跑,问题来了

    py文件在linux上运行,导包错误: 在py文件中添加项目的根目录: import sys sys.path.append('项目路径') sys.path.append(os.path.dirna ...

  5. Sublime-Text macOS 编译运行armadillo

    { "cmd" : ["g++ -std=c++14 -Wall -larmadillo -framework Accelerate ${file_name} -o ${ ...

  6. php array_sum()函数 语法

    php array_sum()函数 语法 作用:返回数组中所有值的和.大理石构件价格 语法:array_sum(arra) 参数: 参数 描述 array  必需.规定数组. 说明:如果所有值都是整数 ...

  7. [USACO17FEB]Why Did the Cow Cross the Road III G (树状数组,排序)

    题目链接 Solution 二维偏序问题. 现将所有点按照左端点排序,如此以来从左至右便满足了 \(a_i<a_j\) . 接下来对于任意一个点 \(j\) ,其之前的所有节点都满足 \(a_i ...

  8. [CSP-S模拟测试]:砖块(模拟)

    题目描述 在一个二维网格平面上,一个网格的坐标由其左下角的点的坐标定义$(x,y)$.在一个二维网格平面上,一个网格的坐标由其左下角的个点的坐标定义$(0,0)$的区域中,此时存在高度为$k$的初始砖 ...

  9. 多行文本溢出隐藏处理,兼容ie,火狐

    问题 多行文本溢出隐藏,webkit内核浏览器如谷歌支持如下写法: overflow: hidden; text-overflow: ellipsis; display: -webkit-box; - ...

  10. PHP 常用算法【总结】

    一.声明数组 ini_set("max_execution_time", "12000"); $arr = [2,4,1,7,33,4,5,6,7,11,1,0 ...