【CodeForces - 833A】The Meaningless Game(思维题,数学,可用牛顿迭代法,知识点总结)
題干:
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 1000000Output
Yes Yes Yes No No YesNote
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; }?
總結
以上是生活随笔為你收集整理的【CodeForces - 833A】The Meaningless Game(思维题,数学,可用牛顿迭代法,知识点总结)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: s.exe - s是什么进程 有什么用
- 下一篇: C 的Pair用法分类整理(精)