【Aizu - 0005 】GCD and LCM
GCD and LCM
Descriptions:
Write a program which computes the greatest common divisor (GCD) and the least common multiple (LCM) of given a and b.
Input
Input consists of several data sets. Each data set contains a and b separated by a single space in a line. The input terminates with EOF.
Constraints
- 0 < a, b ≤ 2,000,000,000
- LCM(a, b) ≤ 2,000,000,000
- The number of data sets ≤ 50
Output
For each data set, print GCD and LCM separated by a single space in a line.
Sample Input
8 6
50000000 30000000
Output for the Sample Input
2 24
10000000 150000000
题目链接:
https://vjudge.net/problem/Aizu-0005
多组输入,就是求这两个数的gcd(最大公约数)和lcm(最小公倍数)
注意数据有点大,保险起见用long long吧
AC代码
- #include <iostream>
- #include <cstdio>
- #include <fstream>
- #include <algorithm>
- #include <cmath>
- #include <deque>
- #include <vector>
- #include <queue>
- #include <string>
- #include <cstring>
- #include <map>
- #include <stack>
- #include <set>
- #include <numeric>
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- ll gcd(ll a,ll b){
- if(b==)
- return a;
- return gcd(b,a%b); //递归求最大公约数
- }
- ll lcm(ll a,ll b){
- return a/gcd(a,b)*b; //递归求最小公倍数
- }
- int main()
- {
- ll a,b;
- while(cin >> a >> b)
- {
- cout << gcd(a,b)<< " "<<lcm(a,b)<<endl;
- }
- return ;
- }
【Aizu - 0005 】GCD and LCM的更多相关文章
- 【51nod 2026】Gcd and Lcm
题目 已知 \(f(x)=\sum_{d|x}μ(d)∗d\) 现在请求出下面式子的值 \(\sum_{i=1}^{n}\sum_{j=1}^{n}f(gcd(i,j))∗f(lcm(i,j))\) ...
- 【CF#338D】GCD Table
[题目描述] 有一张N,M<=10^12的表格,i行j列的元素是gcd(i,j) 读入一个长度不超过10^4,元素不超过10^12的序列a[1..k],问是否在某一行中出现过 [题解] 要保证g ...
- 【BZOJ 2818】 GCD
[题目链接] 点击打开链接 [算法] 线性筛出不大于N的所有素数,枚举gcd(x,y)(设为p),问题转化为求(x,y)=p的个数 设x=x'p, y=y'p,那么有(x,y)=1且 ...
- 【Codeforces 582A】 GCD Table
[题目链接] 点击打开链接 [算法] G中最大的数一定也是a中最大的数. G中次大的数一定也是a中次大的数. 第三.第四可能是由最大和次大的gcd产生的 那么就不难想到下面的算法: ...
- 【HDU 5382】 GCD?LCM! (数论、积性函数)
GCD?LCM! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- 【51nod2026】Gcd and Lcm(杜教筛)
题目传送门:51nod 我们可以先观察一下这个$f(x)=\sum_{d|x}\mu(d) \cdot d$. 首先它是个积性函数,并且$f(p^k)=1-p \ (k>0)$,这说明函数$f( ...
- 【poj 2429】GCD & LCM Inverse (Miller-Rabin素数测试和Pollard_Rho_因数分解)
本题涉及的算法个人无法完全理解,在此提供两个比较好的参考. 原理 (后来又看了一下,其实这篇文章问题还是有的……有时间再搜集一下资料) 代码实现 #include <algorithm> ...
- 【Codeforces 582A】GCD Table
[链接] 我是链接,点我呀:) [题意] 给你一个数组A[]经过a[i][j] = gcd(A[i],A[j])的规则生成的二维数组 让你求出原数组A [题解] 我们假设原数组是A 然后让A数组满足A ...
- 【UVA 11426】gcd之和 (改编)
题面 \(\sum_{i=1}^{n}\sum_{j=1}^m\gcd(i,j)\mod998244353\) \(n,m<=10^7\) Sol 简单的一道莫比乌斯反演题 \(原式=\sum_ ...
随机推荐
- 2016 ACM/ICPC 区域赛(北京) E 题 bfs
https://vjudge.net/problem/UVALive-7672 题意 输入一个五位数n 问由12345变到n的操作最少次数 不可达输出-1 有三种操作 1.交换相邻的位置 次数不 ...
- 寒武纪camp Day1
补题进度:8/10 A(组合计数) 题意: 一个人站在数轴原点,每秒有1/4概率向前走一步,1/4概率向后走一步,1/2概率不动,问t秒后在p位置的概率. t,p<=100000 分析: 枚举不 ...
- win10安装mysql5.6,mysql启动时,闪退
首先在服务中查看是不是mysql启动了 发现在服务中没有mysql服务, 然后找到mysql的安装目录 MYSQL SERVER 5.6 中将my-default.ini 改为my.ini 使用命令行 ...
- Shell 脚本小试牛刀(5) -- 超便捷脚本之高速ssh 登录其它主机
假设你也是以Linux 为工作环境的童鞋,那么此文真是捷报!由于我的学习/工作中(特别是近期玩耍树莓派)常常会使用到ssh 登录其它主机,而每次使用ssh 登录都须要输入老长一大串让我非常烦.所以我写 ...
- Capture and report JavaScript errors with window.onerror
原文:https://blog.sentry.io/2016/01/04/client-javascript-reporting-window-onerror.html onerror is a sp ...
- leetCode 67.Add Binary (二进制加法) 解题思路和方法
Given two binary strings, return their sum (also a binary string). For example, a = "11" b ...
- EXCEPT(差集)集合运算
在集合论中,集合A与B的差集(A-B)是由属于集合A,但不属于集合B的元素组成的集合.可以认为两个集合的差A-B就是从A中减去B中也属于A的元素. 在T-SQL中,集合之差是用EXCEPT集合运算实现 ...
- Oracle改动字段类型
因为需求变动.现要将一个类型NUMBER(8,2)的字段类型改为 char. 大体思路例如以下: 将要更改类型的字段名改名以备份.然后加入一个与要更改类型的字段名同名的字段(原字段已经改名 ...
- MVC架构在游戏开发中的应用
一 定义 MVC即Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写. MVC是一种"前端"的设计模式. MV ...
- jquery 动态绑定bind()及模拟鼠标点击A链接
近来自觉前端有小小进步,幸而记之. 1.两个 css class 紧挨在一起 则在html元素中,要同时拥有这两个class,才能起作用 .block.db{ background-image:url ...