Submission #25291896


Source Code Expand

#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <vector>
#include <iomanip>
#include <queue>
#include <cassert>
#include <complex>
#include <cstring>
using namespace std;
#ifndef ONLINE_JUDGE
inline int __builtin_clz(int v) { // 返回前导0的个数
    return __lzcnt(v);
}
inline int __builtin_ctz(int v) { // 返回末尾0的个数
    if (v == 0) {
        return 0;
    }
    __asm {
        bsf eax, dword ptr[v];
    }
}
inline int __builtin_popcount(int v) { // 返回二进制中1的个数
    return __popcnt(v);
}
#endif
namespace FFT {
    typedef double dbl;
    constexpr int MOD = 1e9 + 7;
    bool ok = false;
    struct num {
        dbl x, y;
        num() { x = y = 0; }
        num(dbl x, dbl y) : x(x), y(y) {}
    };

    inline num operator+(num a, num b) { return num(a.x + b.x, a.y + b.y); }
    inline num operator-(num a, num b) { return num(a.x - b.x, a.y - b.y); }
    inline num operator*(num a, num b) { return num(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); }
    inline num conj(num a) { return num(a.x, -a.y); }

    int base = 1;
    vector<num> roots = { {0, 0}, {1, 0} };
    vector<int> rev = { 0, 1 };

    const dbl PI = acosl(-1.0);

    void ensure_base(int nbase) {
        if (nbase <= base) {
            return;
        }
        rev.resize(1 << nbase);
        for (int i = 0; i < (1 << nbase); i++) {
            rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (nbase - 1));
        }
        roots.resize(1 << nbase);
        while (base < nbase) {
            dbl angle = 2 * PI / (1 << (base + 1));
            for (int i = 1 << (base - 1); i < (1 << base); i++) {
                roots[i << 1] = roots[i];
                dbl angle_i = angle * (2 * i + 1 - (1 << base));
                roots[(i << 1) + 1] = num(cos(angle_i), sin(angle_i));
            }
            base++;
        }
    }

    void fft(vector<num>& a, int n = -1) {
        if (n == -1) {
            n = a.size();
        }
        assert((n & (n - 1)) == 0);
        int zeros = __builtin_ctz(n);
        ensure_base(zeros);
        int shift = base - zeros;
        for (int i = 0; i < n; i++) {
            if (i < (rev[i] >> shift)) {
                swap(a[i], a[rev[i] >> shift]);
            }
        }
        for (int k = 1; k < n; k <<= 1) {
            for (int i = 0; i < n; i += 2 * k) {
                for (int j = 0; j < k; j++) {
                    num z = a[i + j + k] * roots[j + k];
                    a[i + j + k] = a[i + j] - z;
                    a[i + j] = a[i + j] + z;
                }
            }
        }
    }

    vector<num> fa, fb;

    vector<int> mod_multiply(vector<int>& a, vector<int>& b, int nbase = 12, int eq = 0) {
        int need = a.size() + b.size() - 1;
        int sz = 1 << nbase;
        if (sz > (int)fa.size()) {
            fa.resize(sz);
        }
        for (int i = 0; i < (int)a.size(); i++) {
            int x = (a[i] % MOD + MOD) % MOD;
            fa[i] = num(x & ((1 << 15) - 1), x >> 15);
        }
        fill(fa.begin() + a.size(), fa.begin() + sz, num{ 0, 0 });
        fft(fa, sz);
        if (sz > (int) fb.size()) {
            fb.resize(sz);
        }
        if (eq) {
            copy(fa.begin(), fa.begin() + sz, fb.begin());
        } else {
            for (int i = 0; i < (int)b.size(); i++) {
                int x = (b[i] % MOD + MOD) % MOD;
                fb[i] = num(x & ((1 << 15) - 1), x >> 15);
            }
            fill(fb.begin() + b.size(), fb.begin() + sz, num{ 0, 0 });
            fft(fb, sz);
        }
        dbl ratio = 0.25 / sz;
        num r2(0, -1);
        num r3(ratio, 0);
        num r4(0, -ratio);
        num r5(0, 1);
        for (int i = 0; i <= (sz >> 1); i++) {
            int j = (sz - i) & (sz - 1);
            num a1 = (fa[i] + conj(fa[j]));
            num a2 = (fa[i] - conj(fa[j])) * r2;
            num b1 = (fb[i] + conj(fb[j])) * r3;
            num b2 = (fb[i] - conj(fb[j])) * r4;
            if (i != j) {
                num c1 = (fa[j] + conj(fa[i]));
                num c2 = (fa[j] - conj(fa[i])) * r2;
                num d1 = (fb[j] + conj(fb[i])) * r3;
                num d2 = (fb[j] - conj(fb[i])) * r4;
                fa[i] = c1 * d1 + c2 * d2 * r5;
                fb[i] = c1 * d2 + c2 * d1;
            }
            fa[j] = a1 * b1 + a2 * b2 * r5;
            fb[j] = a1 * b2 + a2 * b1;
        }
        fft(fa, sz);
        fft(fb, sz);
        vector<int> res(need);
        for (int i = 0; i < need; i++) {
            long long aa = fa[i].x + 0.5;
            long long bb = fb[i].x + 0.5;
            long long cc = fa[i].y + 0.5;
            res[i] = (aa + ((bb % MOD) << 15) + ((cc % MOD) << 30)) % MOD;
        }
        return res;
    }
};

