結果

問題 No.2763 Macaron Gift Box
ユーザー mikammikam
提出日時 2024-05-18 17:15:14
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 655 ms / 3,000 ms
コード長 13,828 bytes
コンパイル時間 9,982 ms
コンパイル使用メモリ 428,860 KB
実行使用メモリ 12,568 KB
最終ジャッジ日時 2024-05-18 17:15:29
合計ジャッジ時間 13,921 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 170 ms
7,152 KB
testcase_08 AC 44 ms
5,376 KB
testcase_09 AC 92 ms
5,376 KB
testcase_10 AC 400 ms
11,216 KB
testcase_11 AC 423 ms
11,728 KB
testcase_12 AC 655 ms
12,528 KB
testcase_13 AC 495 ms
12,568 KB
testcase_14 AC 45 ms
5,376 KB
testcase_15 AC 44 ms
5,376 KB
testcase_16 AC 44 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/all>
using namespace atcoder;
#include <atcoder/internal_math>
using namespace internal;
#include <bits/stdc++.h>
using namespace std;
#include <boost/multiprecision/cpp_int.hpp>
#pragma GCC target("avx2")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i,a,b) for (int i = (int)(a); i < (int)(b); i++)
#define all(v) v.begin(),v.end()
#define inc(x,l,r) ((l)<=(x)&&(x)<(r)) 
#define Unique(x) sort(all(x)), x.erase(unique(all(x)), x.end())
#define pcnt __builtin_popcountll
#define pb push_back
typedef long long ll;
#define int ll
using ld = long double;
using vi = vector<int>;
using vs = vector<string>;
using P = pair<int,int>;
using vp = vector<P>;
using ull = unsigned long long;
using Bint = boost::multiprecision::cpp_int;
template<typename T1,typename T2> bool chmax(T1 &a, const T2 b) {if (a < b) {a = b; return true;} else return false; }
template<typename T1,typename T2> bool chmin(T1 &a, const T2 b) {if (a > b) {a = b; return true;} else return false; }
template<typename T> using priority_queue_greater = priority_queue<T, vector<T>, greater<T>>;
template<typename T1,typename T2> ostream &operator<< (ostream &os, const pair<T1,T2> &p){os << p.first <<" "<<p.second;return os;}
ostream &operator<< (ostream &os, const modint1000000007 &m){os << m.val();return os;}
istream &operator>> (istream &is, modint1000000007 &m){ll in;is>>in;m=in;return is;}
ostream &operator<< (ostream &os, const modint998244353 &m){os << m.val();return os;}
istream &operator>> (istream &is, modint998244353 &m){ll in;is>>in;m=in;return is;}
template<typename T> istream &operator>>(istream& is,vector<T> &v){for(T &in:v)is>>in;return is;}
template<class... T> void input(T&... a){(cin>> ... >> a);}
#ifdef LOCAL
template<typename T> ostream &operator<<(ostream &os,const vector<T> &v){os<<"\x1b[32m";rep(i,v.size())os<<v[i]<<(i+1!=v.size()?" ":"");os<<"\x1b[0m";return os;}
template<class T> int print(T& a){cout << "\x1b[32m"<< a<< '\n' << "\x1b[0m";return 0;}
template<class T,class... Ts> int print(const T&a, const Ts&... b){cout << "\x1b[32m" << a;(cout<<...<<(cout<<' ',b));cout<<'\n' << "\x1b[0m";return 0;}
#else
template<typename T> ostream &operator<<(ostream &os,const vector<T> &v){rep(i,v.size())os<<v[i]<<(i+1!=v.size()?" ":"");return os;}
template<class T> int print(T& a){cout << a<< '\n';return 0;}
template<class T,class... Ts> int print(const T&a, const Ts&... b){cout << a;(cout<<...<<(cout<<' ',b));cout<<'\n';return 0;}
#endif
#define VI(v,n) vi v(n); input(v)
#define INT(...) int __VA_ARGS__; input(__VA_ARGS__)
#define STR(...) string __VA_ARGS__; input(__VA_ARGS__)
#define CHAR(...) char __VA_ARGS__; input(__VA_ARGS__)
int sign(ll x){return x>0?1:x<0?-1:0;}
ll ceil(ll x,ll y){assert(y!=0);if(sign(x)==sign(y))return (x+y-1)/y;return -((-x/y));}
ll floor(ll x,ll y){assert(y!=0);if(sign(x)==sign(y))return x/y;if(y<0)x*=-1,y*=-1;return x/y-(x%y<0);}
ll abs(ll x,ll y){return abs(x-y);}
ll bit(int n){return 1ll<<n;}
template<class T> bool ins(string s,T t){return s.find(t)!=string::npos;}
P operator+ (const P &p, const P &q){ return P{p.first+q.first,p.second+q.second};}
P operator- (const P &p, const P &q){ return P{p.first-q.first,p.second-q.second};}
int yesno(bool ok,string y="Yes",string n="No"){ cout<<(ok?y:n)<<endl;return 0;}
int YESNO(bool ok,string y="YES",string n="NO"){ cout<<(ok?y:n)<<endl;return 0;}
int takao(bool takahashi){return yesno(takahashi,"Takahashi","Aoki");}
struct __m___m__ {__m___m__(){cin.tie(0);ios_base::sync_with_stdio(false);cout << fixed << setprecision(20);}} _m_m_;
int di[]={0,1,0,-1,-1,-1,1,1};
int dj[]={1,0,-1,0,-1,1,-1,1};
const ll INF = 8e18;
//using mint = modint1000000007;
using mint = modint998244353;
//mint stom(const string &s,int b=10){mint res = 0;for(auto c:s)res *= b,res += c-'0';return res;}
#define drep2(i, m, n) for (int i = (m)-1; i >= (n); --i)
#define drep(i, n) drep2(i, n, 0)

