結果

問題 No.2954 Calculation of Exponentiation
ユーザー tnakao0123
提出日時 2024-11-11 17:45:47
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,343 bytes
コンパイル時間 694 ms
コンパイル使用メモリ 59,348 KB
最終ジャッジ日時 2025-02-25 03:55:36
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 28
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘pii readrat()’:
main.cpp:32:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   32 |   scanf("%s", s);
      |   ~~~~~^~~~~~~~~

ソースコード

diff #

/* -*- 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 >= 0
  if (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;
}
0