前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个简单的数学问题

一个简单的数学问题

作者头像
Fivecc
发布2022-11-21 15:49:24
2180
发布2022-11-21 15:49:24
举报
文章被收录于专栏:前端ACE

A Simple Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 729    Accepted Submission(s): 177 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

 题意:

代码语言:javascript
复制
给定两个正整数a和b,找到合适的X和Y来满足条件   (1)X + Y =a  
                                           (2)(X,Y)的最小公倍数= b

                 即

                                                  x+y=a

                                                 x*y/gcd(x,y)=b

                                            令gcd(x,y)=tem

                                                   x=i*c,y=j*c

                                                  i*c+j*c=a

                                                 c*i*j=b

                                                  tem*(i+j)=a

                                                   tem*i*j=b

因为i j互质 所以gcd(a,b)=c=gcd(x,y) 【最重要的条件】

所以一开始就能得到c值,剩下就是求根

//思路:解一个二元一次方程 求出方程根              //令A=a/tem;             //令B=b/tem;             //A=a/tem;             //B=b/tem;             //k1+k2=A;             //k1*k2=B;             //k1(A-k1)=B;             // k1^2-Ak1+B=0;             // A^A-4*B>0

代码语言:javascript
复制
#include<stdio.h>
#include<string.h>
#include<math.h>
#define M 100003
int gcd(int a,int b){if(b==0) return a;else return gcd(b,a%b);}
int main(){
	long int a,b,x,y,x2,y2,tem,k1,k2,A,B,q;
	while(scanf("%ld%ld",&a,&b)!=EOF){
		     tem=gcd(a,b);
		     A=a/tem;
		     B=b/tem;
		    q=A*A-4*B;
		 	if(q<0) printf("No Solution\n");
		 	else
		 	{ long int  t1=sqrt(q);
		 	   if(t1*t1!=q)printf("No Solution\n");
		 	   else{
		 	   	   x=(A-t1)*tem;
		 	   	   y=(A+t1)*tem;
		 	   	   printf("%ld %ld\n",x/2,y/2);
		 	   }
		 		
		 	}
		 }
	return 0;
} 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  •                                                   x+y=a
  •                                                  x*y/gcd(x,y)=b
  •                                             令gcd(x,y)=tem
  •                                                    x=i*c,y=j*c
  •                                                   i*c+j*c=a
  •                                                  c*i*j=b
  •                                                   tem*(i+j)=a
  •                                                    tem*i*j=b
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档