Cho hai số dương x và y, hãy kiểm tra xem y có phải là sức mạnh của x hay không.examples: & nbsp; Examples : Đầu vào: & nbsp; x = 10, y = 1Output: trorex^0 = 1 x = 10, y = 1 Output: True x^0 = 1 Đầu vào: & nbsp; x = 10, y = 1000Output: truex^3 = 1 x = 10, y = 1000 Output: True x^3 = 1 Đầu vào: & nbsp; x = 10, y = 1001Output: false x = 10, y = 1001 Output: False
Một giải pháp đơn giản là liên tục tính toán sức mạnh của x. Nếu một sức mạnh trở nên bằng y, thì y là một sức mạnh, thì không phải. & Nbsp; & nbsp; C++#include <bits/stdc++.h>
using namespace std;
bool isPower( int x, long int #include <bits/stdc++.h> 1
#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 4 #include <bits/stdc++.h> 5
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 7 #include <bits/stdc++.h> 8
#include <bits/stdc++.h> 3long int using 2 using 3
#include <bits/stdc++.h> 3using 5 using 6using 2 using 8
#include <bits/stdc++.h> 6using 2 namespace 1
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 7 using 6using 2 namespace 6
namespace 7
int namespace 9
#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 3std; 2
#include <bits/stdc++.h> 3std; 4
#include <bits/stdc++.h> 3std; 6
#include <bits/stdc++.h> 3std; 8
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 7 bool 1
namespace 7
Javabool 3 bool 4 bool 5
#include <bits/stdc++.h> 3bool 3 bool 8 bool 9 isPower( 0
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 2
Các Các Các Các #include <bits/stdc++.h> 3namespace 7
#include <bits/stdc++.h> 3bool 3 bool 8 #include <bits/stdc++.h> 08 isPower( int x, int #include <bits/stdc++.h> 1
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 4 #include <bits/stdc++.h> 18isPower( 7#include <bits/stdc++.h> 20
#include <bits/stdc++.h> 21#include <bits/stdc++.h> 7 #include <bits/stdc++.h> 23isPower( 7int 2
#include <bits/stdc++.h> 6int #include <bits/stdc++.h> 28isPower( 7#include <bits/stdc++.h> 30
#include <bits/stdc++.h> 6using 5 #include <bits/stdc++.h> 33
#include <bits/stdc++.h> 21#include <bits/stdc++.h> 35
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 7 #include <bits/stdc++.h> 38
#include <bits/stdc++.h> 3namespace 7
namespace 7
Python3#include <bits/stdc++.h> 42 #include <bits/stdc++.h> 43
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 4 #include <bits/stdc++.h> 46#include <bits/stdc++.h> 47#include <bits/stdc++.h> 47 isPower( 7#include <bits/stdc++.h> 50
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 7 #include <bits/stdc++.h> 53#include <bits/stdc++.h> 47#include <bits/stdc++.h> 47 isPower( 7#include <bits/stdc++.h> 20
#include <bits/stdc++.h> 3using 2 #include <bits/stdc++.h> 47 isPower( 7
#include <bits/stdc++.h> 3using 5 using 6using 2 #include <bits/stdc++.h> 66
#include <bits/stdc++.h> 6using 2 #include <bits/stdc++.h> 47 using 2 #include <bits/stdc++.h> 71 #include <bits/stdc++.h> 72
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 7 using 6using 2 #include <bits/stdc++.h> 47#include <bits/stdc++.h> 47 #include <bits/stdc++.h> 1
#include <bits/stdc++.h> 4#include <bits/stdc++.h> 81isPower( 5isPower( 6isPower( 7#include <bits/stdc++.h> 85
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 87using 6isPower( 7#include <bits/stdc++.h> 20
#include <bits/stdc++.h> 91#include <bits/stdc++.h> 92
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 87using 6int 1#include <bits/stdc++.h> 20
#include <bits/stdc++.h> 4#include <bits/stdc++.h> 81isPower( 7isPower( 6int 7#include <bits/stdc++.h> 85
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 87using 6isPower( 7#include <bits/stdc++.h> 20
#include <bits/stdc++.h> 91#include <bits/stdc++.h> 92
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 87using 6int 1#include <bits/stdc++.h> 20
#include <bits/stdc++.h> 4#include <bits/stdc++.h> 81x, 5isPower( 6x, 7#include <bits/stdc++.h> 85
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 87using 6isPower( 7#include <bits/stdc++.h> 20
#include <bits/stdc++.h> 91#include <bits/stdc++.h> 92
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 87using 6int 1#include <bits/stdc++.h> 20
#include <bits/stdc++.h> 4#include <bits/stdc++.h> 81x, 5isPower( 6long 7#include <bits/stdc++.h> 85
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 87using 6isPower( 7#include <bits/stdc++.h> 20
#include <bits/stdc++.h> 91#include <bits/stdc++.h> 92
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 87using 6int 1#include <bits/stdc++.h> 20
C#using using 53
bool 4 using 55
#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 3bool 3 bool 8 bool using 61int x, int #include <bits/stdc++.h> 1
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 4 #include <bits/stdc++.h> 5
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 7 #include <bits/stdc++.h> 8
#include <bits/stdc++.h> 3long int using 2 using 3
#include <bits/stdc++.h> 6using 5 #include <bits/stdc++.h> 33
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 35
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 7 #include <bits/stdc++.h> 38
#include <bits/stdc++.h> 3namespace 7
#include <bits/stdc++.h> 42 #include <bits/stdc++.h> 43
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 6using 95
#include <bits/stdc++.h> 6using 97
#include <bits/stdc++.h> 6using 99
#include <bits/stdc++.h> 6namespace 01
#include <bits/stdc++.h> 3namespace 7
namespace 7
#include <bits/stdc++.h>3#include <bits/stdc++.h>4 #include <bits/stdc++.h>46#include <bits/stdc++.h>47#include <bits/stdc++.h>47 isPower(7#include <bits/stdc++.h>50namespace 05
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 7 #include <bits/stdc++.h> 53#include <bits/stdc++.h> 47#include <bits/stdc++.h> 47 isPower( 7#include <bits/stdc++.h> 20
#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 3using 2 #include <bits/stdc++.h> 47 isPower( 7
#include <bits/stdc++.h> 3using 5 using 6using 2 #include <bits/stdc++.h> 66
#include <bits/stdc++.h> 6using 2 #include <bits/stdc++.h> 47 using 2 #include <bits/stdc++.h> 71 #include <bits/stdc++.h> 72
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 7 using 6using 2 #include <bits/stdc++.h> 47#include <bits/stdc++.h> 47 #include <bits/stdc++.h> 1
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 87using 6isPower( 7#include <bits/stdc++.h> 20
using using 53
namespace 7
bool 4 using 55
#include <bits/stdc++.h> 3bool 3 bool 8 bool using 61int x, int #include <bits/stdc++.h> 1
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 4 #include <bits/stdc++.h> 5
#include <bits/stdc++.h> 6int using 76
namespace 62
#include <bits/stdc++.h>3bool3 bool8 bool9 using91namespace 63
PHP #include <bits/stdc++.h> 3#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 4 #include <bits/stdc++.h> 5
#include <bits/stdc++.h> 6int using 76
#include <bits/stdc++.h> 6namespace 76
#include <bits/stdc++.h> 6using 5 #include <bits/stdc++.h> 33
#include <bits/stdc++.h> 21#include <bits/stdc++.h> 35
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 7 #include <bits/stdc++.h> 38
#include <bits/stdc++.h> 3namespace 7
#include <bits/stdc++.h> 6namespace 88namespace 89int 2
namespace 91namespace 92namespace 89int 2
#include <bits/stdc++.h> 6namespace 96namespace 89int 2
#include <bits/stdc++.h> 6std; 00namespace 89int 2
std; 03
Độ phức tạp về thời gian: O (logxy) Không gian phụ trợ: O (1) O(Logxy) Auxiliary space: O(1) Tối ưu hóa: & nbsp; chúng ta có thể tối ưu hóa giải pháp ở trên để làm việc trong O (nhật ký log y). Ý tưởng là làm bình phương sức mạnh thay vì nhân nó với x, tức là so sánh y với x^2, x^4, x^8, vv. Nếu x trở nên bằng y, hãy trả về đúng. Nếu x trở thành nhiều hơn y, thì chúng ta sẽ tìm kiếm nhị phân cho sức mạnh của x giữa công suất trước và công suất hiện tại, tức là, giữa x^i và x^(i/2). Theo dõi là bước chi tiết. & Nbsp; We
can optimize above solution to work in O(Log Log y). The idea is to do squaring of power instead of multiplying it with x, i.e., compare y with x^2, x^4, x^8, …etc. If x becomes equal to y, return true. If x becomes more than y, then we do binary search for power of x between previous power and current power, i.e., between x^i and x^(i/2). Following are detailed step. 1) Initialize pow = x, i = 1
2) while (pow < y)
{
pow = pow*pow
i *= 2
}
3) If pow == y
return true;
4) Else construct an array of powers
from x^i to x^(i/2)
5) Binary Search for y in array constructed
in step 4. If not found, return false.
Else return true. Giải pháp thay thế: & nbsp; Ý tưởng là lấy nhật ký của y trong cơ sở x. Nếu nó hóa ra là một số nguyên, chúng ta sẽ trả về đúng. Khác sai. & Nbsp; & nbsp; The idea is to take log of y in base x. If it turns out to
be an integer, we return true. Else false. C++std; 04
std; 05
using namespace std;
bool isPower( int x, int #include <bits/stdc++.h> 1
#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 3int std; 18std; 19std; 20____419std; 22
#include <bits/stdc++.h> 3std; 24 std; 25std; 19std; 20std; 19std; 29
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 7 std; 32
namespace 7
int namespace 9
#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 3std; 38
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 7 bool 1
namespace 7
Javabool 4 using 55
#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 3bool 8 #include <bits/stdc++.h> 08 isPower( int std; 51
std; 52int #include <bits/stdc++.h> 1
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 2
‘ std; 62using 6int std; 65
#include <bits/stdc++.h> 6std; 24 std; 68
std; 69std; 70
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 7 std; 32
#include <bits/stdc++.h> 3namespace 7
#include <bits/stdc++.h> 3bool 3 bool 8 bool 9 std; 80
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 4#include <bits/stdc++.h> 81std; 86isPower( 6std; 88__
#include <bits/stdc++.h> 21std; 91std; 92int 2
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 91
#include <bits/stdc++.h> 21std; 91std; 98int 2
#include <bits/stdc++.h> 3namespace 7
namespace 7
Python3bool 03 bool 04
#include <bits/stdc++.h> 42 bool 06
#include <bits/stdc++.h> 3bool 08#include <bits/stdc++.h> 47 bool 10bool 11bool 11 bool 13
#include <bits/stdc++.h> 3bool 15#include <bits/stdc++.h> 47 bool 10bool 11 bool 13
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 7 isPower( 7 #include <bits/stdc++.h> 4bool 24__
#include <bits/stdc++.h> 4 bool 32#include <bits/stdc++.h> 47#include <bits/stdc++.h> 47bool 35#include <bits/stdc++.h> 92
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 87#include <bits/stdc++.h> 81std; 86isPower( 6std; 88bool 43
C#using using 53
bool 4 using 55
#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 3bool 8 #include <bits/stdc++.h> 08 isPower( int std; 51
#include <bits/stdc++.h> 2
std; 52int #include <bits/stdc++.h> 1
bool 62using 6int bool 65
‘ bool 69bool 70
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 7 std; 32
namespace 7
int namespace 9
#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 4bool 81
#include <bits/stdc++.h> 6bool 83std; 92int 2
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 91
#include <bits/stdc++.h> 6bool 83std; 98int 2
namespace 7
namespace 7
#include <bits/stdc++.h>3#include <bits/stdc++.h>7 bool1namespace 05
Java #include <bits/stdc++.h> 2
bool 4 using 55
#include <bits/stdc++.h> 3bool 8 #include <bits/stdc++.h> 08 isPower( int std; 51
std; 52int #include <bits/stdc++.h> 1
namespace 7
‘ namespace 62
#include <bits/stdc++.h>6std;24 std;68namespace 63
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 7 std; 32
#include <bits/stdc++.h> 2
#include <bits/stdc++.h> 3bool 3 bool 8 bool 9 std; 80
bool 62isPower( 35
#include <bits/stdc++.h> 6#include <bits/stdc++.h> 4#include <bits/stdc++.h> 81std; 86isPower( 6std; 88__
bool 69std; 70
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 7 std; 32
namespace 7
#include <bits/stdc++.h> 4bool 81
#include <bits/stdc++.h> 3isPower( 48std; 92int 2
#include <bits/stdc++.h> 91
#include <bits/stdc++.h> 3isPower( 48std; 98int 2
std; 03
int namespace 9: O(1) Auxiliary space: O(1)
#include <bits/stdc++.h> 3#include <bits/stdc++.h> 7 bool 1Gyayak Jain for suggesting this solution. This article is contributed by Manish Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Làm thế nào để bạn kiểm tra xem một số có phải là sức mạnh của một số python khác không?
Để kiểm tra xem một số nguyên đã cho là có sức mạnh của số nguyên đã cho không, hãy kiểm tra xem x> 0 && y % x == 0: y là sức mạnh lớn nhất của y có thể phù hợp với kiểu dữ liệu số nguyên.check if x > 0 && Y % x == 0 : Y is the largest power of y that can fit into an integer datatype.
Làm thế nào để bạn kiểm tra xem một số có phải là sức mạnh của một số khác không?
Sau đây là bước chi tiết.1) Khởi tạo pow = x, i = 1 2) trong khi (pow
Làm thế nào để bạn kiểm tra xem một số có phải là sức mạnh của 3 trong Python không?
Khoa học dữ liệu thực tế sử dụng Python.. Chúng tôi sẽ sử dụng logarit để giải quyết điều này .. Nếu [log10 (n) / log10 (3)] mod 1 == 0, thì nó sẽ là sức mạnh của ba, nếu không thì không ..
Làm thế nào để bạn đánh giá một sức mạnh trong Python?
Hàm python pow () Hàm pow () trả về giá trị của x cho công suất của y (xy).Nếu có tham số thứ ba, nó sẽ trả lại x cho sức mạnh của y, mô đun z.The pow() function returns the value of x to the power of y (xy). If a third parameter is present, it returns x to the power of y, modulus z. |