题干:
Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting.
The game consists of multiple rounds. Its rules are very simple: in each round, a natural number k is chosen. Then, the one who says (or barks) it faster than the other wins the round. After that, the winner's score is multiplied by k2, and the loser's score is multiplied by k. In the beginning of the game, both Slastyona and Pushok have scores equal to one.
Unfortunately, Slastyona had lost her notepad where the history of all n games was recorded. She managed to recall the final results for each games, though, but all of her memories of them are vague. Help Slastyona verify their correctness, or, to put it another way, for each given pair of scores determine whether it was possible for a game to finish with such result or not.
Input
In the first string, the number of games n (1 ≤ n ≤ 350000) is given.
Each game is represented by a pair of scores a, b (1 ≤ a, b ≤ 109) – the results of Slastyona and Pushok, correspondingly.
Output
For each pair of scores, answer "Yes" if it's possible for a game to finish with given score, and "No" otherwise.
You can output each letter in arbitrary case (upper or lower).
Example
Input
6
2 4
75 45
8 8
16 16
247 994
1000000000 1000000
Output
Yes
Yes
Yes
No
No
Yes
Note
First game might have been consisted of one round, in which the number 2 would have been chosen and Pushok would have won.
The second game needs exactly two rounds to finish with such result: in the first one, Slastyona would have said the number 5, and in the second one, Pushok would have barked the number 3.
题目大意:
有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数
解题思路:将两个人的分数相乘然后开立方即可
解题报告:
这题要注意,pow和sqrt返回的都是double型,如果强转一下,默认向下取整,所以这里需要四舍五入,用round函数。
ps一个题解:题解、、
题意
有n(1<=n<=350000)场游戏,对于每场游戏有若干个回合组成,两个人的初始分均为1,每个回合赢的人当前分数乘上k^2,输的人当前分数乘上k(每一回合的k都是不同的)。给你两个数a,b(1<=a,b<=10^9)问你这两个数是否为他们最终的分数?
思路:
我们先计算a*b,那么考虑a*b=(k_1^3)(k_2^3)…(k_i^3),即a*b为立方数的乘积组成,我们可以二分找出满足a*b=mid^3,我们判断mid是否等于k_1*k_2…*k_i。
当且仅当存在a*b=mid^3,a%mid=0,b%mid=0,那么a,b就为他们最终的分数,反之不是。
AC代码:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;int main()
{int t,a,b;cin>>t;while(t--) {scanf("%d%d",&a,&b);int tmp = round(pow(1.0*a*b,1.0/3));if(a*b == tmp*tmp*tmp && a%tmp==0 & b%tmp==0) puts("Yes");else puts("No");}return 0 ;
}
这个pow开三次方,不知道是怎么做的。
附一种 牛顿迭代法开立方根的方法:
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define double long doubleconst double eps = 1e-1;
double sqrt3(double x) {double r = x;double f = 1, k;do {f = r * r * r - x;k = 3 * r * r;r -= f / k;} while (f > eps);return r;
}int a, b;
long long P;
inline void init() {scanf("%d%d", &a, &b);P = a * 1LL * b;
}inline boolean solve() {long long x = sqrt3(P);if(x * x * x != P) return false;return !(a % x || b % x);
}int T;
int main() {scanf("%d", &T);while(T--) {init();puts(solve() ? ("Yes") : ("No"));}return 0;
}
二分求解:
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define LL long longint sqrt3(LL x) {int l = 1, r = 1e6;while(l <= r) {LL mid = (l + r) >> 1;if(mid * mid * mid <= x) l = mid + 1;else r = mid - 1;}return l - 1;
}int a, b;
long long P;
inline void init() {scanf("%d%d", &a, &b);P = a * 1LL * b;
}inline boolean solve() {long long x = sqrt3(P);if(x * x * x != P) return false;return !(a % x || b % x);
}int T;
int main() {scanf("%d", &T);while(T--) {init();puts(solve() ? ("Yes") : ("No"));}return 0;
}
Hash的方法:
#include <bits/stdc++.h>
using namespace std;
typedef bool boolean;
#define LL long longconst int limit = 1e6;
LL arr3[limit + 1];
inline void rinit() {for(int i = 1; i <= limit; i++)arr3[i] = i * 1LL * i * i;
}int a, b;
long long P;
inline void init() {scanf("%d%d", &a, &b);P = a * 1LL * b;
}inline boolean solve() {int x = lower_bound(arr3 + 1, arr3 + limit + 1, P) - arr3;if(arr3[x] != P) return false;return !(a % x || b % x);
}int T;
int main() {rinit();scanf("%d", &T);while(T--) {init();puts(solve() ? ("Yes") : ("No"));}return 0;
}