結果

問題 No.829 成長関数インフレ中
ユーザー koprickykopricky
提出日時 2018-12-08 09:35:27
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 786 ms / 2,000 ms
コード長 7,251 bytes
コンパイル時間 2,182 ms
コンパイル使用メモリ 185,160 KB
実行使用メモリ 65,940 KB
最終ジャッジ日時 2024-09-15 13:23:01
合計ジャッジ時間 6,323 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
19,584 KB
testcase_01 AC 18 ms
19,584 KB
testcase_02 AC 17 ms
19,456 KB
testcase_03 AC 14 ms
15,232 KB
testcase_04 AC 17 ms
19,456 KB
testcase_05 AC 17 ms
19,456 KB
testcase_06 AC 17 ms
19,456 KB
testcase_07 AC 17 ms
19,456 KB
testcase_08 AC 17 ms
19,456 KB
testcase_09 AC 17 ms
19,456 KB
testcase_10 AC 17 ms
19,456 KB
testcase_11 AC 17 ms
19,456 KB
testcase_12 AC 18 ms
19,456 KB
testcase_13 AC 18 ms
19,456 KB
testcase_14 AC 17 ms
19,456 KB
testcase_15 AC 159 ms
29,940 KB
testcase_16 AC 319 ms
40,380 KB
testcase_17 AC 528 ms
47,324 KB
testcase_18 AC 720 ms
63,708 KB
testcase_19 AC 599 ms
49,304 KB
testcase_20 AC 786 ms
65,940 KB
testcase_21 AC 17 ms
19,456 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx")
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-9
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto& (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define sar(a,n) cout<<#a<<":";rep(pachico,n)cout<<" "<<a[pachico];cout<<endl
#define svec(v) cout<<#v<<":";rep(pachico,v.size())cout<<" "<<v[pachico];cout<<endl
#define svecp(v) cout<<#v<<":";each(pachico,v)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define sset(s) cout<<#s<<":";each(pachico,s)cout<<" "<<pachico;cout<<endl
#define smap(m) cout<<#m<<":";each(pachico,m)cout<<" {"<<pachico.first<<":"<<pachico.second<<"}";cout<<endl
#define pw(x) (1LL<<(x))

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;
typedef double dbl;
typedef pair<int, int> pi;

const int MAX_N = 200000;

#define getchar getchar_unlocked

inline int in() {
    int n = 0; short c;
    while ((c = getchar()) >= '0') n = n * 10 + c - '0';
    return n;
}

int inv[MAX_N],fac[MAX_N],finv[MAX_N];

void make()
{
	fac[0] = fac[1] = 1;
	finv[0] = finv[1] = 1;
	inv[1] = 1;
	for(int i=2;i<MAX_N;i++){
		inv[i] = MOD - (ll)inv[MOD%i] * (MOD/i) % MOD;
		fac[i] = (ll)fac[i-1] * i % MOD;
		finv[i] = (ll)finv[i-1] * inv[i] % MOD;
	}
}

inline int prod(int a, int b)
{
	return (a < b) ? 0 : ((ll)fac[a] * finv[a-b] % MOD);
}

void extgcd(int a,int b, int& x,int& y)
{
	if(b){
		extgcd(b, a%b, y, x);
		y -= (a/b)*x;
	}else{
		x = 1, y = 0;
	}
}

int mod_inverse(int a, int m)
{
	int x, y;
	extgcd(a,m,x,y);
	return (m + x % m) % m;
}

namespace fft
{
    const int maxBase = 18;
    const int maxN = 1 << maxBase;

    struct num
    {
        dbl x, y;
        num(){}
        num(dbl xx, dbl yy): x(xx), y(yy) {}
        num(dbl alp): x(cos(alp)), y(sin(alp)) {}
    };

    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); }

    const dbl PI = acos(-1);

    num root[maxN];
    int rev[maxN];
    bool rootsPrepared = false;

    void prepRoots()
    {
        if (rootsPrepared) return;
        rootsPrepared = true;
        root[1] = num(1, 0);
        for (int k = 1; k < maxBase; ++k)
        {
            num x(2 * PI / pw(k + 1));
            for (int i = pw(k - 1); i < pw(k); ++i)
            {
                root[2 * i] = root[i];
                root[2 * i + 1] = root[i] * x;
            }
        } 
    }    

    int base, N;

    int lastRevN = -1;
    void prepRev()
    {
        if (lastRevN == N) return;
        lastRevN = N;
        rep(i, N) rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (base - 1));
    }

    void fft(num *a, num *f)
    {
        rep(i, N) f[i] = a[rev[i]];
        for (int k = 1; k < N; k <<= 1) for (int i = 0; i < N; i += 2 * k) rep(j, k)
        {
            num z = f[i + j + k] * root[j + k];
            f[i + j + k] = f[i + j] - z;
            f[i + j] = f[i + j] + z;
        }
    }

    num a[maxN], b[maxN], f[maxN], g[maxN];
    ll A[maxN], B[maxN], C[maxN];

    void _multMod(int mod)
    {
        rep(i, N)
        {
            int x = A[i] % mod;
            a[i] = num(x & (pw(15) - 1), x >> 15);
        }
        rep(i, N)
        {
            int x = B[i] % mod;
            b[i] = num(x & (pw(15) - 1), x >> 15);
        }
        fft(a, f);
        fft(b, g);

        rep(i, N)
        {
            int j = (N - i) & (N - 1);
            num a1 = (f[i] + conj(f[j])) * num(0.5, 0);
            num a2 = (f[i] - conj(f[j])) * num(0, -0.5);
            num b1 = (g[i] + conj(g[j])) * num(0.5 / N, 0);
            num b2 = (g[i] - conj(g[j])) * num(0, -0.5 / N);
            a[j] = a1 * b1 + a2 * b2 * num(0, 1);
            b[j] = a1 * b2 + a2 * b1;
        }
        
        fft(a, f);
        fft(b, g);

        rep(i, N)
        {
            ll aa = f[i].x + 0.5;
            ll bb = g[i].x + 0.5;
            ll cc = f[i].y + 0.5;
            C[i] = (aa + bb % mod * pw(15) + cc % mod * pw(30)) % mod;
        }
    }

    void prepAB(int n1, int n2)
    {
        base = 1;
        N = 2;
        while (N < n1 + n2) base++, N <<= 1;
        assert(base <= maxBase);

        for (int i = n1; i < N; ++i) A[i] = 0;
        for (int i = n2; i < N; ++i) B[i] = 0;

        prepRoots();
        prepRev();
    }

    void multMod(int n1, int n2, int mod)
    {
        prepAB(n1, n2);
        _multMod(mod);
    }
}

