Slastyona and her loyal dog Pushok are playing a meaningless game that is indeed very interesting.ios
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.c++
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.less
In the first string, the number of games n (1 ≤ n ≤ 350000) is given.ide
Each game is represented by a pair of scores a, b (1 ≤ a, b ≤ 109) – the results of Slastyona and Pushok, correspondingly.函數
For each pair of scores, answer "Yes" if it's possible for a game to finish with given score, and "No" otherwise.spa
You can output each letter in arbitrary case (upper or lower).rest
6
2 4
75 45
8 8
16 16
247 994
1000000000 1000000
Yes
Yes
Yes
No
No
Yes
First game might have been consisted of one round, in which the number 2 would have been chosen and Pushok would have won.code
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.blog
題目大意:給你兩個初值爲1的數,若是你讓其中一個數乘以x,則另外一個數要乘以x的平方,問這樣的操做若干次可否獲得a和b?ip
解題思路:剛開始就只是想到了使用搜索的方法進行暴力求解,可是看了一下數據發現這樣是絕對會超時的就果斷放棄了。沒想法看了別人的博客發現,a*b必定是一個數的三次方而且a和b取餘這個數都是爲0的,只要找到這個數就能進行判斷了。
有兩種作法:
1.二分:
1 #include <iostream> 2 #include <bits/stdc++.h> 3 #define LL long long 4 using namespace std; 5 6 int main() 7 { 8 LL a,b; 9 int t; 10 while(~scanf("%d",&t)) 11 { 12 while(t--) 13 { 14 scanf("%I64d%I64d",&a,&b); 15 LL l=1,r=1000005,mid; 16 int flag=0; 17 while(l<=r) 18 { 19 mid=(l+r)/2; 20 //printf("%lld\n",mid); 21 if(mid*mid*mid==a*b&&a%mid==0&&b%mid==0) 22 { 23 printf("Yes\n"); 24 flag=1; 25 break; 26 } 27 else if(mid*mid*mid<a*b) 28 { 29 l=mid+1; 30 } 31 else 32 { 33 r=mid-1; 34 } 35 } 36 if(!flag) 37 { 38 printf("No\n"); 39 } 40 } 41 } 42 return 0; 43 }
2.pow()+round()函數
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 #include <bits/stdc++.h> 6 using namespace std; 7 8 int main() 9 { 10 double a,b; 11 int t; 12 while(~scanf("%d",&t)) 13 { 14 while(t--) 15 { 16 scanf("%lf%lf",&a,&b); 17 long long x,y,z; 18 double c=round(pow(a*b,1.0/3)); 19 x=a,y=b,z=c; 20 if(x%z==0&&y%z==0&&z*z*z==x*y) 21 printf("Yes\n"); 22 else 23 printf("No\n"); 24 } 25 } 26 return 0; 27 }
知識點:
ceil(x)返回不小於x的最小整數值(而後轉換爲double型)。 floor(x)返回不大於x的最大整數值。 round(x)返回x的四捨五入整數值。