gcd(最大公约数)lcm(最小公倍数)E - Wolf and Rabbit
1.gcd
递归实现
int gcd(int i,int j)
{
if(j==0) return i;
else return gcd(j,i%j);
}
2、lcm
int gcd(int i,int j)
{
if(j==0) return i;
else return gcd(j,i%j);
}
int lcm(int i ,int j)
{
return i*j/gcd(i*j);
}
3.
E - Wolf and Rabbit
兔子坑问题
A rabbit must hide in one of the holes. A wolf searches the rabbit in anticlockwise order. The first hole he get into is the one signed with 0. Then he will get into the hole every m holes. For example, m=2 and n=6, the wolf will get into the holes which are signed 0,2,4,0. If the rabbit hides in the hole which signed 1,3 or 5, she will survive. So we call these holes the safe holes.
InputThe input starts with a positive integer P which indicates the number of test cases. Then on the following P lines,each line consists 2 positive integer m and n(0<m,n<2147483648).
OutputFor each input m n, if safe holes exist, you should output "YES", else output "NO" in a single line.
Sample Input
2
1 2
2 2
Sample Output
NO
YES
题目意思:狼抓兔子,输入m,n,狼能进入每相距为m的洞,洞的编号为0——n-1,如果狼每个洞都能进入,兔子就会被吃,问:兔子能不能存活?
如果狼每一个洞都能进去,兔子才会必死无疑,即在剔除m为1,这种特殊情况后,m,n,不能存在倍数关系,那么狼便能进入每一个洞,即他们的最大公约数为1.
#include <iostream>
using namespace std;
int gcd(int i,int j)
{
if(j==) return i;
else return gcd(j,i%j);
}
int main()
{
long long t,m,n;
cin>>t;
while(t--)
{
cin>>m>>n;
if(gcd(m,n)==)
cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}
gcd(最大公约数)lcm(最小公倍数)E - Wolf and Rabbit的更多相关文章
- gcd,最大公约数,lcm,最小公倍数
int gcd(int a,int b){ ?a:gcd(b,a%b); } 关于lcm,若写成a*b/gcd(a,b) ,a*b可能会溢出! int lcm(int a,int b){ return ...
- HDU 2504 又见GCD(最大公约数与最小公倍数变形题)
又见GCD Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- Summary: gcd最大公约数、lcm最小公倍数算法
欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数.其计算原理依赖于下面的定理: 定理:gcd(a,b) = gcd(b,a mod b) 证明:a可以表示成a = kb + ...
- [洛谷P1029]最大公约数与最小公倍数问题 题解(辗转相除法求GCD)
[洛谷P1029]最大公约数与最小公倍数问题 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P, ...
- 辗转相除法求最大公约数和最小公倍数【gcd】
要求最小公倍数可先求出最大公约数 设要求两个数a,b的最大公约数 伪代码: int yushu,a,b: while(b不等于0) { yushu=a对b求余 b的值赋给a yushu的值赋给b } ...
- Java求最大公约数和最小公倍数
最大公约数(Greatest Common Divisor(GCD)) 基本概念 最大公因数,也称最大公约数.最大公因子,指两个或多个整数共有约数中最大的一个.a,b的最大公约数记为(a,b),同样的 ...
- 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...
- [C]最大公约数和最小公倍数
/*求最大公约数和最小公倍数 编写程序,在主函数中输入两个正整数 a,b,调用两个函数 fun1() 和 fun2(),分别求 a 和 b 的最大公约数和最小公倍数,在主函数中输出结果. */ #in ...
- codevs 1012 最大公约数和最小公倍数问题
题目描述 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件: 1.P,Q是正整 ...
随机推荐
- inheritance in kentico
Visual inheritance http://devnet.kentico.com/docs/7_0/devguide/index.html?visual_inheritance.htm The ...
- git使用(公钥私钥产生--远程库添加公钥--本地库关联远程库-使用)
原文1:http://www.cnblogs.com/wangmingshun/p/5424767.html 原文2(指令):http://blog.csdn.net/xiaohanluo/artic ...
- BZOJ 2179 FFT模板
思路:FFT板子题 //By SiriusRen #include <cstdio> #include <complex> using namespace std; typed ...
- Bringing up the Avnet MicroZed with Vivado
Bringing up the Avnet MicroZed with Vivado I recently received the Adam Taylor Edition of Avnet's Zy ...
- SSRS参数不能默认全选的解决方法
解决方法选自<SQL Server 2008 R2 Reporting Services 报表服务>一书,亲测有效. 注意:参数默认值如果是字符串需要类型转换 =CStr("AL ...
- 获取xml字符串中的属性值
pagexml = @"<?xml version='1.0' encoding='utf-8'?> <DATAPACKET Version='2.0'> <M ...
- Swift学习笔记(7):函数
目录: 函数参数与返回值 参数标签和参数名称 可变参数 传入传出参数 函数类型 嵌套函数 函数是一段完成特定任务的独立代码片段,使用func标示函数名,使用->标示返回类型. ・可以为函数参数设 ...
- JS基本功 | JavaScript专题之数组 - 方法总结
Array.map() 1. map() 遍历数组 语法: let new_array = arr.map(function callback(currentValue, index, array ...
- java中的json
josn: 一种数据传输格式,与开发语言无关,轻量级 一开始是javaScript的,但是后面比较流传,几乎所有语言都有相应的使用API 数据结构: Object---对象 使用花括号{}包含的键值对 ...
- BZOJ 2741 L (可持久化01Trie+分块)
题目大意:给你一个序列,共有$q$个询问,每次询问区间$[L,R]$内最大连续字段异或和,强制在线,$n<=12000,m<=5000$ 有个细节没处理好$WA$了好久..还有一次$ans ...