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. 用最简单的方法判断JavaScript中this的指向

    目录 * 一个特例 * 开始判断 * 法则一:对象方法中的this指向对象本身(箭头函数形式的除外) * 法则二:多层嵌套函数中的this指向等同于包含该this的最近一个function的this ...

  2. hover用法实例

    //hover,鼠标移入移出的另一种用法 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" &qu ...

  3. 使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序

    使用ASP.NET Core MVC 和 Entity Framework Core 开发一个CRUD(增删改查)的应用程序 不定时更新翻译系列,此系列更新毫无时间规律,文笔菜翻译菜求各位看官老爷们轻 ...

  4. 我的前端故事----关于redux的一些思考

    背景 我一个前端,今年第一份工作就是接手一个 APP 的开发...一个线下 BD 人员用的推广 APP,为了让我这个一天原生开发都没有学过的人能快速开发上线,于是乎就选择了 react-native ...

  5. Express异步进化史

    1.导言 在 Javascript 的世界里,异步(由于JavaScript的单线程运行,所以JavaScript中的异步是可以阻塞的)无处不在. Express 是 node 环境中非常流行的Web ...

  6. 编译安装LAMP并实现wordpress

    author:JevonWei 版权声明:原创作品 软件环境 centos7.3 apr-1.5.2.tar.bz2 apr-util-1.5.4.tar.bz2 httpd-2.4.27.tar.b ...

  7. GUI(国际象棋棋盘)

    package com.niit.javagui; import java.awt.BorderLayout; import java.awt.Color; import java.awt.GridB ...

  8. 微信小程序wx.navigateTo层叠5次限制,特殊情况的建议

    小程序页面的实例使用栈的数据结构存储,栈内元素最多5个(换一种方式说,就是用户最多能点击5次返回),微信小程序能在栈中相对高层某个页面调用其他相对低层的页面实例的方法. 小程序三种页面跳转API 的区 ...

  9. 第02周-Java作业评价

    1. 本周作业简评与建议 作业存在的问题 格式上还是存在问题,但是比较第一次有很大的进步. 答题上,有的同学还是很敷衍,题目要求有分析过程,但是只写一个false,true,没有分析.也比较缺少自己的 ...

  10. 201521123004《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 答:主要以泛型为主 //简单的泛型类的定义,T为类型参数 public ...