gcd模板

 1 #include<iostream>
 2 using namespace std;
 3 int gcd(int a,int b){
 4     if(a>b){
 5         if(a%b!=0)  return gcd(b,a%b);
 6         else  return b;
 7     }
 8     else gcd(b,a);
 9 }
10 int main(){
11     int a,b;
12     while(cin>>a>>b)
13     cout<<gcd(a,b)<<endl;
14     return 0;
15 }

gcd模板,布布扣,bubuko.com

时间: 2024-08-23 12:57:26

gcd模板的相关文章

exgcd&amp;gcd模板

//返回d=gcd(a,b);和对应于等式ax+by=d中的x,y long long extend_gcd(long long a,long long b,long long &x,long long &y) { if(a==0&&b==0) return -1;//无最大公约数 if(b==0){x=1;y=0;return a;} long long d=extend_gcd(b,a%b,y,x); y-=a/b*x; return d; } //*********求

xudyh的gcd模板

hdu 5019 1 #include <cstdlib> 2 #include <cctype> 3 #include <cstring> 4 #include <cstdio> 5 #include <cmath> 6 #include <algorithm> 7 #include <vector> 8 #include <string> 9 #include <iostream> 10 #in

gcd 模板

给 x,y 两个数,求 x,y 的最大公因数. 辗转相除法,直接套!!! 1 function gcd(x,y:longint):longint; 2 begin 3 if y=0 then exit(x) else exit(gcd(y,x mod y)); 4 end; 原文地址:https://www.cnblogs.com/t-s-y/p/10324892.html

hdu1108【gcd算法】

直接上递归版gcd模板,直接while的话效率更高~ 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 int gcd(int a, int b) { 6 return a % b ? gcd(b, a % b) : b; 7 } 8 9 int main() { 10 int a, b; 11 while(cin >> a >> b) { 12 int c = gcd(a

HDU 1576 (乘法逆元)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1576 题目大意:求(A/B)mod 9973.但是给出的A是mod形式n,n=A%9973. 解题思路: 两种思路,一种从乘法逆元角度,另一种从扩展GCD推公式角度. ①乘法逆元: 先来看下逆元和乘法逆元的关系,对于A*X=B,有X=A-1*B,A-1就是普通的逆元了,在这里就是倒数. 如果A*X=B mod n,变成同余式了,那么A-1依然是存在的,只不过不是倒数了,一般把同余之后的逆元称为乘法

2016 ACM/ICPC Asia Regional Qingdao Online HDU5883

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5883 解法:先判断是不是欧拉路,然后枚举 #pragma comment(linker, "/STACK:102400000,102400000") #include <math.h> #include <time.h> #include <stdio.h> #include <string.h> #include <stdlib.h>

ACM-ICPC国际大学生程序设计竞赛北京赛区(2016)网络赛 The Book List

描述 The history of Peking University Library is as long as the history of Peking University. It was build in 1898. At the end of year 2015, it had about 11,000 thousand volumes of books, among which 8,000 thousand volumes were paper books and the othe

OCAC暑期比赛第一场 K题 最大公约数 题解

最大公约数[题目描述]给你两个int范围内的正整数,求它们的最大公约数.[输入格式]输入的一行包含两个正整数 a 和 b (1<=a,b<=2^31),以空格分隔.[输出格式]输出 a 和 b 的最大公约数.[样例输入]6 8[样例输出]2[题目分析]gcd模板题.我们可以用 STL 提供的 __gcd 函数直接进行运算,也可以自己模拟 gcd 函数来写.自己模拟实现的代码: #include <bits/stdc++.h> using namespace std; int gcd

模板 求GCD&amp;LCM

求最大公倍数 1 int GCD(int a,int b) 2 { 3 if(a % b == 0) return b; 4 else 5 return GCD(b,a%b); 6 } 求最小公倍数 1 int LCM(int a,int b) 2 { 3 return a*a/GCD(a,b); //最小公倍数等于两数乘积除以最大公约数 4 }