結果

問題 No.1330 Multiply or Divide
ユーザー SSRSSSRS
提出日時 2021-01-08 22:32:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,271 bytes
コンパイル時間 2,343 ms
コンパイル使用メモリ 184,328 KB
実行使用メモリ 5,236 KB
最終ジャッジ日時 2023-08-10 13:09:24
合計ジャッジ時間 5,838 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 65 ms
4,968 KB
testcase_02 WA -
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 31 ms
4,852 KB
testcase_07 AC 97 ms
4,800 KB
testcase_08 AC 93 ms
5,236 KB
testcase_09 AC 95 ms
4,816 KB
testcase_10 AC 93 ms
5,156 KB
testcase_11 AC 93 ms
5,120 KB
testcase_12 AC 94 ms
5,236 KB
testcase_13 AC 2 ms
4,384 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 91 ms
4,948 KB
testcase_19 AC 91 ms
4,984 KB
testcase_20 AC 92 ms
4,956 KB
testcase_21 AC 91 ms
4,800 KB
testcase_22 AC 91 ms
4,980 KB
testcase_23 AC 84 ms
4,804 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 2 ms
4,384 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,384 KB
testcase_32 AC 2 ms
4,384 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 69 ms
4,604 KB
testcase_35 AC 20 ms
4,380 KB
testcase_36 AC 61 ms
4,564 KB
testcase_37 AC 54 ms
4,384 KB
testcase_38 AC 34 ms
4,380 KB
testcase_39 AC 24 ms
4,380 KB
testcase_40 AC 28 ms
4,384 KB
testcase_41 AC 15 ms
4,380 KB
testcase_42 AC 51 ms
4,380 KB
testcase_43 AC 57 ms
4,380 KB
testcase_44 AC 34 ms
4,796 KB
testcase_45 AC 33 ms
4,976 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: 関数 ‘int main()’ 内:
main.cpp:63:13: 警告: ‘ans’ may be used uninitialized [-Wmaybe-uninitialized]
   63 |     cout << ans << endl;
      |             ^~~
main.cpp:41:9: 備考: ‘ans’ はここで定義されています
   41 |     int ans;
      |         ^~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, M, P;
  cin >> N >> M >> P;
  vector<int> A(N);
  for(int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<vector<int>> p(31);
  bool ok = false;
  for (int i = 0; i < N; i++){
    int t = 0;
    int tmp = A[i];
    while (tmp % P == 0){
      tmp /= P;
      t++;
    }
    if (tmp > 1){
      ok = true;
    }
    p[t].push_back(A[i]);
  }
  if (!ok){
    cout << -1 << endl;
  } else {
    for (int i = 0; i < 31; i++){
      sort(p[i].begin(), p[i].end(), greater<int>());
    }
    vector<int> B;
    for (int i = 0; i < 31; i++){
      if (!p[i].empty()){
        B.push_back(p[i][0]);
      }
    }
    int cnt = B.size();
    map<long long, int> mp;
    queue<long long> Q;
    Q.push(1);
    mp[1] = 0;
    int ans;
    while (!Q.empty()){
      long long v = Q.front();
      Q.pop();
      if (v > M){
        ans = mp[v];
        break;
      }
      if (v % P == 0){
        if (!mp.count(v / P)){
          mp[v / P] = mp[v] + 1;
          Q.push(v / P);
        }
      } else {
        for (int i = 0; i < cnt; i++){
          if (!mp.count(v * B[i])){
            mp[v * B[i]] = mp[v] + 1;
            Q.push(v * B[i]);
          }
        }
      }
    }
    cout << ans << endl;
  }
}
0