結果

問題 No.1330 Multiply or Divide
ユーザー hitonanode
提出日時 2021-01-08 22:21:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 90 ms / 2,000 ms
コード長 1,202 bytes
コンパイル時間 2,146 ms
コンパイル使用メモリ 203,224 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-06-20 01:21:57
合計ジャッジ時間 3,987 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 51
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using plint = pair<lint, lint>;
#define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i<i##_end_;i++)
#define REP(i, n) FOR(i,0,n)
template <typename T> bool chmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; }


void answer(lint x) {
    cout << x << '\n';
    exit(0);
}

int main()
{
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N, M, P;
    cin >> N >> M >> P;
    lint maxi = 0;

    vector<plint> te2w;

    vector<lint> te2amax(32, 0);
    while (N--) {
        lint a;
        cin >> a;
        chmax(maxi, a);
        int te = 1;
        while (a % P == 0) a /= P, te++;
        chmax(te2amax[te], a);
    }
    FOR(t, 1, te2amax.size()) if (te2amax[t] > 1) te2w.emplace_back(t, te2amax[t]);

    const lint tgt = (M + 1 + maxi - 1) / maxi;

    priority_queue<plint, vector<plint>, greater<plint>> pq;
    pq.emplace(1, -1);
    while (pq.size()) {
        auto [D, val] = pq.top();
        pq.pop();
        if (abs(val) >= tgt) {
            answer(D);
        }
        for (auto [dd, a] : te2w) {
            pq.emplace(D + dd, val * a);
        }
    }
    puts("-1");
}
0