Benefit UVA - 11889(已知LCM和其中一个数,求另一个数)
首先对于C不能整除A的状况肯定排除
然后得到B=C/A
然后取G=GCD(A,B)
如果G==1,那么此时B就是解
否则的话,就证明A,B,的最小公倍数肯定不是C,因为其最小公倍数是A*B/G
那么我们就去掉这个公因子,方法是A/G,B*G
即可消去两者公共的倍数,同时还可以保证A*B是一个定值
循环直到G==1为止。。。是。。。是。。是。。。挺神奇的。。。
题意借鉴自https://blog.csdn.net/libin56842/article/details/46442083
https://blog.csdn.net/just_sort/article/details/50983350
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
const int maxn = , INF = 0x7fffffff;
typedef long long LL; int gcd(int a, int b)
{
return b==?a:gcd(b, a%b);
} int main()
{
int T;
cin>> T;
while(T--)
{
int a, b, c;
cin>> a >> c;
if(c % a != )
cout<< "NO SOLUTION" <<endl;
else
{
b = c / a;
if(gcd(a, b) == )
cout<< b <<endl;
else
{
while(gcd(a, b) != )
{
int temp = gcd(a, b);
a /= temp;
b *= temp;
}
cout<< b <<endl;
}
} } return ;
}
Benefit UVA - 11889(已知LCM和其中一个数,求另一个数)的更多相关文章
- 转 已知两点坐标和半径求圆心坐标程序C++
数学思想:利用圆方程和直线方程 已知两点坐标和半径求圆心坐标程序 #include <iostream> #include <fstream> #include <c ...
- NX二次开发-UFUN已知两个向量方向求夹角角度UF_VEC3_angle_between
NX9+VS2012 #include <uf.h> #include <uf_ui.h> #include <uf_vec.h> #include <uf_ ...
- 【C语言】已知三角形三边长,求三角形面积
一. 数学基础: 已知三角形的三边,计算三角形面积,需要用到海伦公式: 即p=(a+b+c)/2 二. 算法: 输入三个边长,套用海伦公式计算面积,并输出. 可以先判断是否可以构成三角形,即任意两边之 ...
- Tree UVA - 548 已知中序遍历和后序遍历,求这颗二叉树。
You are to determine the value of the leaf node in a given binary tree that is the terminal node of ...
- 又见GCD (已知最大公约数和其中一个数求另一个数)
#include<cstdio> int f1(int a,int b) //最大公约数 { ) return b; else return f1(b,a%b); } int f2(int ...
- ACM题目————已知前序和中序求后序
#include <iostream> #include <cstring> #include <cstdio> using namespace std; ], z ...
- HDU 1710Binary Tree Traversals(已知前序中序,求后序的二叉树遍历)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1710 解题思路:可以由先序和中序的性质得到 : 先序的第一个借点肯定是当前子树的根结点, 那么在 中序 ...
- unity 已知cosA和sinA,求A
和c++中的atan2(y,x)类似,unity中有也Mathf.Atan2(y,x).
- 已知一棵完全二叉树,求其节点的个数 要求:时间复杂度低于O(N),N为这棵树的节点个数
package my_basic.class_4; public class Code_08_CBTNode { // 完全二叉树的节点个数 复杂度低于O(N) public static class ...
随机推荐
- java中sleep()方法的解析
Thread.sleep(3000); 就是指让当前正在运行的占用cpu时间片的线程挂起3000ms,把cpu的时间片交给其他线程,但是并没有指定把CPU的时间片接下来到底交给哪个线程,而是让这些线程 ...
- day75
昨日回顾: 1 inclusion_tag -干什么用的?生成html的片段(动态,传参数,传数据) -app下新建一个模块,templatetags -创建一个py文件(mytag.p ...
- DB2 substr,instr使用
看示例,查询下一年'2xxx',例如今年2014,结果为2015 select substr(char(current timestamp),1,4)+1 from SYSIBM.SYSDUMMY1; ...
- Linux中的do{...} while(0)
摘抄自: <<Linux设备驱动开发详解(第二版)>> 在 Linux 内核中,经常会看到do{} while(0)这样的语句,许多人开始都会疑惑,认为do{} while(0 ...
- Cannot connect to the Docker daemon at unix:///var/run/docker.sock.问题解决
出现Cannot connect to the Docker daemon at unix:///var/run/docker.sock时,先用tail -5f /var/log/upstart/do ...
- 20155323刘威良《网络对抗》Exp3 免杀原理与实践
20155323刘威良<网络对抗>Exp3 免杀原理与实践 实践内容 1 正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,自己利用shellco ...
- 利用git将项目上传到github
本文主要介绍如果用git将项目上传到githup. 一.准备工作 (1)欲将项目上传到githup,先在githup上新建一个仓库.这里就不介绍. (2 ...
- VS与Opencv的亲密接触之安装配置过程
最近想把FPGA采集的图像,上传到上位机显示,看到Opencv能帮大忙,所以就折腾折腾! 我用的是VS2012和opencv-2.4.10-2.4.10(目前的最新版本),那个版本无所谓,本文都将适用 ...
- Docker-compose部署gitlab中文版
目录 Docker-compose部署gitlab 1.安装Docker 2.安装Docker-compose 3.安装Gitlab Docker-compose部署gitlab 1.安装Docker ...
- JS设置状态栏
JS设置状态栏可通过window.status = str来设置,在后台可通过 ClientScript.RegisterStartupScript( this.GetType(), "12 ...