結果
問題 | No.1330 Multiply or Divide |
ユーザー |
|
提出日時 | 2021-01-23 03:05:43 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 383 ms / 2,000 ms |
コード長 | 2,429 bytes |
コンパイル時間 | 2,372 ms |
コンパイル使用メモリ | 138,056 KB |
最終ジャッジ日時 | 2025-01-18 07:12:35 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 46 |
ソースコード
#include <iostream> #include <string> #include <sstream> #include <stack> #include <algorithm> #include <cmath> #include <queue> #include <bitset> #include <iomanip> #include <limits> #include <chrono> #include <random> #include <array> #include <unordered_map> #include <functional> #include <complex> #include <numeric> #include <cctype> #include <map> #include <set> #include <cstdlib> #include <bitset> #include <tuple> #include <assert.h> #include <deque> #include <utility> #include <fstream> using namespace std; typedef long long ll; using ull = unsigned long long; template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } //template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); }return a; } //mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); constexpr long long INF = 1LL << 60; constexpr int inf = 1000000007; constexpr long long mod = 1000000007LL; //constexpr long long mod = 998244353; int main() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); ll n, m, P; cin >> n >> m >> P; vector<ll> a(n); ll tm = 1; map<ll, ll> b; for (auto& e : a) { cin >> e; chmax(tm, e); ll cnt = 1; while (e % P == 0) e /= P, cnt += 1; if (e > 1) { if (b.count(e)) chmin(b[e], cnt); else b[e] = cnt; } } for (ll i = 1; i <= 62; i++) { ll mx = 0; for (auto [e, cnt] : b) { if (cnt == i) chmax(mx, e); } if (mx > 0) { for (auto itr = b.begin(); itr != b.end();) { if (itr->second == i and itr->first < mx) { itr = b.erase(itr); } else { itr++; } } } } if (tm > m) { cout << 1 << "\n"; return 0; } ll mx = *max_element(a.begin(), a.end()); if (mx == 1) { cout << -1 << "\n"; } else { map<ll, ll> mp; mp[1] = 0; ll res = INF; auto update = [&](ll x, ll y) { if (mp.count(x)) chmin(mp[x], y); else mp[x] = y; }; while (!mp.empty() and mp.begin()->first <= m) { if (mp.begin()->first * tm > m) { chmin(res, mp.begin()->second + 1); mp.erase(mp.begin()); } else { auto [t, cnt] = *mp.begin(); if (cnt >= res) continue; mp.erase(mp.begin()); for (auto [f, s] : b) { if (f == 1) continue; if(cnt + s < inf) update(t * f, cnt + s); } } } cout << res << "\n"; } }