結果
| 問題 |
No.1330 Multiply or Divide
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-01-23 02:54:31 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,064 bytes |
| コンパイル時間 | 1,360 ms |
| コンパイル使用メモリ | 139,972 KB |
| 最終ジャッジ日時 | 2025-01-18 07:11:59 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 45 TLE * 1 |
ソースコード
#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;
vector<pair<ll, ll>> b;
for (auto& e : a) {
cin >> e;
chmax(tm, e);
b.emplace_back(e, 1);
while (e % P == 0) e /= P, b.back().second += 1;
b.back().first = e;
}
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();
mp.erase(mp.begin());
for (auto [f, s] : b) {
if (f == 1) continue;
update(t * f, cnt + s);
}
}
}
cout << res << "\n";
}
}