結果

問題 No.1330 Multiply or Divide
ユーザー 👑 jupirojupiro
提出日時 2021-01-23 02:54:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,064 bytes
コンパイル時間 1,573 ms
コンパイル使用メモリ 144,776 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 13:03:46
合計ジャッジ時間 8,759 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
testcase_45 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#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";
	}
}
0