//fast IO by yosupo
struct Scanner {
    FILE* fp = nullptr;
    char line[(1 << 15) + 1];
    size_t st = 0, ed = 0;
    void reread() {
        memmove(line, line + st, ed - st);
        ed -= st;
        st = 0;
        ed += fread(line + ed, 1, (1 << 15) - ed, fp);
        line[ed] = '\0';
    }
    bool succ() {
        while (true) {
            if (st == ed) {
                reread();
                if (st == ed) return false;
            }
            while (st != ed && isspace(line[st])) st++;
            if (st != ed) break;
        }
        if (ed - st <= 50) reread();
        return true;
    }
    template <class T, enable_if_t<is_same<T, string>::value, int> = 0>
    bool read_single(T& ref) {
        if (!succ()) return false;
        while (true) {
            size_t sz = 0;
            while (st + sz < ed && !isspace(line[st + sz])) sz++;
            ref.append(line + st, sz);
            st += sz;
            if (!sz || st != ed) break;
            reread();
        }
        return true;
    }
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    bool read_single(T& ref) {
        if (!succ()) return false;
        bool neg = false;
        if (line[st] == '-') {
            neg = true;
            st++;
        }
        ref = T(0);
        while (isdigit(line[st])) {
            ref = 10 * ref + (line[st++] - '0');
        }
        if (neg) ref = -ref;
        return true;
    }
    template <class T> bool read_single(vector<T>& ref) {
        for (auto& d : ref) {
            if (!read_single(d)) return false;
        }
        return true;
    }
    void read() {}
    template <class H, class... T> void read(H& h, T&... t) {
        bool f = read_single(h);
        assert(f);
        read(t...);
    }
    Scanner(FILE* _fp) : fp(_fp) {}
};

struct Printer {
public:
    template <bool F = false> void write() {}
    template <bool F = false, class H, class... T>
    void write(const H& h, const T&... t) {
        if (F) write_single(' ');
        write_single(h);
        write<true>(t...);
    }
    template <class... T> void writeln(const T&... t) {
        write(t...);
        write_single('\n');
    }

    Printer(FILE* _fp) : fp(_fp) {}
    ~Printer() { flush(); }

private:
    static constexpr size_t SIZE = 1 << 15;
    FILE* fp;
    char line[SIZE], small[50];
    size_t pos = 0;
    void flush() {
        fwrite(line, 1, pos, fp);
        pos = 0;
    }
    void write_single(const char& val) {
        if (pos == SIZE) flush();
        line[pos++] = val;
    }
    template <class T, enable_if_t<is_integral<T>::value, int> = 0>
    void write_single(T val) {
        if (pos > (1 << 15) - 50) flush();
        if (val == 0) {
            write_single('0');
            return;
        }
        if (val < 0) {
            write_single('-');
            val = -val;  // todo min
        }
        size_t len = 0;
        while (val) {
            small[len++] = char('0' + (val % 10));
            val /= 10;
        }
        for (size_t i = 0; i < len; i++) {
            line[pos + i] = small[len - 1 - i];
        }
        pos += len;
    }
    void write_single(const string& s) {
        for (char c : s) write_single(c);
    }
    void write_single(const char* s) {
        size_t len = strlen(s);
        for (size_t i = 0; i < len; i++) write_single(s[i]);
    }
    template <class T> void write_single(const vector<T>& val) {
        auto n = val.size();
        for (size_t i = 0; i < n; i++) {
            if (i) write_single(' ');
            write_single(val[i]);
        }
    }
    void write_single(long double d) {
        {
            long long v = d;
            write_single(v);
            d -= v;
        }
        write_single('.');
        for (int _ = 0; _ < 8; _++) {
            d *= 10;
            long long v = d;
            write_single(v);
            d -= v;
        }
    }
};

