HDU 5974 A Simple Math Problem
来源:互联网 发布:面包板入门单片机 编辑:程序博客网 时间:2024/06/05 11:32
Problem Description
Given two positive integers a and b,find suitable X and Y to meet the conditions:
X+Y=a
Least Common Multiple (X, Y) =b
Input
Input includes multiple sets of test data.Each test data occupies one line,including two positive integers a(1≤a≤2*10^4),b(1≤b≤10^9),and their meanings are shown in the description.Contains most of the 12W test cases.
Output
For each set of input data,output a line of two integers,representing X, Y.If you cannot find such X and Y,output one line of "No Solution"(without quotation).
Sample Input
6 8
798 10780
Sample Output
No Solution
308 490
Given two positive integers a and b,find suitable X and Y to meet the conditions:
X+Y=a
Least Common Multiple (X, Y) =b
Input
Input includes multiple sets of test data.Each test data occupies one line,including two positive integers a(1≤a≤2*10^4),b(1≤b≤10^9),and their meanings are shown in the description.Contains most of the 12W test cases.
Output
For each set of input data,output a line of two integers,representing X, Y.If you cannot find such X and Y,output one line of "No Solution"(without quotation).
Sample Input
6 8
798 10780
Sample Output
No Solution
308 490
题意:给定a,b,满足x+y=a、lcm(最小公倍数)(x,y)=b,求这样的x,y
思路:12w的测试数据,当然不能暴力。
首先肯定能想到x*y/gcd(x,y)==b; {gcd(a,b)代表a,b的最大公约数}
设gcd(x,y)=t;
i*t = x;
j*t = y;
则i和j肯定互质;
所以i+j和i*j也肯定互质(下面会用到此规则,不明白为什么的,可记住该结论)
i*t + j*t = a;
i*j*t = b;
由i+j和i*j互质,所以gcd(a,b) = t;(解决该题的关键一波转换)
故i+j = a/t;
i*j = b/t;
这马上联想到高中学的韦达定理;
构造二次函数t*x^2-a*t+b = 0;
用求根公式求出来即可得i , j; (取正根)
当然中间还有一些判别式的判定。
代码如下:
#include<stdio.h>#include<string.h>#include<iostream>#include<math.h>using namespace std;int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b);}int main(){ int a,b,t,x,y; while(~scanf("%d%d",&a,&b)) { t = gcd(a,b); //最大公约数 if(a*a-4*t*b<0) //判别式判断是否有根 printf("No Solution\n"); else { int i = ((a+sqrt(a*a-4*t*b))/(2*t)); x = i*t; y = a-x; if(x*y/gcd(x,y)==b) //判定解得的根是否正确 { if(x<=y) printf("%d %d\n",x,y); else printf("%d %d\n",y,x); } else printf("No Solution\n"); } } return 0;}
阅读全文
0 0
- hdu 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- HDU 5974 A Simple Math Problem(数论)
- hdu 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- HDU 5974 A Simple Math Problem 数学
- hdu 5974 A Simple Math Problem
- HDU 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- hdu 5974 A Simple Math Problem
- HDU 5974 A Simple Math Problem
- HDU-5974-A Simple Math Problem
- [HDU](5974)A Simple Math Problem ---数论
- hdu A Simple Math Problem
- A Simple Math Problem (HDU
- A Simple Math Problem HDU
- Hdu 6188 Duizi and Shunzi【思维+Dp】
- html,css
- 安卓学习-(数据存储(文件和SharedPreferences))
- 博弈论的纳什平衡的两个例题
- leetcode 30. Substring with Concatenation of All Words
- HDU 5974 A Simple Math Problem
- Spark 之 一些常用的操作
- 中企动力全面赋能中小企业 让大数据场景化营销落地生花
- CUDA6.5+VS2010安装和搭建过程
- 8086CPU端口读写
- webrtc libjingle worker thread
- HTML与CSS练习——js选择器
- 安卓学习-(数据存储(数据库,原生sql,和原生安卓操作))
- spring_定时任务_spring-task_定时输出