結果

問題 No.854 公平なりんご分配
ユーザー finefine
提出日時 2019-07-26 22:47:03
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 1,539 ms / 3,153 ms
コード長 5,172 bytes
コンパイル時間 1,949 ms
コンパイル使用メモリ 181,812 KB
実行使用メモリ 246,784 KB
最終ジャッジ日時 2023-09-15 03:24:54
合計ジャッジ時間 18,909 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
7,760 KB
testcase_01 AC 5 ms
7,800 KB
testcase_02 AC 6 ms
7,916 KB
testcase_03 AC 5 ms
7,812 KB
testcase_04 AC 5 ms
7,804 KB
testcase_05 AC 5 ms
7,868 KB
testcase_06 AC 6 ms
7,692 KB
testcase_07 AC 5 ms
7,752 KB
testcase_08 AC 6 ms
7,912 KB
testcase_09 AC 6 ms
7,796 KB
testcase_10 AC 5 ms
7,688 KB
testcase_11 AC 5 ms
7,704 KB
testcase_12 AC 6 ms
8,400 KB
testcase_13 AC 5 ms
8,332 KB
testcase_14 AC 5 ms
7,644 KB
testcase_15 AC 5 ms
8,080 KB
testcase_16 AC 6 ms
8,488 KB
testcase_17 AC 5 ms
8,344 KB
testcase_18 AC 5 ms
8,320 KB
testcase_19 AC 6 ms
8,464 KB
testcase_20 AC 5 ms
8,384 KB
testcase_21 AC 5 ms
8,552 KB
testcase_22 AC 7 ms
8,384 KB
testcase_23 AC 8 ms
9,084 KB
testcase_24 AC 9 ms
10,124 KB
testcase_25 AC 7 ms
9,088 KB
testcase_26 AC 10 ms
10,404 KB
testcase_27 AC 8 ms
10,180 KB
testcase_28 AC 6 ms
8,420 KB
testcase_29 AC 6 ms
8,400 KB
testcase_30 AC 7 ms
8,896 KB
testcase_31 AC 9 ms
9,912 KB
testcase_32 AC 90 ms
59,040 KB
testcase_33 AC 124 ms
34,688 KB
testcase_34 AC 228 ms
78,748 KB
testcase_35 AC 150 ms
62,716 KB
testcase_36 AC 34 ms
11,308 KB
testcase_37 AC 89 ms
55,424 KB
testcase_38 AC 63 ms
46,404 KB
testcase_39 AC 396 ms
82,340 KB
testcase_40 AC 140 ms
30,712 KB
testcase_41 AC 160 ms
51,652 KB
testcase_42 AC 195 ms
77,232 KB
testcase_43 AC 268 ms
54,496 KB
testcase_44 AC 273 ms
71,184 KB
testcase_45 AC 200 ms
20,092 KB
testcase_46 AC 391 ms
71,136 KB
testcase_47 AC 114 ms
42,332 KB
testcase_48 AC 173 ms
76,180 KB
testcase_49 AC 156 ms
77,192 KB
testcase_50 AC 82 ms
55,316 KB
testcase_51 AC 383 ms
73,580 KB
testcase_52 AC 175 ms
36,316 KB
testcase_53 AC 78 ms
35,848 KB
testcase_54 AC 176 ms
41,592 KB
testcase_55 AC 52 ms
36,020 KB
testcase_56 AC 51 ms
37,368 KB
testcase_57 AC 95 ms
31,508 KB
testcase_58 AC 139 ms
17,632 KB
testcase_59 AC 43 ms
28,604 KB
testcase_60 AC 121 ms
31,524 KB
testcase_61 AC 25 ms
12,264 KB
testcase_62 AC 147 ms
27,824 KB
testcase_63 AC 71 ms
29,276 KB
testcase_64 AC 30 ms
15,996 KB
testcase_65 AC 102 ms
68,856 KB
testcase_66 AC 72 ms
22,824 KB
testcase_67 AC 125 ms
46,164 KB
testcase_68 AC 104 ms
23,880 KB
testcase_69 AC 85 ms
82,376 KB
testcase_70 AC 42 ms
31,364 KB
testcase_71 AC 49 ms
33,432 KB
testcase_72 AC 44 ms
11,240 KB
testcase_73 AC 69 ms
56,728 KB
testcase_74 AC 157 ms
67,492 KB
testcase_75 AC 92 ms
33,740 KB
testcase_76 AC 95 ms
54,300 KB
testcase_77 AC 114 ms
74,812 KB
testcase_78 AC 179 ms
26,028 KB
testcase_79 AC 168 ms
48,068 KB
testcase_80 AC 163 ms
50,584 KB
testcase_81 AC 91 ms
51,644 KB
testcase_82 AC 270 ms
246,720 KB
testcase_83 AC 454 ms
246,700 KB
testcase_84 AC 461 ms
246,676 KB
testcase_85 AC 463 ms
246,680 KB
testcase_86 AC 237 ms
246,672 KB
testcase_87 AC 267 ms
246,756 KB
testcase_88 AC 265 ms
246,776 KB
testcase_89 AC 265 ms
246,764 KB
testcase_90 AC 268 ms
246,744 KB
testcase_91 AC 267 ms
246,672 KB
testcase_92 AC 1,539 ms
246,784 KB
testcase_93 AC 1,534 ms
246,696 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