template<class T>
struct FormalPowerSeries : vector<T> {
    using vector<T>::vector;
    using vector<T>::operator=;
    using F = FormalPowerSeries;

    F operator-() const {
        F res(*this);
        for (auto &e : res) e = -e;
        return res;
    }
    F &operator*=(const T &g) {
        for (auto &e : *this) e *= g;
        return *this;
    }
    F &operator/=(const T &g) {
        assert(g != T(0));
        *this *= g.inv();
        return *this;
    }
    F &operator+=(const F &g) {
        int n = (*this).size(), m = g.size();
        rep(i, min(n, m)) (*this)[i] += g[i];
        return *this;
    }
    F &operator-=(const F &g) {
        int n = (*this).size(), m = g.size();
        rep(i, min(n, m)) (*this)[i] -= g[i];
        return *this;
    }
    F &operator<<=(const int d) {
        int n = (*this).size();
        (*this).insert((*this).begin(), d, 0);
        (*this).resize(n);
        return *this;
    }
    F &operator>>=(const int d) {
        int n = (*this).size();
        (*this).erase((*this).begin(), (*this).begin() + min(n, d));
        (*this).resize(n);
        return *this;
    }
    F inv(int d = -1) const {
        int n = (*this).size();
        assert(n != 0 && (*this)[0] != 0);
        if (d == -1) d = n;
        assert(d > 0);
        F res{(*this)[0].inv()};
        while (res.size() < d) {
        int m = size(res);
        F f(begin(*this), begin(*this) + min(n, 2*m));
        F r(res);
        f.resize(2*m), internal::butterfly(f);
        r.resize(2*m), internal::butterfly(r);
        rep(i, 2*m) f[i] *= r[i];
        internal::butterfly_inv(f);
        f.erase(f.begin(), f.begin() + m);
        f.resize(2*m), internal::butterfly(f);
        rep(i, 2*m) f[i] *= r[i];
        internal::butterfly_inv(f);
        T iz = T(2*m).inv(); iz *= -iz;
        rep(i, m) f[i] *= iz;
        res.insert(res.end(), f.begin(), f.begin() + m);
        }
        return {res.begin(), res.begin() + d};
    }
    F pow(int t){
        if(t==1)return *this;
        F r = pow(t>>1);
        r *= r;
        if(t&1)r*=(*this);
        return r;
    }

