結果

問題 No.1330 Multiply or Divide
ユーザー SSRSSSRS
提出日時 2021-01-08 22:30:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,302 bytes
コンパイル時間 1,748 ms
コンパイル使用メモリ 188,956 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 03:56:03
合計ジャッジ時間 5,180 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 33 ms
6,940 KB
testcase_07 AC 96 ms
6,940 KB
testcase_08 AC 96 ms
6,940 KB
testcase_09 AC 99 ms
6,940 KB
testcase_10 AC 96 ms
6,944 KB
testcase_11 AC 94 ms
6,944 KB
testcase_12 AC 93 ms
6,944 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 2 ms
6,940 KB
testcase_17 WA -
testcase_18 AC 93 ms
6,944 KB
testcase_19 AC 92 ms
6,944 KB
testcase_20 AC 93 ms
6,940 KB
testcase_21 AC 92 ms
6,944 KB
testcase_22 AC 93 ms
6,940 KB
testcase_23 AC 87 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
testcase_30 WA -
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 2 ms
6,944 KB
testcase_34 WA -
testcase_35 AC 20 ms
6,944 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 36 ms
6,940 KB
testcase_39 AC 23 ms
6,944 KB
testcase_40 AC 27 ms
6,940 KB
testcase_41 AC 16 ms
6,944 KB
testcase_42 WA -
testcase_43 AC 56 ms
6,940 KB
testcase_44 AC 35 ms
6,940 KB
testcase_45 AC 34 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function 'int main()':
main.cpp:64:13: warning: 'ans' may be used uninitialized [-Wmaybe-uninitialized]
   64 |     cout << ans << endl;
      |             ^~~
main.cpp:42:9: note: 'ans' was declared here
   42 |     int ans;
      |         ^~~

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
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(A[i]);
      }
    }
    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