• hdu5974 A Simple Math Problem(数学)


    题目链接
    大意:给你两个数X,YX,Y,让你找两个数a,ba,b,满足a+b=X,lcm(a,b)=Ya+b=X,lcm(a,b)=Y.
    思路:枚举gcd(a,b)gcd(a,b),假设gcd(a,b)=ka=xak,b=xbk,gcd(a,b)=k,那么a=x_a*k,b=x_b*k,化简上面给的两个式子即可得到
    xa+xb=Xk,xaxb=Ykx_a+x_b=frac{X}{k},x_a*x_b=frac{Y}{k},显然这是一个方程组,将xb=Xkxax_b=frac{X}{k}-x_a代入第二个式子即可得到一个一元二次方程,解这个方程即可。

    #include<bits/stdc++.h>
    
    #define LL long long
    #define fi first
    #define se second
    #define mp make_pair
    #define pb push_back
    
    using namespace std;
    
    LL gcd(LL a,LL b){return b?gcd(b,a%b):a;}
    LL lcm(LL a,LL b){return a/gcd(a,b)*b;}
    LL powmod(LL a,LL b,LL MOD){LL ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
    const int N = 2e5 +11;
    int a,b;
    int main(){
    	ios::sync_with_stdio(false);
    	while(cin>>a>>b){
    		int q=sqrt(a);
    		int sta=0,anL,anR;
    		for(int i=1;i<=q;i++){
    			if(a%i)continue;
    			int A=a/i;
    			int B=b/i;
    			if(A*A<4*B)continue;
    			int T=floor(sqrt(A*A-4*B));
    			if(T*T!=A*A-4*B)continue;
    			else {
    				if((A-T)%2||(A+T)%2)continue;
    				int L=(A-T)/2*i;
    				int R=(A+T)/2*i;
    				if(lcm(L,R)!=b)continue;
    				anL=(A-T)/2*i;
    				anR=(A+T)/2*i;
    				sta=1;
    				break;
    			}
    		}
    		for(int i=1;i<=q&&!sta;i++){
    			if(a%i)continue;
    			int A=a/(a/i);
    			int B=b/(a/i);
    			if(A*A<4*B)continue;
    			int T=floor(sqrt(A*A-4*B));
    			if(T*T!=A*A-4*B){continue;}
    			else {
    				if((A-T)%2||(A+T)%2){continue;}
    				int L=(A-T)/2*(a/i); 
    				int R=(A+T)/2*(a/i);
    				if(lcm(L,R)!=b){continue;}
    				anL=(A-T)/2*(a/i);
    				anR=(A+T)/2*(a/i);
    				sta=1;
    				break;
    			}
    		}
    		if(!sta)cout<<"No Solution
    ";
    		else cout<<anL<<' '<<anR<<endl;
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    python学习笔记二--列表
    python学习笔记一--字符串
    写点什么呢
    nagios&pnp4nagios--yum 安装
    敏捷开发的思路
    Foreman--管理PuppetClient
    url编码解码的问题(urlencode/quote)
    json数据的处理和转化(loads/load/dump/dumps)
    http和https的区别
    python中requests的用法总结
  • 原文地址:https://www.cnblogs.com/pubgoso/p/10759745.html
Copyright © 2020-2023  润新知