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),他们之间的长度为素数的概率为多大? 树分治 ...
随机推荐
- ASE Alpha Sprint - backend scrum 9
本次scrum于2019.11.14再sky garden进行,持续15分钟. 参与人: Xin Kang, Zhikai Chen, Jia Ning, Hao Wang 请假: Lihao Ran ...
- linux配置 sudo 授权管理
为什么使用 sudo,如果普通用户使用 su - root 切换到管理员.进行非法操作,比如 passwd root 修改 root 密码.那么系统其他用户将无法访问系统.这个普通管理员说白了,已经” ...
- express 获取post 请求参数
在 Express 中没有内置获取表单 POST 请求体的 API , 我们需要添加第三方插件库 安装: npm install --save body-parser 配置: var bodyPars ...
- 三、Windows下用FFmpeg+nginx+rtmp搭建直播环境 实现推流、拉流
一.环境 1.开发环境:windows 2.开发工具:FFmpeg.nginx.nginx-rmtp-module (链接:https://pan.baidu.com/s/119d2GeMzddas_ ...
- 常见3种Git服务器的构建
学习Git不同的服务器形式,具体如下: - 创建SSH协议服务器 - 创建Git协议服务器 - 创建HTTP协议服务器 方案: Git支持很多服务器协议形式,不同协议的Git服务器,客户端就可以使用不 ...
- Nginx的反向调度功能
1.案例实现Nginx反向代理; 2.反向代理的理论知识拓展. 一, 实验Nginx的反向代理功能 使用Nginx实现Web反向代理功能,实现如下功能: 后端Web服务器两台,可以使用httpd实现 ...
- UNIX网络编程总结三
套接口结构 IPv4套接口地址结构: 1 2 3 4 5 6 7 struct sockaddr_in{/*16字节*/ uint8_t sin_len; /*结构体长度,8位*/ sa_famil ...
- 12JDBC
1.JDBC概述 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java ...
- [sqlmap源码阅读] 数据库识别
通过网页返回的数据库错误信息识别网站所有数据库类型,用到的正则表达式及支持识别的数据库类型,这些信息以xml文件的形式存在,使用 sax 解析xml.
- pspice中参数的意义
摘自:http://royroyyy.blog.163.com/blog/static/137650617201102610471196/ 有源器件在符号库中的名称(NAME)通常以关键字开头,后根长 ...