struct poly
{
    vi v;
    poly() {}
    poly(vi vv)
    {
        v = vv;
    }
    int size()
    {
        return (int)v.size();
    }
    poly cut(int maxLen)
    {
        if (maxLen < len(v)) v.resize(maxLen);
        return *this;
    }
    poly norm()
    {
        while (len(v) > 1 && v.back() == 0) v.pop_back();
        return *this;
    }
    inline int& operator [] (int i)
    {
        return v[i];
    }         
};

poly operator + (poly A, poly B)
{
    poly C;
    C.v = vi(max(len(A), len(B)));
    rep(i, len(C))
    {
        if (i < len(A)) C[i] = (C[i] + A[i]) % MOD;
        if (i < len(B)) C[i] = (C[i] + B[i]) % MOD;
    }
    return C.norm();
}

poly operator - (poly A, poly B)
{
    poly C;
    C.v = vi(max(len(A), len(B)));
    rep(i, len(C))
    {
        if (i < len(A)) C[i] = (C[i] + A[i]) % MOD;
        if (i < len(B)) C[i] = (C[i] + MOD - B[i]) % MOD;
    }
    return C.norm();
}

poly operator * (poly A, poly B)
{
    poly C;
    C.v = vi(len(A) + len(B) - 1);

    rep(i, len(A)) fft::A[i] = A[i];
    rep(i, len(B)) fft::B[i] = B[i];
    fft::multMod(len(A), len(B), MOD);
    rep(i, len(C)) C[i] = fft::C[i];
    return C.norm();
}

poly c[2*MAX_N];
int cnt[MAX_N];

int main()
{
    int n = in(), B = in();
    rep(i,n){
        cnt[in()]++;
    }
    make();
    int id = 0, num = 0;
    priority_queue<P, vector<P>, greater<P> > que;
    rrep(i,MAX_N){
        if(!cnt[i]) continue;
        c[num] = (vi){prod(id+cnt[i]-1, cnt[i]) % MOD, (int)((ll)prod(id+cnt[i]-1, cnt[i]-1) * cnt[i] % MOD)};
        que.push(P(2, num++));
        id += cnt[i];
    }
    while(len(que) >= 2){
        int p = que.top().se; que.pop();
        int q = que.top().se; que.pop();
        c[num] = c[p] * c[q];
        que.push(P(len(c[num]), num));
        num++;
    }
    int index = que.top().se;
    int ans = 0, nB = 1;
    rep(i, len(c[index])){
        ans = (ans + (((ll)c[index][i] * i) % MOD) * nB) % MOD;
        nB = (ll)nB * B % MOD;
    }
    cout << ans << "\n";
    return 0;
}
0