結果

問題 No.8046 yukicoderの過去問
ユーザー minatominato
提出日時 2021-01-23 10:59:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 13,556 bytes
コンパイル時間 4,667 ms
コンパイル使用メモリ 284,896 KB
実行使用メモリ 12,556 KB
最終ジャッジ日時 2024-06-09 15:29:57
合計ジャッジ時間 6,058 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 77 ms
8,684 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 144 ms
12,432 KB
testcase_06 AC 223 ms
12,344 KB
testcase_07 AC 217 ms
12,556 KB
testcase_08 AC 218 ms
12,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using i128 = __int128_t;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
template<class T> using vec = vector<T>;
template<class T> using vvec = vector<vector<T>>;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
constexpr char ln = '\n';
istream& operator>>(istream& is, __int128_t& x) {
    x = 0;
    string s;
    is >> s;
    int n = int(s.size()), it = 0;
    if (s[0] == '-') it++;
    for (; it < n; it++) x = (x * 10 + s[it] - '0');
    if (s[0] == '-') x = -x;
    return is;
}
ostream& operator<<(ostream& os, __int128_t x) {
    if (x == 0) return os << 0;
    if (x < 0) os << '-', x = -x;
    deque<int> deq;
    while (x) deq.emplace_front(x % 10), x /= 10;
    for (int e : deq) os << e;
    return os;
}
template<class T1, class T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
    return os << "(" << p.first << ", " << p.second << ")";
}
template<class T> 
ostream& operator<<(ostream& os, const vector<T>& v) {
    os << "{";
    for (int i = 0; i < int(v.size()); i++) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "}";
}
template<class Container> inline int SZ(const Container& v) { return int(v.size()); }
template<class T> inline void UNIQUE(vector<T>& v) { v.erase(unique(v.begin(), v.end()), v.end()); }
template<class T1, class T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return true; } return false; }
template<class T1, class T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return true; } return false; }
inline int topbit(ull x) { return x == 0 ? -1 : 63 - __builtin_clzll(x); }
inline int botbit(ull x) { return x == 0 ? 64 : __builtin_ctzll(x); }
inline int popcount(ull x) { return __builtin_popcountll(x); }
inline int kthbit(ull x, int k) { return (x >> k) & 1; }
inline constexpr long long TEN(int x) { return x == 0 ? 1 : TEN(x-1) * 10; }
template<class T> inline T ABS(T x) { return max(x, -x); }
const string YESNO[2] = {"NO", "YES"};
const string YesNo[2] = {"No", "Yes"};
const string yesno[2] = {"no", "yes"};
inline void YES(bool t = 1) { cout << YESNO[t] << "\n"; }
inline void Yes(bool t = 1) { cout << YesNo[t] << "\n"; }
inline void yes(bool t = 1) { cout << yesno[t] << "\n"; }
inline void print() { cout << "\n"; }
template<class T>
inline void print(const vector<T>& v) {
    for (auto it = begin(v); it != end(v); ++it) {
        if (it != begin(v)) cout << " ";
        cout << *it;
    }
    print();
}
template<class T, class... Args>
inline void print(const T& x, const Args& ... args) {
    cout << x << " ";
    print(args...);
}
#ifdef MINATO_LOCAL
inline void debug_out() { cerr << endl; }
template <class T, class... Args>
inline void debug_out(const T& x, const Args& ... args) {
    cerr << " " << x;
    debug_out(args...);
}
#define debug(...) cerr << __LINE__ << " : [" << #__VA_ARGS__ << "] =", debug_out(__VA_ARGS__)
#else
#define debug(...) (void(0))
#endif
struct fast_ios { fast_ios() { cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(7); }; } fast_ios_;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

#include <atcoder/all>
using namespace atcoder;
//using mint = modint998244353;
using mint = modint1000000007;
//using mint = modint;

istream& operator>>(istream& is, modint998244353& M) { long long x; is >> x; M = x; return is; }
ostream& operator<<(ostream& os, const modint998244353& M) { return os << M.val(); }
istream& operator>>(istream& is, modint1000000007& M) { long long x; is >> x; M = x; return is; }
ostream& operator<<(ostream& os, const modint1000000007& M) { return os << M.val(); }
template<int m> istream& operator>>(istream& is, static_modint<m>& M) { long long x; is >> x; M = x; return is; }
template<int m> ostream& operator<<(ostream& os, const static_modint<m>& M) { return os << M.val(); }
istream& operator>>(istream& is, modint& M) { long long x; is >> x; M = x; return is; }
ostream& operator<<(ostream& os, const modint& M) { return os << M.val(); }

vector<mint> multiply(vector<mint> a, vector<mint> b) { return convolution(a,b); }
vector<mint> multiply_naive(vector<mint> a, vector<mint> b) {
    int n = int(a.size()), m = int(b.size());
    if (!n or !m) return {};
    vector<mint> ret(n + m - 1);
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            ret[i + j] += a[i] * b[j];
        }
    }
    return ret;
}

