結果
| 問題 |
No.854 公平なりんご分配
|
| コンテスト | |
| ユーザー |
ngtkana
|
| 提出日時 | 2019-07-26 22:05:33 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,765 bytes |
| コンパイル時間 | 2,568 ms |
| コンパイル使用メモリ | 207,332 KB |
| 最終ジャッジ日時 | 2025-01-07 07:58:21 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 WA * 1 |
| other | WA * 31 TLE * 4 MLE * 57 |
ソースコード
#include <bits/stdc++.h>
#define LOCAL
using std::to_string;
auto to_string(std::string s) -> std::string {
return '"' + s + '"';
}
auto to_string(char c) -> std::string {
return "'" + std::string{c} + "'";
}
auto to_string(const char* s) -> std::string {
return to_string((std::string) s);
}
auto to_string(bool b) -> std::string {
return (b ? "true" : "false");
}
template <typename T, typename U>
auto to_string(std::pair<T, U> p) -> std::string {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <size_t N>
auto to_string(std::bitset<N> bs) -> std::string {
std::string res{};
for (size_t i = 0; i < N; i++)
res.insert(res.begin(), bs.test(i) ? '1' : '0');
return res;
}
template <typename T>
auto to_string(T v) -> std::string {
bool flg = false;
std::string res = "{";
for (auto const&x : v) {
if (flg) res += ", ";
else flg = true;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { std::cerr << std::endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
std::cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) std::cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
template <size_t N>
class sieve_of_eratosthenes {
std::bitset<N> is_prime_;
public:
constexpr sieve_of_eratosthenes(){
is_prime_ = ~is_prime_;
is_prime_.reset(0), is_prime_.reset(1);
for (size_t p = 2; p < N; p++) {
if (!is_prime_.test(p)) continue;
for (size_t j = 2; j * p < N; j++) {
is_prime_.reset(p * j);
}
}
}
// Return the bitset testing if a number is prime.
constexpr const auto& is_prime() const {return is_prime_;}
// Returns the vector of prime numbers.
template <typename T>
auto primes() const {
std::vector<T> primes{};
for (size_t i = 0; i < N; i++) {
if (is_prime_.test(i)) primes.push_back(i);
}
return primes;
}
};
template <typename T>
auto make_vector(size_t sz, T t) {
return std::vector<T>(sz, t);
}
template <size_t N, typename T, typename U, std::enable_if_t<
N == 1, std::nullptr_t> = nullptr>
auto make_higher_vector(size_t sz, U u) {
return make_vector(sz, T(u));
}
template <size_t N, typename T, std::enable_if_t<
N == 1, std::nullptr_t> = nullptr>
auto make_higher_vector(size_t sz) {
return std::vector<T>(sz);
}
template <size_t N, typename T, typename... Args, std::enable_if_t<
N != 1, std::nullptr_t> = nullptr>
auto make_higher_vector(size_t a, Args... args) {
return make_vector(a, make_higher_vector<N - 1, T>(args...));
}
template <typename T, typename Size_t>
auto& at(T& t, Size_t i) {
return t.at(i);
}
template <typename T, typename Size_t, typename... Args>
auto& at(T& t, Size_t i, Args... args) {
return at(t.at(i), args...);
}
int main() {
std::cin.tie(0); std::cin.sync_with_stdio(false);
constexpr int pmax = 200000;
auto primes = sieve_of_eratosthenes<pmax + 1>{}.primes<int>();
int psize = primes.size();
auto ind = [&] (int x) {
assert(2 <= x && x <= pmax);
auto lb = std::lower_bound(primes.begin(), primes.end(), x);
assert(x == *lb);
return lb - primes.begin();
};
auto factorize = [&] (int x) {
std::vector<int> ret;
for (auto p : primes) {
for (; x % p == 0; x /= p) {
ret.push_back(ind(p));
}
if (x == 1) break;
}
if (x != 1) return std::vector<int>{};
return ret;
};
int n; std::cin >> n;
auto a = [&] {
std::vector<int> a(n);
for (auto&x : a) std::cin >> x;
a.insert(a.begin(), -1);
return a;
}();
int q; std::cin >> q;
auto cum = [&] {
auto cum = std::vector(psize, std::vector(n + 1, 0));
for (int i = 1; i <= n; i++) {
auto crr = a.at(i);
auto factors = factorize(crr);
for (auto x : factors) {
at(cum, x, i)++;
}
}
for (auto& v : cum) {
auto new_v = std::vector<int>(n + 1, 0);
std::partial_sum(v.begin(), v.end(), new_v.begin());
v = std::move(new_v);
}
return cum;
}();
auto range_sum = [&] (int i, int l, int r) {
return at(cum, i, r) - at(cum, i, l);
};
while (q--) {
int p, l, r;
std::cin >> p >> l >> r;
l--;
bool ans = true;
auto factors = factorize(p);
if (p != 1 && factors.size() == 0) ans = false;
int mlt = 0, prv = -1;
for (auto x : factors) {
if (x != prv) {
if (prv != -1 && range_sum(x, l, r) < mlt) ans = false;
mlt = 0;
}
mlt++;
prv = x;
}
if (mlt > 0) {
if (range_sum(prv, l, r) < mlt) ans = false;
}
std::cout << (ans ? "Yes" : "No") << std::endl;
}
return 0;
}
ngtkana