QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#490471 | #1285. Stirling Number | hewanying | AC ✓ | 313ms | 44596kb | C++14 | 2.0kb | 2024-07-25 15:20:35 | 2024-07-25 15:20:35 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e6+5;
int L,R,n,q,p,r,m,k,fac[N],ifac[N],f[N],prm[N],ct=0,g,pw[N],ipw[N];
int adc(int a,int b){return a+b>=p?a+b-p:a+b;}
int dec(int a,int b){return a<b?a-b+p:a-b;}
int mul(int a,int b){return 1ll*a*b%p;}
void add(int &a,int b){a=adc(a,b);}
void del(int &a,int b){a=dec(a,b);}
int qpow(int a,int b=p-2){
int res=1;
while(b){if(b&1) res=mul(res,a);a=mul(a,a),b>>=1;}
return res;
}
bool chk(int x){
if(qpow(x,p-1)!=1) return 0;
for(int i=1;i<=ct;i++)
if(qpow(x,(p-1)/prm[i])==1) return 0;
return 1;
}
int findrt(){
for(int i=2;i<p;i++) if(chk(i)) return i;
return -1;
}
void init(){
fac[0]=1;
for(int i=1;i<p;i++) fac[i]=mul(fac[i-1],i);
ifac[p-1]=qpow(fac[p-1]);
for(int i=p-1;i>=1;i--) ifac[i-1]=mul(ifac[i],i);
int x=p-1;
for(int i=2;i*i<=x;i++){
if(x%i==0){
prm[++ct]=i;
while(x%i==0) x/=i;
}
}
if(x!=1) prm[++ct]=x;
g=findrt();
pw[0]=1,f[0]=fac[r];
for(int i=1;i<=p-2;i++)
pw[i]=mul(pw[i-1],g),f[i]=mul(fac[pw[i]+r-1],ifac[pw[i]-1]);
}
int C(int n,int m){
if(m<0||n<m) return 0;
return mul(fac[n],mul(ifac[m],ifac[n-m]));
}
int binom(int n,int m){
if(m<0||n<m) return 0;
if(m==0) return 1;
return mul(C(n%p,m%p),binom(n/p,m/p));
}
int calc(int k){
if(k>=r) return fac[r];
int res=dec(0,mul(k+1,f[0]));
for(int j=1;j<=p-2;j++)
del(res,mul(f[j],mul(dec(pw[(p-1-j)*(k+1)%(p-1)],1),qpow(dec(pw[p-1-j],1)))));
return res;
}
int SOLVE(int M){
if(!M||M<q) return 0;
m=(M-q)/(p-1),k=(M-q)%(p-1);
if(!r){
if((q+m)&1) return dec(0,binom(q-1,m));
return binom(q-1,m);
}
if(!k) k=p-1,--m;
int res=0;
res=dec(mul(binom(q,m),calc(k)),mul(binom(q-1,m-1),fac[r]));
if((q+m)&1) return dec(0,res);
return res;
}
signed main(){
/*2024.7.25 H_W_Y QOJ 1285 Stirling Number*/
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
cin>>n>>L>>R>>p;
q=n/p,r=n%p;init();
cout<<dec(SOLVE(R),SOLVE(L-1));
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11900kb
input:
4 1 4 5
output:
4
result:
ok "4"
Test #2:
score: 0
Accepted
time: 0ms
memory: 11828kb
input:
6 5 5 29
output:
15
result:
ok "15"
Test #3:
score: 0
Accepted
time: 297ms
memory: 42556kb
input:
1000 685 975 999983
output:
482808
result:
ok "482808"
Test #4:
score: 0
Accepted
time: 0ms
memory: 11704kb
input:
8 7 8 7
output:
1
result:
ok "1"
Test #5:
score: 0
Accepted
time: 0ms
memory: 11708kb
input:
6 4 6 3
output:
2
result:
ok "2"
Test #6:
score: 0
Accepted
time: 2ms
memory: 13796kb
input:
31494923 16387579 27674098 2
output:
0
result:
ok "0"
Test #7:
score: 0
Accepted
time: 6ms
memory: 26104kb
input:
1000000000 179971578 813833436 383101
output:
53093
result:
ok "53093"
Test #8:
score: 0
Accepted
time: 87ms
memory: 24108kb
input:
1000000000 243662537 919841454 336437
output:
75332
result:
ok "75332"
Test #9:
score: 0
Accepted
time: 30ms
memory: 42600kb
input:
1000000000 802415407 880806868 999983
output:
960771
result:
ok "960771"
Test #10:
score: 0
Accepted
time: 25ms
memory: 40576kb
input:
1000000000 644768002 859679621 999983
output:
805072
result:
ok "805072"
Test #11:
score: 0
Accepted
time: 23ms
memory: 42492kb
input:
1000000000 413491669 805704689 999983
output:
138470
result:
ok "138470"
Test #12:
score: 0
Accepted
time: 313ms
memory: 42620kb
input:
48537068788 22847195743 28163317559 999983
output:
529264
result:
ok "529264"
Test #13:
score: 0
Accepted
time: 167ms
memory: 42532kb
input:
828536325370 779765412000 782633091631 999983
output:
836701
result:
ok "836701"
Test #14:
score: 0
Accepted
time: 159ms
memory: 42540kb
input:
258077969836231211 200983000620029238 226661348290193221 999983
output:
104488
result:
ok "104488"
Test #15:
score: 0
Accepted
time: 157ms
memory: 42624kb
input:
258904208719347339 185679775210965354 223112834501603079 999983
output:
0
result:
ok "0"
Test #16:
score: 0
Accepted
time: 26ms
memory: 44584kb
input:
259730451897430763 53367210716367086 159749126385022258 999983
output:
0
result:
ok "0"
Test #17:
score: 0
Accepted
time: 306ms
memory: 44596kb
input:
260556695075514187 28149045695465635 29562653808086859 999983
output:
669091
result:
ok "669091"
Test #18:
score: 0
Accepted
time: 24ms
memory: 40504kb
input:
1000000000000000000 199156813867768126 571262092911942493 919337
output:
732102
result:
ok "732102"
Test #19:
score: 0
Accepted
time: 166ms
memory: 44572kb
input:
353534534534 1999983 2234324324 999983
output:
613376
result:
ok "613376"
Test #20:
score: 0
Accepted
time: 163ms
memory: 42544kb
input:
353534534534 0 2234324324 999983
output:
520965
result:
ok "520965"
Test #21:
score: 0
Accepted
time: 26ms
memory: 44512kb
input:
353534534534 22342343 353534534534 999983
output:
281927
result:
ok "281927"