You are given two integers x and y. You want to choose two strictly positive (greater than zero) integers a and b, and then apply the following operation to x exactly a times: replace x with b⋅x
You want to find two positive integers a and b such that x becomes equal to y after this process. If there are multiple possible pairs, you can choose any of them. If there is no such pair, report it.
For example:
- if x=3 and y=75, you may choose a=2 and b=5, so that x becomes equal to 3⋅5⋅5=75;
- if x = 100 and y=100, you may choose a=3 and b=1, so that x becomes equal to 100⋅1⋅1⋅1=100;
- if x=42 and y=13, there is no answer since you cannot decrease x with the given operations.
Input
The first line contains one integer tt (1 ≤t≤1e4) — the number of test cases.
Each test case consists of one line containing two integers x and y (1 ≤x,y≤100).
Output
If it is possible to choose a pair of positive integers a and b so that x becomes y after the aforementioned process, print these two integers. The integers you print should be not less than 1 and not greater than 10^9 (it can be shown that if the answer exists, there is a pair of integers a and b meeting these constraints). If there are multiple such pairs, print any of them.
If it is impossible to choose a pair of integers a and b so that x becomes y, print the integer 0 twice.
Sample 1
Inputcopy | Outputcopy |
---|---|
3 3 75 100 100 42 13 | 2 5 3 1 0 0 |
题意理解:有两个整数x和y,问是否存在x*Q(整数)=y?如果Q不存在,输出0 0;反之,针对Q,如果存在(m的n次方等于Q),输出n m(1<n<1e9),如果不存在,直接输出1 Q
思路:感觉这个题目其实主要是分类讨论吧。
1.x<y时,如果y%x!=0,即无论x怎么乘最终都无法得到y,此时应输出0 0(由题目及样例可知);如果y%x==0,当x==1时,就可以直接输出1 y/x(其实一开始没有考虑到这个,看了那个错误样例才知道) ;当x!=1时,先判断一下y/x是否是素数,如果是素数就直接输出 1 y/x(可以减少一下时间复杂度)(这里一开始也以为输出0 0,hh),如果不是的话,就再次进行判断是否有某个数x的i次方等于y/x,如果有,输出i x(注意这里的x找的是y/x的最小质因子)(这里采用最小质因子是为了方便,因为感觉如果采用合数,还需要考虑它的拆分啊什么的,感觉会很麻烦),反之,输出1 y/x(其实这里一开始我以为输出0 0后来看了错误样例才知道应该是这样)。
2.x==y时,输出i(可以是大于1小于1e9的任意数) x,这里为了方便起见,直接按样例输出 3 1了,hh
3.x>y时,x乘以任何整数都无法变成y,所以显然输出0 0
代码:
#include <iostream>
#include <cmath>
#include<algorithm>
using namespace std;
int sushu(int x) {
if (x < 2)
return 0;
for (int i = 2; i <= x / i; i++) {
if (x % i == 0)
return 0;
}
return 1;
}
int main() {
int t;
cin >> t;
while (t--) {
int x, y;
cin >> x >> y;
if (x < y ) {
if (y % x != 0)
cout << 0 << ' ' << 0 << endl;
else {
if (x == 1) {
cout << 1 << ' ' << y << endl;
continue;
}
int cnt = y / x;
if (sushu(cnt))
cout << 1 << ' ' << cnt << endl;
else {
int t;
for (int i = 2; i < 1e9; i++) {
if (cnt % i == 0) {
t = i;
break;
}
}
for (int i = 2; i < 1e9; i++) {
if (pow(t, i) == cnt) {
cout << i << ' ' << t << endl;
break;
} else if (pow(t, i) > cnt) {
cout << 1 << ' ' << cnt<< endl;
break;
}
}
}
}
} else if (x == y)
cout << 3 << ' ' << 1 << endl;
else if (x > y)
cout << 0 << ' ' << 0 << endl;
}
return 0;
}
写的不太好,如果出现问题,欢迎批评指正啊