C. The Meaningless Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSlastyona 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.
InputIn 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.
OutputFor 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).
Exampleinput6
2 4
75 45
8 8
16 16
247 994
1000000000 1000000outputYes
Yes
Yes
No
No
YesNoteFirst 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.
解题思路:如果a*b==i^3 && a*a%b==0 && b*b%a==0
那么就输出Yes。否则输出No。
ps:这题其实直接用pow就可以解出了,注意精度,但是在处理高精度的方面我比较方。。。所以用二分查找。
也tle几次,发现竟然是习惯性的多查找几个数就超时了。改了之后AC了,但是时间是998ms。。。看了别人的代码都差不多,不造为什么,算了,下次还是用高精度吧。
AC代码:
1 #include<iostream> 2 #include<stdio.h> 3 using namespace std; 4 int main() 5 { 6 int T; 7 cin>>T; 8 while(T--){ 9 long long a,b; 10 scanf("%lld%lld",&a,&b); 11 long long m=a*b; 12 long long l=1,r=1000000,mid=(l+r)/2; 13 bool flag=false; 14 if(a*a%b==0&&b*b%a==0){ 15 while(l<=r){ 16 mid=(l+r)/2; 17 if((mid*mid*mid)==m){ 18 flag=true; 19 break; 20 } 21 else if((mid*mid*mid)>m){ 22 r=mid-1; 23 }else{ 24 l=mid+1; 25 } 26 } 27 } 28 if(flag) cout<<"Yes"<<endl; 29 else cout<<"No"<<endl; 30 } 31 return 0; 32 }