QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#326014#8211. Enumerating Substringsucup-team134AC ✓40ms19872kbC++145.6kb2024-02-12 07:29:062024-02-12 07:29:06

Judging History

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

  • [2024-02-12 07:29:06]
  • 评测
  • 测评结果:AC
  • 用时:40ms
  • 内存:19872kb
  • [2024-02-12 07:29:06]
  • 提交

answer

#include<bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
typedef long long ll;
using namespace std;
typedef pair<int,int> pii;

const int mod=1e9+7;
inline int add(int x,int y){int ret=x+y;if(ret>=mod)ret-=mod;return ret;}
inline int sub(int x,int y){int ret=x-y;if(ret<0)ret+=mod;return ret;}
inline int mul(int x,int y){return ((ll)x*y)%mod;}
inline int step(int base,int pw){int ret=1;while(pw){if(pw&1)ret=mul(ret,base);base=mul(base,base);pw>>=1;}return ret;}
inline int invv(int x){return step(x,mod-2);}

namespace polynomial{

    int INF=1e9;

    struct polyn{

        vector<int>a;

        polyn(){}
        polyn(vector<int>b){a=b;}

        void push_back(int x){
            a.pb(x);
        }
        int size(){return a.size();}
        void resize(int n){a.resize(n);}

        int& operator [](int x){
            if(x>=a.size())a.resize(x+1);
            return a[x];
        }

        polyn operator -(polyn b){
            polyn ret;
            ret.resize(max(b.size(),(int)a.size()));
            for(int i=0;i<a.size();i++)ret[i]=add(ret[i],a[i]);
            for(int i=0;i<b.size();i++)ret[i]=sub(ret[i],b[i]);
            return ret;
        }

        polyn operator +(polyn b){
            polyn ret;
            ret.resize(max(b.size(),(int)a.size()));
            for(int i=0;i<a.size();i++)ret[i]=add(ret[i],a[i]);
            for(int i=0;i<b.size();i++)ret[i]=add(ret[i],b[i]);
            return ret;
        }

        polyn operator *(int c){
            polyn ret=(*this);
            for(int i=0;i<ret.size();i++)ret[i]=mul(ret[i],c);
            return ret;
        }

        friend polyn operator *(const int c,polyn p){
            return p*c;
        }

        polyn brute_pmul(polyn b){
            polyn ret;
            ret.resize(a.size()+b.size()-1);
            for(int i=0;i<a.size();i++)
                for(int j=0;j<b.size();j++)
                    ret[i+j]=add(ret[i+j],mul(a[i],b[j]));
            return ret;
        }

        polyn operator *(polyn b){
            return brute_pmul(b);
        }

        polyn mod_xk(int n){
            polyn ret(a);
            ret.resize(n);
            return ret;
        }

        polyn neg(){
            polyn ret=(*this);
            for(int i=1;i<ret.size();i+=2)ret[i]=sub(0,ret[i]);
            return ret;
        }


        void ispis(){
            for(int i=0;i<a.size();i++)printf("%d ",a[i]);
            printf("POLYN ispis\n");
        }

    };

}

using namespace polynomial;

const int maxn=3e5+10;

int k;
polyn get_Px(int p,int c,int d,int r){

    int f0=step(k,p);
    int f1=sub(step(k,p+r),f0);

    polyn ret;
    ret[2]=sub(d,mul(c,f1));
    ret[1]=sub(f1,mul(c,f0));
    ret[0]=f0;

    return ret;
}
polyn get_Qx(int c){

    polyn ret,p1,p2;
    p1[0]=1;
    p1[1]=sub(0,1);

    p2[0]=1;
    p2[1]=1;

    p2=p1*p2;

    p1[1]=sub(0,c);
    ret=p1*p2;

    return ret;
}
polyn section(polyn p,int par){

    polyn ret;

    int curr=0;
    for(int i=par;i<p.size();i+=2)
        ret[curr++]=p[i];

    return ret;
}
int f_ex(int p,int i,int r){

    if(i<0)return 0;

    int c=step(k,r);
    int d=sub( step(k,p+2*r) , step(k,p+r));

    polyn P=get_Px(p,c,d,r);
    polyn Q=get_Qx(c);


    /// i-th term
    while(i){

        polyn gore=section(P*Q.neg(),i%2);
        polyn dole=section(Q*Q.neg(),0);

        P=gore;
        Q=dole;
        i/=2;
    }

    return mul(P[0],invv(Q[0]));
}
int f_ex_k(int p,int i,int r){

    if(i<0)return 0;

    int c=step(k,r);
    int d=sub( step(k,p+2*r) , step(k,p+r));

    polyn P=get_Px(p,c,d,r);
    polyn Q=get_Qx(c);
    polyn ppp;
    ppp[0]=1;
    ppp[1]=sub(0,c);
    Q=Q*ppp;


    /// i-th term
    while(i){

        polyn gore=section(P*Q.neg(),i%2);
        polyn dole=section(Q*Q.neg(),0);

        P=gore;
        Q=dole;
        i/=2;
    }

    return mul(P[0],invv(Q[0]));
}
int fksum(int n,int r){/// gets the sum k^n-i * f(i)

    if(n<0)return 0;

    int d=n/r;
    int d2=n%r;

    int ret=mul(f_ex_k(0,d-1,r),step(k,n-(d-1)*r));
    ret=mul(ret,r);

    int pom=mul(f_ex(0,d,r),step(k,n-d*r));
    ret=add(ret,mul(pom,add(d2,1)));

    return ret;
}

