QOJ.ac
QOJ
The 2nd Universal Cup Finals is coming! Check out our event page, schedule, and competition rules!
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#671189 | #9451. Expected Waiting Time | maojun | TL | 0ms | 5984kb | C++23 | 836b | 2024-10-24 11:33:00 | 2024-10-24 11:33:01 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e6+5;
int n,p;
ll F[N],I[N];
inline ll ksm(ll x){ll r=1;for(int y=p-2;y;y>>=1,x=x*x%p)if(y&1)r=r*x%p;return r;}
inline ll C(int n,int m){return m<0||n<m?0:F[n]*I[m]%p*I[n-m]%p;}
inline void slv(){
ll a0=0,b0,A,B;
scanf("%d%d%lld%lld%lld",&n,&p,&b0,&A,&B);
F[0]=1;for(int i=1;i<=n*2;i++)F[i]=F[i-1]*i%p;
I[n*2]=ksm(F[n*2]);for(int i=n*2-1;~i;i--)I[i]=I[i+1]*(i+1)%p;
ll rs=0,tt=F[n*2]*I[n]%p*I[n+1]%p;
for(int i=1;i<=n*2;i++){
b0=(A*b0+B)%p;a0=(a0+b0+1)%p;
ll x=0;
for(int j=i-1;j>=0;j-=2)x=(x+(C(i-1,i+j-1>>1)-C(i-1,i+j+1>>1)+p)*(C(n*2-i,n*2-i+j+1>>1)-C(n*2-i,n*2-i+j+3>>1)+p))%p;
rs=(rs+x*(p-a0)+(tt-x+p)*a0)%p;
}
printf("%lld\n",rs*ksm(tt)%p);
}
int main(){
int T;scanf("%d",&T);while(T--)slv();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5984kb
input:
5 1 1000000007 0 1 0 2 1000000007 0 1 1 2 7 5 2 3 3 31 15 6 24 20 1000000007 0 1 0
output:
1 12 1 21 879705565
result:
ok 5 number(s): "1 12 1 21 879705565"
Test #2:
score: -100
Time Limit Exceeded
input:
4400 3954 1000000007 0 1 0 1306 1000000007 0 1 0 3774 1000000007 0 1 0 3345 1000000007 0 1 0 891 1000000007 0 1 0 2462 1000000007 0 1 0 237 1000000007 0 1 0 26 1000000007 0 1 0 2510 1000000007 0 1 0 637 1000000007 0 1 0 3250 1000000007 0 1 0 3447 1000000007 0 1 0 1222 1000000007 0 1 0 133 1000000007...