//59501818244292734739283969=5.95*10^25 までの値を正しく計算
//最終的な列の大きさが 2^24 までなら動く
//最終的な列の大きさが 2^20 以下のときは,下の 3 つの素数を使ったほうが速い
namespace arbitrary_convolution {
    constexpr int m0 = 167772161;
    constexpr int m1 = 469762049;
    constexpr int m2 = 754974721;
    constexpr int r01 = 104391568; // mint1(m0).inv()
    constexpr int r02 = 323560596; // mint2(m0).inv()
    constexpr int r12 = 399692502; // mint2(m1).inv()
    // constexpr int m0 = 1045430273;
    // constexpr int m1 = 1051721729;
    // constexpr int m2 = 1053818881;
    // constexpr int r01 = 175287122; // mint1(m0).inv()
    // constexpr int r02 = 395182206; // mint2(m0).inv()
    // constexpr int r12 = 526909943; // mint2(m1).inv()
    constexpr int r02r12 = (long long)(r02) * r12 % m2;
    constexpr long long w1 = m0;
    constexpr long long w2 = (long long)(m0) * m1;

    template<class T>
    vector<int> multiply(const vector<T>& a, const vector<T>& b, int mod) {
        int n = int(a.size()), m = int(b.size());

        vector<T> v0 = convolution<m0>(a, b);
        vector<T> v1 = convolution<m1>(a, b);
        vector<T> v2 = convolution<m2>(a, b);
        vector<int> ret(n + m - 1);

        const int W1 = w1 % mod;
        const int W2 = w2 % mod;
        for (int i = 0; i < n + m - 1; i++) {
            int n1 = v1[i], n2 = v2[i], x = v0[i];
            int y = (long long)(n1 + m1 - x) * r01 % m1;
            int z = ((long long)(n2 + m2 - x) * r02r12 + (long long)(m2 - y) * r12) % m2;
            ret[i] = ((long long)(x) + (long long)(y) * W1 + (long long)(z) * W2) % mod;
        }
        return ret;
    }

    template<class D>
    vector<D> multiply(vector<D> a, vector<D> b) {
        int n = int(a.size()), m = int(b.size());
        if (!n or !m) return {};
        if (min(n,m) < 128) {
            if (n < m) {
                swap(n, m);
                swap(a, b);
            }
            vector<D> ret(n + m - 1);
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < m; j++) {
                    ret[i + j] += a[i] * b[j];
                }
            }
            return ret;
        }
        vector<int> a_(n), b_(m);
        for (int i = 0; i < n; i++) a_[i] = a[i].val();
        for (int i = 0; i < m; i++) b_[i] = b[i].val();
        vector<int> c = multiply<int>(a_, b_, D::mod());
        vector<D> ret(n + m - 1);
        for (int i = 0; i < n + m - 1; i++) ret[i] = D::raw(c[i]);
        return ret;
    }
}

template<class D, vector<D> (*op)(vector<D>, vector<D>)> 
struct FormalPowerSeries {
    using Poly = FormalPowerSeries;

    vector<D> v;

    FormalPowerSeries(const vector<D>& v_ = {}) : v(v_) { shrink(); }
    void shrink() {
        while (v.size() && v.back() == 0) v.pop_back();
    }

    int size() const { return int(v.size()); }

    D freq(int p) const { return (p < size()) ? v[p] : D(0); }

    Poly operator+(const Poly& r) const {
        int n = max(size(), r.size());
        vector<D> res(n);
        for (int i = 0; i < n; i++) res[i] = freq(i) + r.freq(i);
        return res;
    }
    Poly operator-(const Poly& r) const {
        int n = max(size(), r.size());
        vector<D> res(n);
        for (int i = 0; i < n; i++) res[i] = freq(i) - r.freq(i);
        return res;
    }
    Poly operator*(const Poly& r) const { return op(v, r.v); }
    Poly operator*(const D& r) const {
        int n = size();
        vector<D> res(n);
        for (int i = 0; i < n; i++) res[i] = v[i] * r;
        return res;
    }
    Poly operator*(const vector<pair<int, D>>& r) const {
        assert(!r.empty());
        int n = size();
        int m = r.back().first;
        vector<D> res(n + m);
        for (int i = 0; i < n; i++) {
            for (auto e : r) {
                res[i + e.first] += v[i] * e.second;
            }
        }
        return res;
    }
    Poly operator/(const D& r) const {
        return *this * r.inv();
    }
    Poly operator/(const Poly& r) const {
        if (size() < r.size()) return {{}};
        int n = size() - r.size() + 1;
        return (rev().pre(n) * r.rev().inv(n)).pre(n).rev(n);
    }
    Poly operator/(const vector<pair<int, D>>& r) const {
        assert(!r.empty());
        int n = size();
        auto [d, c] = r.front();
        assert(d == 0 && c != D(0));
        D ic = D(1) / c;
        vector<D> res(n);
        for (int i = 0; i < n; i++) {
            for (auto& e : r) {
                if (e.first and e.first <= i) {
                    res[i] -= res[i - e.first] * e.second;
                } 
            }
            res[i] += v[i];
            res[i] *= ic;
        }
        return res;
    }
    
