QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#385128#8329. Excusetriple_dogs#AC ✓230ms22340kbC++144.5kb2024-04-10 15:50:452024-04-10 15:50:46

Judging History

你现在查看的是最新测评结果

  • [2024-04-10 15:50:46]
  • 评测
  • 测评结果:AC
  • 用时:230ms
  • 内存:22340kb
  • [2024-04-10 15:50:45]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int M=998244353;
namespace fft{
    struct num{
        double x,y;
        num() {x=y=0;}
        num(double x,double 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 double 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){
            double angle=2*PI/(1<<(base+1));
            for (int i=1<<(base-1);i<(1<<base);i++){
                roots[i<<1]=roots[i];
                double 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> multiply_mod(vector<int> &a,vector<int> &b,int m,int eq=0){
        int need=a.size()+b.size()-1;
        int nbase=0;
        while ((1<<nbase)<need) nbase++;
        ensure_base(nbase);
        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]%m+m)%m;
            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]%m+m)%m;
                fb[i]=num(x&((1<<15)-1),x>>15);
            }
            fill(fb.begin()+b.size(),fb.begin()+sz,num{0,0});
            fft(fb,sz);
        }
        double ratio=0.25/sz;
        num r2(0,-1),r3(ratio,0),r4(0,-ratio),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++){
            ll aa=fa[i].x+0.5;
            ll bb=fb[i].x+0.5;
            ll cc=fa[i].y+0.5;
            res[i]=(aa+((bb%m)<<15)+((cc%m)<<30))%m;
        }
        return res;
    }
}
const int maxn=1e5+10;
int f[maxn],nf[maxn],p2[maxn],inv_p2[maxn],inv[maxn];
int dp[maxn];
void add(int &x,int y){x+=y;if (x>=M)x-=M;}
void init(){
    inv[1]=1; for (int i=2;i<maxn;i++) inv[i]=M-1ll*(M/i)*inv[M%i]%M;
    nf[0]=f[0]=1; for (int i=1;i<maxn;i++) f[i]=1ll*f[i-1]*i%M,nf[i]=1ll*nf[i-1]*inv[i]%M;
    p2[0]=inv_p2[0]=1; for (int i=1;i<maxn;i++) p2[i]=1ll*p2[i-1]*2%M,inv_p2[i]=1ll*inv_p2[i-1]*inv[2]%M;
}
void solve(int l,int r){
    if (l==r){
        dp[l]=(1ll*dp[l]*f[l]+p2[l]-1)%M*inv_p2[l]%M;
        return;
    }
    int mid=(l+r)>>1;
    solve(l,mid);
    vi A(mid-l+1),B(r-l+1);
    for (int i=l;i<=mid;i++) A[i-l]=1ll*dp[i]*nf[i]%M;
    for (int i=0;i<=r-l;i++) B[i]=nf[i];
    vi C=fft::multiply_mod(A,B,M);
    for (int i=mid+1;i<=r;i++) add(dp[i],C[i-l]);
    solve(mid+1,r);
}
int main(){
    int n; cin >> n;
    init(); solve(0,n);
    cout << dp[n] << endl;
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 5756kb

input:

1

output:

499122177

result:

ok 1 number(s): "499122177"

Test #2:

score: 0
Accepted
time: 0ms
memory: 5792kb

input:

3

output:

561512450

result:

ok 1 number(s): "561512450"

Test #3:

score: 0
Accepted
time: 2ms
memory: 5752kb

input:

10

output:

609769250

result:

ok 1 number(s): "609769250"

Test #4:

score: 0
Accepted
time: 0ms
memory: 6000kb

input:

1000

output:

475714976

result:

ok 1 number(s): "475714976"

Test #5:

score: 0
Accepted
time: 3ms
memory: 6196kb

input:

1024

output:

178624793

result:

ok 1 number(s): "178624793"

Test #6:

score: 0
Accepted
time: 227ms
memory: 22268kb

input:

100000

output:

537516197

result:

ok 1 number(s): "537516197"

Test #7:

score: 0
Accepted
time: 230ms
memory: 22340kb

input:

99471

output:

489775976

result:

ok 1 number(s): "489775976"

Test #8:

score: 0
Accepted
time: 112ms
memory: 13984kb

input:

65536

output:

171446457

result:

ok 1 number(s): "171446457"

Test #9:

score: 0
Accepted
time: 120ms
memory: 14180kb

input:

84792

output:

371578800

result:

ok 1 number(s): "371578800"

Test #10:

score: 0
Accepted
time: 222ms
memory: 22148kb

input:

93846

output:

841905002

result:

ok 1 number(s): "841905002"

Extra Test:

score: 0
Extra Test Passed