0
点赞
收藏
分享

微信扫一扫

【ICPC第46届上海站 D题 Strange_Fractions】数学

独孤凌雪 2022-03-11 阅读 22
算法c++

题目链接

题意:

给你一个p,q,问是否存在一组a,b均为整数,且a/b + b/a = p/q

分析:

看下面的图片吧
在这里插入图片描述
下面请看代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<cstdlib>
#include<climits>
#include<unordered_map>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef unsigned long long ull;
const int mod = 1e9+7;
const int N = 100010;
 
int main(){
	int T;
	cin>>T;
	while(T--){
		LL p,q;
		cin>>p>>q;
		LL t = __gcd(p,q);p /= t;q /= t;
		bool flag = false;
		for(LL i=1;i*i<=p;i++){
			int b = sqrt(p-i*i); 
			if((i*i + b*b) == p && i*b == q){
				flag = true;
				cout<<i<<" "<<b<<endl;
				break;
			}
		}
		if(!flag) cout<<"0 0"<<endl;
	}
	return 0;
}


举报

相关推荐

0 条评论