CodeChef GCD2
GCD2Problem 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的更多相关文章
- scau 2015寒假训练
并不是很正规的.每个人自愿参与自愿退出,马哥找题(马哥超nice么么哒). 放假第一周与放假结束前一周 2015-01-26 http://acm.hust.edu.cn/vjudge/contest ...
- 【BZOJ-3514】Codechef MARCH14 GERALD07加强版 LinkCutTree + 主席树
3514: Codechef MARCH14 GERALD07加强版 Time Limit: 60 Sec Memory Limit: 256 MBSubmit: 1288 Solved: 490 ...
- 【BZOJ4260】 Codechef REBXOR 可持久化Trie
看到异或就去想前缀和(⊙o⊙) 这个就是正反做一遍最大异或和更新答案 最大异或就是很经典的可持久化Trie,从高到低贪心 WA: val&(1<<(base-1))得到的并不直接是 ...
- codechef 两题
前面做了这场比赛,感觉题目不错,放上来. A题目:对于数组A[],求A[U]&A[V]的最大值,因为数据弱,很多人直接排序再俩俩比较就过了. 其实这道题类似百度之星资格赛第三题XOR SUM, ...
- codechef January Challenge 2014 Sereja and Graph
题目链接:http://www.codechef.com/JAN14/problems/SEAGRP [题意] 给n个点,m条边的无向图,判断是否有一种删边方案使得每个点的度恰好为1. [分析] 从结 ...
- BZOJ3509: [CodeChef] COUNTARI
3509: [CodeChef] COUNTARI Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 339 Solved: 85[Submit][St ...
- CodeChef CBAL
题面: https://www.codechef.com/problems/CBAL 题解: 可以发现,我们关心的仅仅是每个字符出现次数的奇偶性,而且字符集大小仅有 26, 所以我们状态压缩,记 a[ ...
- CodeChef FNCS
题面:https://www.codechef.com/problems/FNCS 题解: 我们考虑对 n 个函数进行分块,设块的大小为S. 每个块内我们维护当前其所有函数值的和,以及数组中每个元素对 ...
- codechef Prime Distance On Tree(树分治+FFT)
题目链接:http://www.codechef.com/problems/PRIMEDST/ 题意:给出一棵树,边长度都是1.每次任意取出两个点(u,v),他们之间的长度为素数的概率为多大? 树分治 ...
随机推荐
- 2019-3-1-C#-json-转-xml-字符串
title author date CreateTime categories C# json 转 xml 字符串 lindexi 2019-03-01 09:20:24 +0800 2019-1-1 ...
- 一、VS支持Vue语法
一.VS支持Vue语法
- linux命令之 repeat 重复执行命令
$ vim ~/.bashrc function repeat() { number=$1 shift echo $@ for n in $(seq $number); do $@ done } $ ...
- Linux架构之Nginx 配置文件
第42章 nginx相关配置文件 1.Nginx主配置文件 路径 类型 作用 /etc/nginx/nginx.conf 配置文件 nginx主配置文件 /etc/nginx/conf.d/def ...
- 3.xml的解析
1.xml的解析原理简介(xml是标记型文档) (1)js使用dom解析标记型文档(html)? - 根据html的层级结构,在内存中分配一个树形结构,把html的标签,属性和文本都封装成对象 - d ...
- 两个list 合并后去除掉重复removeAll()的,然后再随机获取最后list中的 几个值
public static void test1(){ String s1="1,2,5,3,6,9"; String n1[]=s1.split(","); ...
- linux运维、架构之路-ansible批量管理
一.ansible软件 1.介绍 ①ansible是一个基于Python开发的自动化运维工具 ②其功能实现基于SSH远程连接服务 ③ansible可以实现批量系统配置.批量软件部署.批量文件拷贝.批量 ...
- Angular:自定义表单控件
分享一个最近写的支持表单验证的时间选择组件. import {AfterViewInit, Component, forwardRef, Input, OnInit, Renderer} from & ...
- 图论 List
题目 #A 小 K 的农场 (Unaccepted) #B 信息传递 (Unaccepted) #C 最短路计数 (Accepted) #D 通往奥格瑞玛的道路 (Accepted) ...
- Python 测评工具
开源--Python测评工具 Github仓库 本次实验作业的测评工具仅使用Python语言编写. 程序思路是基于文本的快速匹配. 编译test.py运行 1.GUI界面 GUI界面使用了PyQt5完 ...