const int maxm=2010;
int dp[maxm][maxm],fact[maxm],inv[maxm],n,m;

int count_beautiful(int m,int k){

    if(m<0)return 0;

    int up=1;
    int ret=dp[m][0];
    for(int i=1;i<=m;i++){

        up=mul(up,k);
        k--;

        ret=add(ret,mul(dp[m][i],mul(mul(up,inv[i]),fact[i]) ) );
    }

    return ret;
}

int calc_singles(){
    return mul(mul(step(k,n-m),n-m+1),count_beautiful(m,k));
}

int main(){

    ///freopen("test.txt","r",stdin);

    scanf("%d %d %d",&n,&m,&k);

    dp[0][0]=1;
    for(int i=0;i<maxm;i++)
    for(int j=0;j<maxm;j++){
        if(i==0 && j==0)continue;

        if(i-1>=0){
            if(2*j-(i-1)>=0)dp[i][j]=add(dp[i][j],mul( (2*j-(i-1) ),dp[i-1][j] ));
            if(j-1>=0)dp[i][j]=add(dp[i][j],dp[i-1][j-1]);
        }
    }

    fact[0]=1;
    for(int i=1;i<maxm;i++)fact[i]=mul(fact[i-1],i);
    inv[maxm-1]=invv(fact[maxm-1]);
    for(int i=maxm-1;i>0;i--)inv[i-1]=mul(inv[i],i);

    int ret=calc_singles();

    int up=1;
    int kk=k;
    for(int r=1;r<m;r++){
        up=mul(up,kk);
        kk--;

        int real_r=m-r;
        ret=sub(ret, mul(fact[r],mul( inv[r] , mul(up,mul( count_beautiful(m-2*r,k-r) , fksum(n-m-real_r,real_r) ) ) )) );
    }
    printf("%d\n",ret);

    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 13ms
memory: 19852kb

input:

4 2 3

output:

228

result:

ok 1 number(s): "228"

Test #2:

score: 0
Accepted
time: 40ms
memory: 19596kb

input:

999999 1999 12345678

output:

52352722

result:

ok 1 number(s): "52352722"

Test #3:

score: 0
Accepted
time: 8ms
memory: 19648kb

input:

7 4 2

output:

182

result:

ok 1 number(s): "182"

Test #4:

score: 0
Accepted
time: 12ms
memory: 19540kb

input:

4 3 4

output:

480

result:

ok 1 number(s): "480"

Test #5:

score: 0
Accepted
time: 8ms
memory: 19588kb

input:

3 1 1

output:

3

result:

ok 1 number(s): "3"

Test #6:

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

input:

5 5 1

output:

0

result:

ok 1 number(s): "0"

Test #7:

score: 0
Accepted
time: 8ms
memory: 19576kb

input:

7 4 3

output:

5784

result:

ok 1 number(s): "5784"

Test #8:

score: 0
Accepted
time: 20ms
memory: 19576kb

input:

5 2 4

output:

3932

result:

ok 1 number(s): "3932"

Test #9:

score: 0
Accepted
time: 20ms
memory: 19556kb

input:

8 2 2

output:

1522

result:

ok 1 number(s): "1522"

Test #10:

score: 0
Accepted
time: 12ms
memory: 19588kb

input:

8 1 2

output:

2048

result:

ok 1 number(s): "2048"

Test #11:

score: 0
Accepted
time: 6ms
memory: 19652kb

input:

7 5 3

output:

2430

result:

ok 1 number(s): "2430"

Test #12:

score: 0
Accepted
time: 16ms
memory: 19584kb

input:

10 4 3

output:

272004

result:

ok 1 number(s): "272004"

Test #13:

score: 0
Accepted
time: 15ms
memory: 19848kb

input:

675978 614 2

output:

0

result:

ok 1 number(s): "0"

Test #14:

score: 0
Accepted
time: 8ms
memory: 19648kb

input:

244613 38 1

output:

0

result:

ok 1 number(s): "0"

Test #15:

score: 0
Accepted
time: 26ms
memory: 19576kb

input:

186293 1462 1

output:

0

result:

ok 1 number(s): "0"

Test #16:

score: 0
Accepted
time: 20ms
memory: 19656kb

input:

24867 886 1

output:

0

result:

ok 1 number(s): "0"

Test #17:

score: 0
Accepted
time: 21ms
memory: 19588kb

input:

976164 1014 2

output:

0

result:

ok 1 number(s): "0"

Test #18:

score: 0
Accepted
time: 8ms
memory: 19568kb

input:

179356 2 716844809

output:

577866092

result:

ok 1 number(s): "577866092"

Test #19:

score: 0
Accepted
time: 21ms
memory: 19572kb

input:

621001 130 310625363

output:

892869197

result:

ok 1 number(s): "892869197"

Test #20:

score: 0
Accepted
time: 18ms
memory: 19596kb

input:

678862 850 754662812

output:

582264789

result:

ok 1 number(s): "582264789"

Test #21:

score: 0
Accepted
time: 22ms
memory: 19540kb

input:

650845 978 348443366

output:

825425732

result:

ok 1 number(s): "825425732"

Test #22:

score: 0
Accepted
time: 8ms
memory: 19584kb

input:

669914 402 87448112

output:

318098088

result:

ok 1 number(s): "318098088"

Test #23:

score: 0
Accepted
time: 11ms
memory: 19872kb

input:

998593 530 681228665

output:

408255654

result:

ok 1 number(s): "408255654"

Test #24:

score: 0
Accepted
time: 37ms
memory: 19540kb

input:

369361 1954 125266115

output:

509912384

result:

ok 1 number(s): "509912384"

Test #25:

score: 0
Accepted
time: 23ms
memory: 19800kb

input:

900226 1378 424079373

output:

406320917

result:

ok 1 number(s): "406320917"

Test #26:

score: 0
Accepted
time: 28ms
memory: 19652kb

input:

334887 1506 17859926

output:

503264679

result:

ok 1 number(s): "503264679"

Test #27:

score: 0
Accepted
time: 21ms
memory: 19648kb

input:

936048 544 53978328

output:

548647866

result:

ok 1 number(s): "548647866"

Test #28:

score: 0
Accepted
time: 21ms
memory: 19592kb

input:

152789 1264 792983073

output:

839541707

result:

ok 1 number(s): "839541707"

Test #29:

score: 0
Accepted
time: 31ms
memory: 19812kb

input:

714493 1392 91796331

output:

721071046

result:

ok 1 number(s): "721071046"

Test #30:

score: 0
Accepted
time: 22ms
memory: 19588kb

input:

269571 816 830801077

output:

330064211

result:

ok 1 number(s): "330064211"

Test #31:

score: 0
Accepted
time: 19ms
memory: 19620kb

input:

845120 944 424581630

output:

348960190

result:

ok 1 number(s): "348960190"

Test #32:

score: 0
Accepted
time: 8ms
memory: 19864kb

input:

533990 368 163586376

output:

522092095

result:

ok 1 number(s): "522092095"

Test #33:

score: 0
Accepted
time: 29ms
memory: 19652kb

input:

181707 1792 462399634

output:

373795106

result:

ok 1 number(s): "373795106"

Test #34:

score: 0
Accepted
time: 37ms
memory: 19584kb

input:

417349 1920 761212891

output:

587051329

result:

ok 1 number(s): "587051329"

Test #35:

score: 0
Accepted
time: 22ms
memory: 19576kb

input:

526583 1344 500217637

output:

108767800

result:

ok 1 number(s): "108767800"

Test #36:

score: 0
Accepted
time: 24ms
memory: 19576kb

input:

867054 769 93998191

output:

239123369

result:

ok 1 number(s): "239123369"

Extra Test:

score: 0
Extra Test Passed