Scanner sc(stdin);
Printer pr(stdout);

int main() {
    int n, m, q;
    sc.read(n);
    sc.read(m);
    sc.read(q);
    vector<vector<int>> pre(2002, vector<int>(2001, 0));
    vector<vector<int>> suf(2002, vector<int>(2001, 0));
    vector<int> a(2001, 0);
    vector<int> res(q);
    vector<vector<pair<int, int>>> queries(2001);
    vector<int> tmp(2001, 1);
    for (int i = 1; i <= n; i++) {
        sc.read(a[i]);
    }
    pre[0][0] = suf[2001][0] = 1;
    int nbase = 12;
    FFT::ensure_base(nbase);
    for (int i = 1; i <= 2000; i++) {
        for (int j = 0; j <= 2000; j++) tmp[j] = (j <= a[i]);
        auto conv = FFT::mod_multiply(tmp, pre[i - 1]);
        for (int j = 0; j <= min(m, (int)conv.size() - 1); j++) {
            pre[i][j] = conv[j];
        }
    }
    for (int i = 2000; i; i--) {
        for (int j = 0; j <= 2000; j++) tmp[j] = (j <= a[i]);
        auto conv = FFT::mod_multiply(tmp, suf[i + 1]);
        for (int j = 0; j <= min(m, (int)conv.size() - 1); j++) {
            suf[i][j] = conv[j];
        }
    }
    for (int i = 0; i < q; i++) {
        int x, y;
        sc.read(x);
        sc.read(y);
        queries[x].push_back({ i,y });
    }
    for (int i = 1; i <= 2000; i++) {
        if (queries[i].empty()) continue;
        auto conv = FFT::mod_multiply(pre[i - 1], suf[i + 1]);
        for (auto [x, y] : queries[i]) {
            res[x] = conv[m - y];
        }
    }
    for (int i = 0; i < q; i++) {
        pr.writeln(res[i]);
    }
    return 0;
}

/*
4 7 5
1 2 3 4
4 0
3 2
2 1
1 1
1 0

*/

Submission Info

Submission Time
Task D - 注文の多い高橋商店
User st1vdy
Language C++ (GCC 9.2.1)
Score 80
Code Size 10606 Byte
Status TLE
Exec Time 2120 ms
Memory 45460 KiB

Judge Result

Set Name Sample Subtask1 Subtask2 Subtask3 Subtask4
Score / Max Score 0 / 0 10 / 10 20 / 20 50 / 50 0 / 20
Status
AC × 2
AC × 10
AC × 13
AC × 13
AC × 18
TLE × 5
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
Subtask1 sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt
Subtask2 sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt
Subtask3 sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask3_01.txt, subtask3_02.txt, subtask3_03.txt
Subtask4 sample_01.txt, sample_02.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask3_01.txt, subtask3_02.txt, subtask3_03.txt, subtask4_01.txt, subtask4_02.txt, subtask4_03.txt, subtask4_04.txt, subtask4_05.txt, subtask4_06.txt, subtask4_07.txt
Case Name Status Exec Time Memory
sample_01.txt AC 1399 ms 35564 KiB
sample_02.txt AC 1353 ms 35616 KiB
subtask1_01.txt AC 1352 ms 35564 KiB
subtask1_02.txt AC 1379 ms 35328 KiB
subtask1_03.txt AC 1438 ms 35472 KiB
subtask1_04.txt AC 1357 ms 35456 KiB
subtask1_05.txt AC 1404 ms 35548 KiB
subtask1_06.txt AC 1389 ms 35400 KiB
subtask1_07.txt AC 1379 ms 35648 KiB
subtask1_08.txt AC 1372 ms 35616 KiB
subtask2_01.txt AC 1390 ms 36260 KiB
subtask2_02.txt AC 1390 ms 36856 KiB
subtask2_03.txt AC 1406 ms 36888 KiB
subtask3_01.txt AC 1564 ms 35604 KiB
subtask3_02.txt AC 1652 ms 35636 KiB
subtask3_03.txt AC 1628 ms 35596 KiB
subtask4_01.txt AC 1893 ms 44936 KiB
subtask4_02.txt TLE 2061 ms 45400 KiB
subtask4_03.txt TLE 2050 ms 43440 KiB
subtask4_04.txt AC 1447 ms 41376 KiB
subtask4_05.txt TLE 2120 ms 45460 KiB
subtask4_06.txt TLE 2063 ms 43748 KiB
subtask4_07.txt TLE 2060 ms 45212 KiB