A Simple Math Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2891    Accepted Submission(s): 907

Problem DescriptionGiven 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
 

题意:给你 x+y=a, lcm(x,y)=b 求 a,b

思路:

易得:x/gcd+y/gcd=a/gcd, x/gcd*y/gcd=b/gcd; 令i=x/gcd, j=y/gcd; 即i*gcd+j*gcd=a,i*j*gcd=b ==> gcd(a,b)=gcd(x,y);解个方程即可。

PS:刚开始用的枚举gcd的方法,复杂度1e7,但是一直WA???

代码:

 #include<bits/stdc++.h>
//#include<regex>
#define db double
#include<vector>
#define ll long long
#define vec vector<ll>
#define Mt vector<vec>
#define ci(x) scanf("%d",&x)
#define cd(x) scanf("%lf",&x)
#define cl(x) scanf("%lld",&x)
#define pi(x) printf("%d\n",x)
#define pd(x) printf("%f\n",x)
#define pl(x) printf("%lld\n",x)
#define MP make_pair
#define PB push_back
#define inf 0x3f3f3f3f3f3f3f3f
#define fr(i,a,b) for(int i=a;i<=b;i++)
const int N=1e3+;
const int mod=1e9+;
const int MOD=mod-;
const db eps=1e-;
const db PI=acos(-1.0);
using namespace std;
map<ll,int> mp;
int main(){
//freopen("data.in","r",stdin);
//freopen("data.out","w",stdout);
ll a,b;
for(int i=;i<=;i++){
ll x=1ll*i*i;
mp[x]=i;
}
while(scanf("%lld%lld",&a,&b)==){
bool ok=;
ll i=__gcd(a,b);
// for(ll i=1;i*i<=a;i++) {
if(a%i==&&b%i==){
ll bb=b/i,aa=a/i,y=aa*aa-*bb;
if(y<||!mp.count(y)||(aa+mp[y])%==){printf("No Solution\n");continue;}
ll ans1=(aa+mp[y])/;
ll ans2=(aa-mp[y])/;
ans1*=i,ans2*=i;
ll cnt1=a-ans1,cnt2=a-ans2;
if(cnt1*ans1/__gcd(cnt1,ans1)==b){
printf("%lld %lld\n",cnt1,ans1);
}
else if(cnt2*ans2/__gcd(cnt2,ans2)==b){
printf("%lld %lld\n",cnt2,ans2);
}
}
// } }
return ;
}
 
 

HDU 5974 数学的更多相关文章

  1. HDU 5984 数学期望

    对长为L的棒子随机取一点分割两部分,抛弃左边一部分,重复过程,直到长度小于d,问操作次数的期望. 区域赛的题,比较基础的概率论,我记得教材上有道很像的题,对1/len积分,$ln(L)-ln(d)+1 ...

  2. HDU 5974 A Simple Math Problem 数学题

    http://acm.hdu.edu.cn/showproblem.php?pid=5974 遇到数学题真的跪.. 题目要求 X + Y = a lcm(X, Y) = b 设c = gcd(x, y ...

  3. HDU 5976 数学,逆元

    1.HDU 5976 Detachment 2.题意:给一个正整数x,把x拆分成多个正整数的和,这些数不能有重复,要使这些数的积尽可能的大,输出积. 3.总结:首先我们要把数拆得尽可能小,这样积才会更 ...

  4. *HDU 2451 数学

    Simple Addition Expression Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  5. [ An Ac a Day ^_^ ] hdu 4565 数学推导+矩阵快速幂

    从今天开始就有各站网络赛了 今天是ccpc全国赛的网络赛 希望一切顺利 可以去一次吉大 希望还能去一次大连 题意: 很明确是让你求Sn=[a+sqrt(b)^n]%m 思路: 一开始以为是水题 暴力了 ...

  6. HDU 5974 A Simple Math Problem(数论+结论)

    Problem Description Given two positive integers a and b,find suitable X and Y to meet the conditions ...

  7. hdu 4506(数学,循环节+快速幂)

    小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  8. hdu 4432 数学杂题

    http://acm.hdu.edu.cn/showproblem.php?pid=4432 6分钟写的代码,一上午去调试,, 哎,一则题目没看懂就去写了,二则,哎,,恶心了.在坚持几天然后ACM退役 ...

  9. HDU - 5974 A Simple Math Problem (数论 GCD)

    题目描述: Given two positive integers a and b,find suitable X and Y to meet the conditions: X+Y=a Least ...

随机推荐

  1. macaca 测试web(2)

    还是以博客园登录为例, 可以查看上一篇上一篇 这次只测试登录,使用ddt的数据驱动,这里我博客也有记录 地址 其实我在uiautomator +python 实现安卓自动化 一文当中我已经使用过ddt ...

  2. selenium 对chrome浏览器操作

    参照http://www.testwo.com/blog/6931博客内容 1.下载ChromeDriver驱动包(下载地址: http://chromedriver.storage.googleap ...

  3. httpd常用配置

    author:JevonWei 版权声明:原创作品 检查配置文件时,如下提示,则因为没有server的服务名称导致,故设置网站的服务server名称,若没有设置web服务名,主默认解析系统主机名(添加 ...

  4. .net基础第一天

    .net基础 <head>+控制类标签 +<head> 1  <body>+网页要显示的内容+</body> <body +空格+bgcolor= ...

  5. Project 10:简单图像的绘制

    目标:绘制如图图像 #include <stdio.h> int main() { int n,i,j,o,k=0; printf("请输入一个数:"); scanf( ...

  6. JS嵌套循环的典型练习题

    1.斐波那契数列 ①分数 <script type="text/javascript">    var a = 1   var b = 1   var c    for ...

  7. Alpha阶段-个人总结

    一.五个问题 1.第三章中提到了"质量"和"按时交付"的问题,我想问,世事难料,当两者不能兼得的时候,我是保证质量却无法按时交付,还是水两下保证按时交付呢? 2 ...

  8. 团队作业4——第一次项目冲刺(Alpha版本) 4.24

    团队作业4--第一次项目冲刺(Alpha版本) Day four: 会议照片 每日站立会议: 项目进展 今天是项目的Alpha敏捷冲刺的第三天,先大概整理下昨天已完成的任务以及今天计划完成的任务.今天 ...

  9. 201521123023《java程序设计》第三周学习总结

    1. 本周学习总结 2. 书面作业 1.代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; p ...

  10. 201521123036 《Java程序设计》第13周学习总结

    本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 书面作业 网络基础 1.1 比较ping www.baidu.com与ping cec.jmu.edu.cn,分 ...