    // O(n log n)
    F &multiply_inplace(const F &g, int d = -1) {
        int n = this->size();
        if (d == -1) d = n;
        assert(d >= 0);
        *this = convolution(move(*this), g);
        this->resize(d);
        return *this;
    }
    F multiply(const F &g, const int d = -1) const { return F(*this).multiply_inplace(g, d); }
    // O(n log n)
    F &divide_inplace(const F &g, int d = -1) {
        int n = this->size();
        if (d == -1) d = n;
        assert(d >= 0);
        *this = convolution(move(*this), g.inv(d));
        this->resize(d);
        return *this;
    }
    F divide(const F &g, const int d = -1) const { return F(*this).divide_inplace(g, d); }

    F &operator*=(vector<pair<int, T>> g) {
        sort(all(g),[](const pair<int,T> &a,const pair<int,T> &b){return a.first<b.first;});
        int n = (*this).size();
        auto [d, c] = g.front();
        if (d == 0) g.erase(g.begin());
        else c = 0;
        drep(i, n) {
            (*this)[i] *= c;
            for (auto &[j, b] : g) {
                if (j > i) break;
                (*this)[i] += (*this)[i-j] * b;
            }
        }
        return *this;
    }
    F &operator/=(vector<pair<int, T>> g) {
        sort(all(g),[](const pair<int,T> &a,const pair<int,T> &b){return a.first<b.first;});
        int n = (*this).size();
        auto [d, c] = g.front();
        assert(d == 0 && c != T(0));
        T ic = c.inv();
        g.erase(g.begin());
        rep(i, n) {
            for (auto &[j, b] : g) {
                if (j > i) break;
                (*this)[i] -= (*this)[i-j] * b;
            }
            (*this)[i] *= ic;
        }
        return *this;
    }

    // multiply and divide (1 + cz^d)
    void multiply(const int d, const T c) { 
        int n = (*this).size();
        if (c == T(1)) drep(i, n-d) (*this)[i+d] += (*this)[i];
        else if (c == T(-1)) drep(i, n-d) (*this)[i+d] -= (*this)[i];
        else drep(i, n-d) (*this)[i+d] += (*this)[i] * c;
    }
    void divide(const int d, const T c) {
        int n = (*this).size();
        if (c == T(1)) rep(i, n-d) (*this)[i+d] -= (*this)[i];
        else if (c == T(-1)) rep(i, n-d) (*this)[i+d] += (*this)[i];
        else rep(i, n-d) (*this)[i+d] -= (*this)[i] * c;
    }

    T eval(const T &a) const {
        T x(1), res(0);
        for (auto e : *this) res += e * x, x *= a;
        return res;
    }
    // O(n)
    F &integ_inplace() {
        int n = this->size();
        assert(n > 0);
        if (n == 1) return *this = F{0};
        this->insert(this->begin(), 0);
        this->pop_back();
        vector<T> inv(n);
        inv[1] = 1;
        int p = T::mod();
        rep2(i, 2, n) inv[i] = - inv[p%i] * (p/i);
        rep2(i, 2, n) (*this)[i] *= inv[i];
        return *this;
    }
    F integ() const { return F(*this).integ_inplace(); }

    // O(n)
    F &deriv_inplace() {
        int n = this->size();
        assert(n > 0);
        rep2(i, 2, n) (*this)[i] *= i;
        this->erase(this->begin());
        this->push_back(0);
        return *this;
    }
    F deriv() const { return F(*this).deriv_inplace(); }

    // O(n log n)
    F &log_inplace(int d = -1) {
        int n = this->size();
        assert(n > 0 && (*this)[0] == 1);
        if (d == -1) d = n;
        assert(d >= 0);
        if (d < n) this->resize(d);
        F f_inv = this->inv();
        this->deriv_inplace();
        this->multiply_inplace(f_inv);
        this->integ_inplace();
        return *this;
    }
    F log(const int d = -1) const { return F(*this).log_inplace(d); }

