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. 一位资深程序员大牛给予Java提升技术的学习路线建议

    15套java架构师.集群.高可用.高可扩 展.高性能.高并发.性能优化.Spring boot.Redis.ActiveMQ.Nginx.Mycat.Netty.Jvm大型分布 式项目实战视频教程 ...

  2. Redis介绍和环境安装

    -------------------Redis环境安装------------------- 1.安装     1.卸载软件         sudo apt-get remove redis-se ...

  3. 散列表(拉链法与线性探测法)Java实现

    package practice; import java.security.Principal; import java.util.Scanner; import edu.princeton.cs. ...

  4. 一、Solr的相关概念了解

    1.1. 什么是Solr Solr 是Apache下的一个顶级开源项目,采用Java开发,它是基于Lucene的全文搜索服务器.Solr提供了比Lucene更为丰富的查询语言,同时实现了可配置.可扩展 ...

  5. Net分布式系统之七:日志采集系统(1)

    日志对大型应用系统或者平台尤其重要,系统日志采集.分析是系统运维.维护及用户分析的基础. 一.系统日志分类 一般系统日志可分为三大类: 1.用户行为日志:通过采集系统用户使用系统过程中,一系列的操作日 ...

  6. Java中ArrayList remove会遇到的坑

    前言 平时最常用的莫过于ArrayList和HashMap了,面试的时候也是问答的常客.先不去管容量.负载因子什么的,就是简单的使用也会遇到坑. Remove 元素 经常遇到的一个场景是:遍历list ...

  7. 发布一个Python小程序:ManHourCalendar

    程序诞生的那些事儿 先聊聊背景资料档案.. 大约两年前,我只身前往岛国赚点外快.在那边的派遣制度工作中,存在一个大约叫每月的标准工作时间的概念,按照自家公司跟派遣目标公司(业界称为现场)的合同,规定了 ...

  8. lua代码的加载

    lua代码的加载 Openresty是什么 OpenResty是一个基于 Nginx 与 Lua 的高性能 Web 平台,通过把lua嵌入到Nginx中,使得我们可以用轻巧的lua语言进行nginx的 ...

  9. java记事本1.2版

    功能:实现了新建,打开,保存,退出,复制,剪切,粘贴等功能 效果图:

  10. Java学习5——标识符和关键字

    标识符: 1.Java对各种变量.方法和类等要素命名时使用的字符串序列称为标识符.凡是自己可以起名字的地方都叫标识符,都要遵守标识符的规则. 2.Java标识符命名规则: 标识符由字母.下划线&quo ...