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. UNIX网络编程总结一

    客户与服务器通信使用TCP在同一网络通信时,大致按下面的方式通信:client→TCP→IP→以太网驱动程序→以太网→以太网驱动程序→IP→TCP→server.若不在同一网络则需要路由器连接. 客户 ...

  2. SpringMVC的@ResponseBody注解简介

    SpringMVC简介 SpringMVC也叫Spring Web MVC 属于展示层框架.是Spring框架的一部分. 核心组件类DispatherServlet springMVC是围绕Dispa ...

  3. flask之注册功能

    一:注册功能 1:前端准备表单 # 前端代码 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  4. home home clock

    博客园添加home home clock 将下面代码复制进博客侧边栏广告即可 <div id="myTime"> <object classid="cl ...

  5. MTV和MVC的区别

    著名的MVC模式:方便解藕 所谓的MVC就是把web应用分为三层 1.模型层: model     负责业务对象和数据库的对象(ORM)的映射 2.视图层       views 负责与用户的交互(书 ...

  6. JAVA中的反射机制 详解

    主要介绍以下几方面内容 理解 Class 类 理解 Java 的类加载机制 学会使用 ClassLoader 进行类加载 理解反射的机制 掌握 Constructor.Method.Field 类的用 ...

  7. hdu 4845 : 拯救大兵瑞恩 (bfs+状态压缩)

    题目链接 #include<bits/stdc++.h> using namespace std; typedef long long LL; int n,m,p,s,k; ,,,-}; ...

  8. Dubbo学习-2-注册中心搭建

    1.Dubbo支持如下几种注册中心,推荐使用zookeeper来作为注册中心. 2. 下载zookeeper https://zookeeper.apache.org/releases.html#do ...

  9. 重置Jenkins的构建历史

    1.重置单个JOB的构建历史item = Jenkins.instance.getItemByFullName("your-job-name-here") //THIS WILL ...

  10. 关于KindEditor编辑器,word文档中的图文全选粘贴,在编辑器中不显示图片的问题

    图片的复制无非有两种方法,一种是图片直接上传到服务器,另外一种转换成二进制流的base64码 目前限chrome浏览器使用,但是项目要求需要支持所有的浏览器,包括Windows和macOS系统.没有办 ...