    // O(n log n)
    F &exp_inplace(int d = -1) {
        int n = this->size();
        assert(n > 0 && (*this)[0] == 0);
        if (d == -1) d = n;
        assert(d >= 0);
        F g{1}, g_fft;
        this->resize(d);
        (*this)[0] = 1;
        F h_drv(this->deriv());
        for (int m = 1; m < d; m *= 2) {
        // prepare
        F f_fft(this->begin(), this->begin() + m);
        f_fft.resize(2*m), internal::butterfly(f_fft);

        // Step 2.a'
        if (m > 1) {
            F _f(m);
            rep(i, m) _f[i] = f_fft[i] * g_fft[i];
            internal::butterfly_inv(_f);
            _f.erase(_f.begin(), _f.begin() + m/2);
            _f.resize(m), internal::butterfly(_f);
            rep(i, m) _f[i] *= g_fft[i];
            internal::butterfly_inv(_f);
            _f.resize(m/2);
            _f /= T(-m) * m;
            g.insert(g.end(), _f.begin(), _f.begin() + m/2);
        }

        // Step 2.b'--d'
        F t(this->begin(), this->begin() + m);
        t.deriv_inplace();
        {
            // Step 2.b'
            F r{h_drv.begin(), h_drv.begin() + m-1};
            // Step 2.c'
            r.resize(m); internal::butterfly(r);
            rep(i, m) r[i] *= f_fft[i];
            internal::butterfly_inv(r);
            r /= -m;
            // Step 2.d'
            t += r;
            t.insert(t.begin(), t.back()); t.pop_back();
        }

        // Step 2.e'
        t.resize(2*m); internal::butterfly(t); 
        g_fft = g; g_fft.resize(2*m); internal::butterfly(g_fft);
        rep(i, 2*m) t[i] *= g_fft[i];
        internal::butterfly_inv(t);
        t.resize(m);
        t /= 2*m;
        
        // Step 2.f'
        F v(this->begin() + m, this->begin() + min(d, 2*m)); v.resize(m);
        t.insert(t.begin(), m-1, 0); t.push_back(0);
        t.integ_inplace();
        rep(i, m) v[i] -= t[m+i];

        // Step 2.g'
        v.resize(2*m); internal::butterfly(v);
        rep(i, 2*m) v[i] *= f_fft[i];
        internal::butterfly_inv(v);
        v.resize(m);
        v /= 2*m;

        // Step 2.h'
        rep(i, min(d-m, m)) (*this)[m+i] = v[i];
        }
        return *this;
    }
    F exp(const int d = -1) const { return F(*this).exp_inplace(d); }

    // O(n log n)
    F &pow_inplace(ll k, int d = -1) {
        int n = this->size();
        if (d == -1) d = n;
        assert(d >= 0);
        int l = 0;
        while ((*this)[l] == 0) ++l;
        if (l > d/k) return *this = F(d);
        T ic = (*this)[l].inv();
        T pc = (*this)[l].pow(k);
        this->erase(this->begin(), this->begin() + l);
        *this *= ic;
        this->log_inplace();
        *this *= k;
        this->exp_inplace();
        *this *= pc;
        this->insert(this->begin(), l*k, 0);
        this->resize(d);
        return *this;
    }
    F pow(const ll k, const int d = -1) const { return F(*this).pow_inplace(k, d); }
    F &operator*=(const F &g) { return multiply_inplace(g); }
    F &operator/=(const F &g) { return divide_inplace(g); }
    F operator*(const T &g) const { return F(*this) *= g; }
    F operator/(const T &g) const { return F(*this) /= g; }
    F operator+(const F &g) const { return F(*this) += g; }
    F operator-(const F &g) const { return F(*this) -= g; }
    F operator<<(const int d) const { return F(*this) <<= d; }
    F operator>>(const int d) const { return F(*this) >>= d; }
    F operator*(vector<pair<int, T>> g) const { return F(*this) *= g; }
    F operator/(vector<pair<int, T>> g) const { return F(*this) /= g; }
};
using fps = FormalPowerSeries<mint>;
using sfps = vector<pair<int,mint>>;

// log(1-x^k) n/k個の非ゼロの項の和
sfps log_(int n,int k){
    sfps res;
    for(int i=1;i*k<=n;i++)res.emplace_back(i*k,-(mint(i).inv()));
    return res;
}
void add_sfps(fps &f,sfps &s){
    for(auto [k,v]:s)f[k]+=v;
}
signed main() {

    INT(N,K);
    fps f(N+1);
    for(int j=1;j<=N;j++){
        auto s = log_(N,(K+1)*j);
        add_sfps(f,s);
    }
    for(int j=1;j<=N;j++){
        auto s = log_(N,j);
        for(auto &e:s)e.second *= -1;
        add_sfps(f,s);
    }
    f.exp_inplace();
    for(int x=1;x<=N;x++)cout<<f[x]<<" ";
    cout<<endl;
    return 0;
}
0