QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#327754#1285. Stirling NumberNaCly_FishAC ✓86ms29024kbC++142.2kb2024-02-15 13:38:492024-02-15 13:38:50

Judging History

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

  • [2024-02-15 13:38:50]
  • 评测
  • 测评结果:AC
  • 用时:86ms
  • 内存:29024kb
  • [2024-02-15 13:38:49]
  • 提交

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];
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;
    inv[p+1] = 1;

}

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;
    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(p);
    printf("%d",(solve(n,r,p)-solve(n,l-1,p)+p*2)%p);
    return 0;   
}

詳細信息

Test #1:

score: 100
Accepted
time: 3ms
memory: 16200kb

input:

4 1 4 5

output:

4

result:

ok "4"

Test #2:

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

input:

6 5 5 29

output:

15

result:

ok "15"

Test #3:

score: 0
Accepted
time: 83ms
memory: 29024kb

input:

1000 685 975 999983

output:

482808

result:

ok "482808"

Test #4:

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

input:

8 7 8 7

output:

1

result:

ok "1"

Test #5:

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

input:

6 4 6 3

output:

2

result:

ok "2"

Test #6:

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

input:

31494923 16387579 27674098 2

output:

0

result:

ok "0"

Test #7:

score: 0
Accepted
time: 14ms
memory: 24672kb

input:

1000000000 179971578 813833436 383101

output:

53093

result:

ok "53093"

Test #8:

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

input:

1000000000 243662537 919841454 336437

output:

75332

result:

ok "75332"

Test #9:

score: 0
Accepted
time: 27ms
memory: 25696kb

input:

1000000000 802415407 880806868 999983

output:

960771

result:

ok "960771"

Test #10:

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

input:

1000000000 644768002 859679621 999983

output:

805072

result:

ok "805072"

Test #11:

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

input:

1000000000 413491669 805704689 999983

output:

138470

result:

ok "138470"

Test #12:

score: 0
Accepted
time: 81ms
memory: 29000kb

input:

48537068788 22847195743 28163317559 999983

output:

529264

result:

ok "529264"

Test #13:

score: 0
Accepted
time: 48ms
memory: 27424kb

input:

828536325370 779765412000 782633091631 999983

output:

836701

result:

ok "836701"

Test #14:

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

input:

258077969836231211 200983000620029238 226661348290193221 999983

output:

104488

result:

ok "104488"

Test #15:

score: 0
Accepted
time: 55ms
memory: 28416kb

input:

258904208719347339 185679775210965354 223112834501603079 999983

output:

0

result:

ok "0"

Test #16:

score: 0
Accepted
time: 27ms
memory: 25508kb

input:

259730451897430763 53367210716367086 159749126385022258 999983

output:

0

result:

ok "0"

Test #17:

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

input:

260556695075514187 28149045695465635 29562653808086859 999983

output:

669091

result:

ok "669091"

Test #18:

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

input:

1000000000000000000 199156813867768126 571262092911942493 919337

output:

732102

result:

ok "732102"

Test #19:

score: 0
Accepted
time: 53ms
memory: 28104kb

input:

353534534534 1999983 2234324324 999983

output:

613376

result:

ok "613376"

Test #20:

score: 0
Accepted
time: 62ms
memory: 28596kb

input:

353534534534 0 2234324324 999983

output:

520965

result:

ok "520965"

Test #21:

score: 0
Accepted
time: 27ms
memory: 27392kb

input:

353534534534 22342343 353534534534 999983

output:

281927

result:

ok "281927"