結果
問題 | No.2954 Calculation of Exponentiation |
ユーザー |
![]() |
提出日時 | 2024-11-11 17:45:47 |
言語 | C++17(gcc12) (gcc 12.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,343 bytes |
コンパイル時間 | 659 ms |
コンパイル使用メモリ | 59,520 KB |
実行使用メモリ | 5,248 KB |
最終ジャッジ日時 | 2024-11-11 17:45:49 |
合計ジャッジ時間 | 1,860 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
ソースコード
/* -*- coding: utf-8 -*-** 2954.cc: No.2954 Calculation of Exponentiation - yukicoder*/#include<cstdio>#include<cmath>#include<algorithm>#include<utility>using namespace std;/* typedef */using pii = pair<int,int>;/* subroutines */template <typename T>T gcd(T m, T n) { // m >= 0, n >= 0if (m < n) swap(m, n);while (n > 0) {T r = m % n;m = n;n = r;}return m;}pii readrat() {char s[32];scanf("%s", s);char *cpt = s;int sign = 1;if (*cpt == '-') sign = -1, cpt++;int p = 0, q = 0;while (*cpt >= '0' && *cpt <= '9') p = p * 10 + (*(cpt++) - '0');cpt++; // '.'while (*cpt >= '0' && *cpt <= '9') q = q * 10 + (*(cpt++) - '0');int n = p * 10000 + q, d = 10000;int g = gcd(n, d);n /= g, d /= g;return {n * sign, d};}/* main */int main() {auto [an, ad] = readrat();auto [bn, bd] = readrat();if (bn == 0) { puts("Yes"); return 0; }if (bn < 0) {swap(an, ad);bn = -bn;}//printf(" a=%d/%d, b=%d/%d\n", an, ad, bn, bd);if (ad == 1) {if (bd == 1) { puts("Yes"); return 0; }int x0 = 1, x1 = an + 1;while (x0 + 1 < x1) {int x = (x0 + x1) / 2;if (pow(x, bd) > an) x1 = x;else x0 = x;}if (pow(x0, bd) == an) { puts("Yes"); return 0; }}puts("No");return 0;}