    Poly operator%(const Poly& r) const { return *this - *this / r * r; }
    Poly operator<<(int s) const {
        vector<D> res(size() + s);
        for (int i = 0; i < size(); i++) res[i + s] = v[i];
        return res;
    }
    Poly operator>>(int s) const {
        if (size() <= s) return Poly();
        vector<D> res(size() - s);
        for (int i = 0; i < size() - s; i++) res[i] = v[i + s];
        return res;
    }
    Poly& operator+=(const Poly& r) { return *this = *this + r; }
    Poly& operator-=(const Poly& r) { return *this = *this - r; }
    Poly& operator*=(const Poly& r) { return *this = *this * r; }
    Poly& operator*=(const D& r) { return *this = *this * r; }
    Poly& operator*=(const vector<pair<int, D>>& r) { return *this = *this * r; }
    Poly& operator/=(const Poly& r) { return *this = *this / r; }
    Poly& operator/=(const D &r) {return *this = *this/r;}
    Poly& operator/=(const vector<pair<int, D>>& r) { return *this = *this / r; }
    Poly& operator%=(const Poly& r) { return *this = *this % r; }
    Poly& operator<<=(const size_t& n) { return *this = *this << n; }
    Poly& operator>>=(const size_t& n) { return *this = *this >> n; }

    Poly mul(int d, const D& c) const {
        vector<D> res(size() + d);
        for (int i = 0; i < size(); i++) {
            res[i] += v[i];
            res[i+d] += v[i] * c;
        }
        return res;
    }
    Poly div(int d, const D& c) const {
        vector<D> res(size());
        for (int i = 0; i < size(); i++) {
            res[i] = v[i];
            if (i >= d) res[i] -= res[i - d] * c;
        }
        return res;
    }
    Poly pre(int le) const {
        return {{v.begin(), v.begin() + min(size(), le)}};
    }
    Poly rev(int n = -1) const {
        vector<D> res = v;
        if (n != -1) res.resize(n);
        reverse(res.begin(), res.end());
        return res;
    }
    Poly diff() const {
        vector<D> res(max(0, size() - 1));
        for (int i = 1; i < size(); i++) res[i - 1] = freq(i) * i;
        return res;
    }
    Poly inte() const {
        vector<D> res(size() + 1);
        for (int i = 0; i < size(); i++) res[i + 1] = freq(i) / (i + 1);
        return res;
    }

    // f * f.inv() = 1 + g(x)x^m
    Poly inv(int m) const {
        Poly res = Poly({D(1) / freq(0)});
        for (int i = 1; i < m; i *= 2) {
            res = (res * D(2) - res * res * pre(2 * i)).pre(2 * i);
        }
        return res.pre(m);
    }
    Poly exp(int n) const {
        assert(freq(0) == 0);
        Poly f({1}), g({1});
        for (int i = 1; i < n; i *= 2) {
            g = (g * 2 - f * g * g).pre(i);
            Poly q = diff().pre(i - 1);
            Poly w = (q + g * (f.diff() - f * q)).pre(2 * i - 1);
            f = (f + f * (*this - w.inte()).pre(2 * i)).pre(2 * i);
        }
        return f.pre(n);
    }
    Poly log(int n) const {
        assert(freq(0) == 1);
        auto f = pre(n);
        return (f.diff() * f.inv(n - 1)).pre(n - 1).inte();
    }
    Poly sqrt(int n) const {
        assert(freq(0) == 1);
        Poly f = pre(n + 1);
        Poly g({1});
        for (int i = 1; i < n; i *= 2) {
            g = (g + f.pre(2 * i) * g.inv(2 * i)) / 2;
        }
        return g.pre(n + 1);
    }

    Poly pow_mod(ll n, const Poly& mod) {
        Poly x = *this, r = {{1}};
        while (n) {
            if (n & 1) r = r * x % mod;
            x = x * x % mod;
            n >>= 1;
        }
        return r;
    }

    friend ostream& operator<<(ostream& os, const Poly& p) {
        if (p.size() == 0) return os << "0";
        for (auto i = 0; i < p.size(); i++) {
            if (p.v[i] != 0) {
                os << p.v[i] << " x^" << i;
                if (i != p.size() - 1) os << " + ";
            }
        }
        return os;
    }
};

using Poly = FormalPowerSeries<mint, arbitrary_convolution::multiply>;

int main() {
    int K,N; cin >> K >> N;
    Poly f;
    f.v.assign(K+1,0);
    f.v[0] = 1;
    rep(i,N) {
        int x; cin >> x;
        if (x <= K) f.v[x]--;
    }
    f = f.inv(K+1);

    cout << f.freq(K) << ln;
}
0