列表

详情


NC231122. Strange_Fractions

描述

Given a positive fraction , you should find two positive integers a,b that . If no such integers, report it.

输入描述

The first line contains one integer , denoting the number of test cases.

For each test case:

Input one line containing two integers , denoting the given fraction.

输出描述

For each test case:

If solution exists, output one line containing two integers , or print two zeros in one line if no solution.

示例1

输入:

2
5 2
5 1

输出:

1 2
0 0

说明:

For the first case, \frac{5}{2} = \frac{1}{2} + \frac{2}{1} holds. So one possible solution is a=1,b=2.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 63ms, 内存消耗: 1784K, 提交时间: 2022-04-21 14:44:47

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

int main(){
	int tt;
	cin>>tt;
	while(tt--){
		int p,q;
		scanf("%d%d",&p,&q);
		ll d2=ll(p)*p-4*q*q;
		int d=0;
		d=sqrtl(d2);
		if(ll(d)*d!=d2)puts("0 0");
		else printf("%d %d\n",p+d,2*q);
	}
}

上一题