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

兔子坑问题

There is a hill with n holes around. The holes are signed from 0 to n-1.

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的更多相关文章

  1. 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 ...

  2. HDU 2504 又见GCD(最大公约数与最小公倍数变形题)

    又见GCD Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Subm ...

  3. Summary: gcd最大公约数、lcm最小公倍数算法

    欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数.其计算原理依赖于下面的定理: 定理:gcd(a,b) = gcd(b,a mod b) 证明:a可以表示成a = kb + ...

  4. [洛谷P1029]最大公约数与最小公倍数问题 题解(辗转相除法求GCD)

    [洛谷P1029]最大公约数与最小公倍数问题 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P, ...

  5. 辗转相除法求最大公约数和最小公倍数【gcd】

    要求最小公倍数可先求出最大公约数 设要求两个数a,b的最大公约数 伪代码: int yushu,a,b: while(b不等于0) { yushu=a对b求余 b的值赋给a yushu的值赋给b } ...

  6. Java求最大公约数和最小公倍数

    最大公约数(Greatest Common Divisor(GCD)) 基本概念 最大公因数,也称最大公约数.最大公因子,指两个或多个整数共有约数中最大的一个.a,b的最大公约数记为(a,b),同样的 ...

  7. 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)

    定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...

  8. [C]最大公约数和最小公倍数

    /*求最大公约数和最小公倍数 编写程序,在主函数中输入两个正整数 a,b,调用两个函数 fun1() 和 fun2(),分别求 a 和 b 的最大公约数和最小公倍数,在主函数中输出结果. */ #in ...

  9. codevs 1012 最大公约数和最小公倍数问题

    题目描述 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件:  1.P,Q是正整 ...

随机推荐

  1. bzoj3444: 最后的晚餐(并查集+组合数学)

    3444: 最后的晚餐 题目:传送门 题解: 考虑有解的情况: 直接上并查集,同一个联通块里的人一定要坐在一起的.不难发现其实对于每个联通块最多就只有两种排列方式,那就直接把大于等于两个人的联通块先去 ...

  2. [JZOJ 5888] [NOIP2018模拟9.29] GCD生成树 解题报告 (最大生成树+公约数)

    题目链接: http://172.16.0.132/senior/#main/show/5888 题目: 题解: 思路是这样的:两个数的最大公约数一定不会比这两个数的任意一个数大.因此我们把权值相等的 ...

  3. mysql实战45讲读书笔记(一) 一条SQL查询语句是如何执行的

    我们经常说,看一个事儿千万不要直接陷入细节里,你应该先鸟瞰其全貌,这样能够帮助你从高维度理解问题.同样,对于MySQL的学习也是这样.平时我们使用数据库,看到的通常都是一个整体.比如,你有个最简单的表 ...

  4. 新疆大学OJ(ACM) 1047: string 字符串排序

    1047: string 时间限制: 1 Sec  内存限制: 128 MB 题目描述 有n个字符串字符串n<=50000,把所有字符串串起来,得到一个字典序最小的字符串. 输入 输入第一行是一 ...

  5. HDU 1587 Flowers【贪心】

    题意:给出n种花的价钱,和总的金额m,问最多能够买到多少朵花.先排序,然后就是便宜的花在能够买的范围内能够多买就多买 #include<iostream> #include<cstd ...

  6. 之前的大数相加存在bug,这个ac通过了

    #include <iostream> #include <string> /*project:两个大整数相加 **@author:浅滩 **data:2019.05.15 * ...

  7. LayUI加载js无效问题

    在部署系统的时候,本地调试一切正常,layer.js均能正常加载.然而部署到服务器之后,经常性的出现layer.js无法加载问题.导致页面弹框无法使用. 一开始以为是Google浏览器问题,因为刚刚更 ...

  8. Virtualenv入门基础教程

    本文目录: [TOC]虚拟环境简介 VirtualEnv用于在一台机器上创建多个独立的Python虚拟运行环境,多个Python环境相互独立,互不影响,它能够: 在没有权限的情况下安装新套件    不 ...

  9. gps 地图

    http://www.cnblogs.com/sylvanas2012/p/5342530.html http://blog.csdn.net/ma969070578/article/details/ ...

  10. 实验了一下对于struct引用的成员的改动

    今天写代码的时候,不确定struct用引用传递给函数的时候,他的成员在函数里面改变的时候,是否能影响到外面. 实验了一下 #include <stdio.h> #include <s ...