QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#327766#1285. Stirling NumberNaCly_FishAC ✓54ms29316kbC++142.2kb2024-02-15 13:46:252024-02-15 13:46:25

Judging History

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

  • [2024-02-15 13:46:25]
  • 评测
  • 测评结果:AC
  • 用时:54ms
  • 内存:29316kb
  • [2024-02-15 13:46:25]
  • 提交

answer

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 1000005
#define ll long long
using namespace std;

inline int power(int a,int t,const int& p){
    int res = 1;
    while(t){
        if(t&1) res = (ll)res*a%p;
        a = (ll)a*a%p;
        t >>= 1;
    }
    return res;
}

int findrt(const int& x){
    static int fac[N];
    int cnt = 0,m = x-1;
    for(int i=2;i*i<=m;++i){
        if(m%i!=0) continue;
        fac[++cnt] = i;
        while(m%i==0) m /= i;
    }
    if(m>1) fac[++cnt] = m;
    for(int i=2;i<=x;++i){
        bool flag = true;
        for(int j=1;j<=cnt;++j){
            if(power(i,(x-1)/fac[j],x)!=1) continue;
            flag = false;
            break;
        }
        if(flag) return i;
    }
    return -1;
}

int fac[N],ifac[N],pw[N],lg[N],inv[N],f[N];
int g;

void init(int n,const int& p){
    pw[0] = inv[1] = fac[0] = fac[1] = ifac[0] = ifac[1] = 1;
    for(int i=2;i<p;++i) fac[i] = (ll)fac[i-1]*i%p;
    ifac[p-1] = p-1;
    for(int i=p-2;i;--i) ifac[i] = (ll)ifac[i+1]*(i+1)%p;
    for(int i=2;i<p;++i) inv[i] = (ll)fac[i-1]*ifac[i]%p;
    for(int i=1;i<p-1;++i) pw[i] = (ll)pw[i-1]*g%p;
    for(int i=0;i<p-1;++i) lg[pw[i]] = i;
    inv[p+1] = 1;
    for(int i=0;i<p-1;++i) f[i] = (pw[i]+n-1>=p)?0:(ll)fac[pw[i]+n-1]*ifac[pw[i]-1]%p;

}

int binom(ll n,ll m,const int& p){
    if(n<m) return 0;
    if(n>=p) return (ll)binom(n/p,m/p,p)*binom(n%p,m%p,p)%p;
    return (ll)fac[n]*ifac[m]%p*ifac[n-m]%p;
}

int sum(int n,int m,const int& p){ // [x^m] of x^{\overline n}/(1-x)
    if(n==0) return 1;
    if(m>=n) return fac[n];
    int res = (ll)f[0]*(m+1)%p;
    for(int j=1;j<p-1;++j)
        res = (res + (ll)f[j]*(1-pw[p-1-(ll)j*(m+1)%(p-1)])%p*inv[p+1-pw[p-1-j]])%p;
    return -res;
}

int solve(ll n,ll m,const int& p){
    ll L,q = n/p;
    int t,r = n%p;
    if(m<q) return 0;
    m -= q;
    t = m%(p-1),L = m/(p-1);
    int res = ((ll)sum(r,t,p)*binom(q,L,p) - (ll)fac[r]*(L==0?0:binom(q-1,L-1,p)))%p;
    return ((q-L)&1)?-res:res;
}

ll n,l,r;
int p;

int main(){
    scanf("%lld%lld%lld%d",&n,&l,&r,&p);
    g = findrt(p);
    init(n%p,p);
    printf("%d",(solve(n,r,p)-solve(n,l-1,p)+p*2)%p);
    return 0;   
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 6008kb

input:

4 1 4 5

output:

4

result:

ok "4"

Test #2:

score: 0
Accepted
time: 1ms
memory: 5944kb

input:

6 5 5 29

output:

15

result:

ok "15"

Test #3:

score: 0
Accepted
time: 54ms
memory: 28240kb

input:

1000 685 975 999983

output:

482808

result:

ok "482808"

Test #4:

score: 0
Accepted
time: 1ms
memory: 5796kb

input:

8 7 8 7

output:

1

result:

ok "1"

Test #5:

score: 0
Accepted
time: 1ms
memory: 5800kb

input:

6 4 6 3

output:

2

result:

ok "2"

Test #6:

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

input:

31494923 16387579 27674098 2

output:

0

result:

ok "0"

Test #7:

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

input:

1000000000 179971578 813833436 383101

output:

53093

result:

ok "53093"

Test #8:

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

input:

1000000000 243662537 919841454 336437

output:

75332

result:

ok "75332"

Test #9:

score: 0
Accepted
time: 36ms
memory: 28908kb

input:

1000000000 802415407 880806868 999983

output:

960771

result:

ok "960771"

Test #10:

score: 0
Accepted
time: 36ms
memory: 28644kb

input:

1000000000 644768002 859679621 999983

output:

805072

result:

ok "805072"

Test #11:

score: 0
Accepted
time: 38ms
memory: 28696kb

input:

1000000000 413491669 805704689 999983

output:

138470

result:

ok "138470"

Test #12:

score: 0
Accepted
time: 50ms
memory: 28756kb

input:

48537068788 22847195743 28163317559 999983

output:

529264

result:

ok "529264"

Test #13:

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

input:

828536325370 779765412000 782633091631 999983

output:

836701

result:

ok "836701"

Test #14:

score: 0
Accepted
time: 43ms
memory: 28848kb

input:

258077969836231211 200983000620029238 226661348290193221 999983

output:

104488

result:

ok "104488"

Test #15:

score: 0
Accepted
time: 35ms
memory: 29316kb

input:

258904208719347339 185679775210965354 223112834501603079 999983

output:

0

result:

ok "0"

Test #16:

score: 0
Accepted
time: 32ms
memory: 27920kb

input:

259730451897430763 53367210716367086 159749126385022258 999983

output:

0

result:

ok "0"

Test #17:

score: 0
Accepted
time: 46ms
memory: 28392kb

input:

260556695075514187 28149045695465635 29562653808086859 999983

output:

669091

result:

ok "669091"

Test #18:

score: 0
Accepted
time: 17ms
memory: 25980kb

input:

1000000000000000000 199156813867768126 571262092911942493 919337

output:

732102

result:

ok "732102"

Test #19:

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

input:

353534534534 1999983 2234324324 999983

output:

613376

result:

ok "613376"

Test #20:

score: 0
Accepted
time: 51ms
memory: 27960kb

input:

353534534534 0 2234324324 999983

output:

520965

result:

ok "520965"

Test #21:

score: 0
Accepted
time: 34ms
memory: 28316kb

input:

353534534534 22342343 353534534534 999983

output:

281927

result:

ok "281927"