bool is_prime(int n) {
    if (n % 2 == 0) return false;
    for (int i = 2; i * i <= n; i++) {
        if (n % i == 0) return false;
    }
    return n != 1;
}

vector<int> divisor(int n) {
    vector<int> res;
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            res.emplace_back(i);
            if (i != n / i) res.emplace_back(n / i);
        }
    }
    return res;
}

map<int, int> prime_factor(int n) {
    map<int, int> res;
    for (int i = 2; i * i <= n; i++) {
        while (n % i == 0) {
            ++res[i];
            n /= i;
        }
    }
    if (n != 1) res[n] = 1;
    return res;
}

vector<int> sieve(int n) {
    vector<int> res;
    vector<bool> is_prime(n + 1, true);
    is_prime[0] = false;
    is_prime[1] = false;
    for (int i = 2; i <= n; i++) {
        if (!is_prime[i]) continue;
        res.emplace_back(i);
        for (int j = 2 * i; j <= n; j += i) is_prime[j] = false;
    }
    return res;
}

template <typename T>
struct SegmentTree {
    int n;
    vector<T> data;
    T INITIAL_VALUE;

    //使うときは、この2つを適宜変更する
    static T merge(T x, T y);
    void updateNode(int k, T x);

    SegmentTree(int size, T initial_value) {
        n = 1;
        INITIAL_VALUE = initial_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_VALUE);
    }

    SegmentTree(const vector<T>& v, T initial_value) {
        int size = v.size();
        n = 1;
        INITIAL_VALUE = initial_value;
        while (n < size) n *= 2;
        data.resize(2 * n - 1, INITIAL_VALUE);

        for (int i = 0; i < size; i++) data[i + n - 1] = v[i];
        for (int i = n - 2; i >= 0; i--) data[i] = merge(data[i * 2 + 1], data[i * 2 + 2]);
    }

    T getLeaf(int k) {
        return data[k + n - 1];
    }

    void update(int k, T x) {
        k += n - 1; //葉の節点
        updateNode(k, x);
        while (k > 0) {
            k = (k - 1) / 2;
            data[k] = merge(data[k * 2 + 1], data[k * 2 + 2]);
        }
    }

    //区間[a, b)に対するクエリに答える
    //k:節点番号, [l, r):節点に対応する区間
    T query(int a, int b, int k, int l, int r) {
        //[a, b)と[l, r)が交差しない場合
        if (r <= a || b <= l) return INITIAL_VALUE;
        //[a, b)が[l, r)を含む場合、節点の値
        if (a <= l && r <= b) return data[k];
        else {
            //二つの子をマージ
            T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
            T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
            return merge(vl, vr);
        }
    }

    //外から呼ぶ用
    T query(int a, int b) {
        return query(a, b, 0, 0, n);
    }

    //非再帰版: バグってるかもしれないので定数倍高速化する時以外使わないで
    //区間[a, b)に対するクエリに答える
    T query_fast(int a, int b) {
        T vl = INITIAL_VALUE, vr = INITIAL_VALUE;
        for (int l = a + n, r = b + n; l != r; l >>= 1, r >>= 1) {
            if (l & 1) vl = merge(vl, data[l++ - 1]);
            if (r & 1) vr = merge(data[--r - 1], vr);
        }
        return merge(vl, vr);
    }
};

//使うときは以下2つを変更
//非可換の場合は順序に注意!!!
template <typename T>
T SegmentTree<T>::merge(T x, T y) {
    return x + y;
}

template <typename T>
void SegmentTree<T>::updateNode(int k, T x) {
    data[k] = x;
}

const int MAXV = 2000;
const ll INF = 1000000000;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    vector<int> sv = sieve(MAXV);
    /* 
    vector<int> ids(MAXV + 1, -1);
    for (int i = 0; i < sv.size(); i++) {
        ids[sv[i]] = i;
    }*/

    vector< vector<ll> > cnt(MAXV + 1, vector<ll>(sv.size(), 0));
    cnt[0] = vector<ll>(sv.size(), INF);
    for (int i = 0; i < sv.size(); i++) {
        int x = sv[i];
        for (int j = x; j <= MAXV; j += x) {
            int d = j;
            while (d % x == 0) {
                cnt[j][i]++;
                d /= x;
            }
        }
    }

    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];

    vector< vector<ll> > vst(sv.size(), vector<ll>(n + 1, 0));
    for (int i = 0; i < sv.size(); i++) {
        for (int j = 0; j < n; j++) {
            vst[i][j + 1] = vst[i][j] + cnt[a[j]][i];
        }
    }

    int q;
    cin >> q;
    for (int i = 0; i < q; i++) {
        int p, l, r;
        cin >> p >> l >> r;
        l--;
        bool ans = true;
        bool f0 = false;
        for (int j = 0; j < sv.size(); j++) {
            ll x = 0;
            while (p % sv[j] == 0) {
                x++;
                p /= sv[j];
            }
            ll sum = vst[j][r] - vst[j][l];
            //cerr << sum << " ";
            if (sum >= INF) {
                ans = true;
                f0 = true;
                break;
            }
            if (x > sum) {
                ans = false;
                break;
            }
        }
        if (!f0) ans &= (p == 1);
        cout << (ans ? "Yes" : "NO") << "\n";
    }
    return 0;
}
0