QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#327749#1285. Stirling NumberNaCly_FishAC ✓884ms27376kbC++142.4kb2024-02-15 13:34:452024-02-15 13:34:45

Judging History

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

  • [2024-02-15 13:34:45]
  • 评测
  • 测评结果:AC
  • 用时:884ms
  • 内存:27376kb
  • [2024-02-15 13:34:45]
  • 提交

answer

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 1000003
#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];
int g;

void init(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;

}

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];
    static int f[N];
    for(int i=0;i<p-1;++i) f[i] = (ll)binom(pw[i]+n-1,n,p)*fac[n]%p;
    int res = (ll)f[0]*(m+1)%p,b = power(g,p-2,p);
    for(int j=1;j<p-1;++j)
        res = (res + (ll)f[j]*(1-power(b,(ll)j*(m+1)%(p-1),p))%p*power(1-power(b,j,p),p-2,p))%p;
    res = (ll)res*(p-1)%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);
    //printf("q = %lld,t = %d,L = %lld\n",q,t,L);
    //printf("fac[%d] = %d\n",r,fac[r]);
    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(p);
    printf("%d",(solve(n,r,p)-solve(n,l-1,p)+p*2)%p);
    return 0;   
}

詳細信息

Test #1:

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

input:

4 1 4 5

output:

4

result:

ok "4"

Test #2:

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

input:

6 5 5 29

output:

15

result:

ok "15"

Test #3:

score: 0
Accepted
time: 883ms
memory: 27296kb

input:

1000 685 975 999983

output:

482808

result:

ok "482808"

Test #4:

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

input:

8 7 8 7

output:

1

result:

ok "1"

Test #5:

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

input:

6 4 6 3

output:

2

result:

ok "2"

Test #6:

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

input:

31494923 16387579 27674098 2

output:

0

result:

ok "0"

Test #7:

score: 0
Accepted
time: 13ms
memory: 11432kb

input:

1000000000 179971578 813833436 383101

output:

53093

result:

ok "53093"

Test #8:

score: 0
Accepted
time: 269ms
memory: 11760kb

input:

1000000000 243662537 919841454 336437

output:

75332

result:

ok "75332"

Test #9:

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

input:

1000000000 802415407 880806868 999983

output:

960771

result:

ok "960771"

Test #10:

score: 0
Accepted
time: 30ms
memory: 23400kb

input:

1000000000 644768002 859679621 999983

output:

805072

result:

ok "805072"

Test #11:

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

input:

1000000000 413491669 805704689 999983

output:

138470

result:

ok "138470"

Test #12:

score: 0
Accepted
time: 877ms
memory: 27228kb

input:

48537068788 22847195743 28163317559 999983

output:

529264

result:

ok "529264"

Test #13:

score: 0
Accepted
time: 445ms
memory: 27372kb

input:

828536325370 779765412000 782633091631 999983

output:

836701

result:

ok "836701"

Test #14:

score: 0
Accepted
time: 447ms
memory: 27372kb

input:

258077969836231211 200983000620029238 226661348290193221 999983

output:

104488

result:

ok "104488"

Test #15:

score: 0
Accepted
time: 459ms
memory: 27128kb

input:

258904208719347339 185679775210965354 223112834501603079 999983

output:

0

result:

ok "0"

Test #16:

score: 0
Accepted
time: 25ms
memory: 23440kb

input:

259730451897430763 53367210716367086 159749126385022258 999983

output:

0

result:

ok "0"

Test #17:

score: 0
Accepted
time: 884ms
memory: 27376kb

input:

260556695075514187 28149045695465635 29562653808086859 999983

output:

669091

result:

ok "669091"

Test #18:

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

input:

1000000000000000000 199156813867768126 571262092911942493 919337

output:

732102

result:

ok "732102"

Test #19:

score: 0
Accepted
time: 451ms
memory: 27220kb

input:

353534534534 1999983 2234324324 999983

output:

613376

result:

ok "613376"

Test #20:

score: 0
Accepted
time: 460ms
memory: 27316kb

input:

353534534534 0 2234324324 999983

output:

520965

result:

ok "520965"

Test #21:

score: 0
Accepted
time: 30ms
memory: 23260kb

input:

353534534534 22342343 353534534534 999983

output:

281927

result:

ok "281927"