NC14365. Expected Waiting Time
描述
输入描述
There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 104), indicating the number of test cases. For each test case:The first linecontains five integersn, p ,b0 ,A ,B (1 ≤ n ≤ 106,2n < p ≤ 2×109, 0 ≤ b0, A ,B < p), where p is aprime number. The meanings ofn andp are described above. The rest of them is agenerator of a, where a0 = 0, ai = ai-1 + bi + 1 and bi = (A ⋅ bi−1 + B) mod p for all 1 ≤ i ≤ 2n.It is guaranteed that the sum of n in all test cases does not exceed 107.
输出描述
For each test case, output one integer denoting the answer in a single line.
示例1
输入:
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
输出:
1 12 1 21 879705565
说明:
C++ 解法, 执行用时: 1473ms, 内存消耗: 15980K, 提交时间: 2021-08-04 16:24:18
#include<bits/stdc++.h> typedef long long ll; const int N=2e6+7; int n,P,b,A,B,T,i,a[N],sum,ans; int h[N],inv[N]; inline ll po(ll a,ll b){ ll t=1; for(;b;b>>=1,a=a*a%P)if(b&1)t=t*a%P; return t; } inline int C(int n){ if(n&1)return 0; return h[n>>1]; } int main(){ scanf("%d",&T); while(T--){ scanf("%d%d%d%d%d",&n,&P,&b,&A,&B); h[0]=1; h[1]=1; inv[0]=inv[1]=1; for(i=2;i<=n+1;i++)inv[i]=1LL*(P-inv[P%i])*(P/i)%P; for(i=2;i<=n;i++)h[i]=1LL*h[i-1]*(i*4-2)%P*inv[i+1]%P; n*=2; for(i=1;i<=n;i++){ b=(1LL*b*A+B)%P; a[i]=(1LL*a[i-1]+1LL*b+1)%P; } sum=0; ans=0; for(i=1;i<n;i++){ sum=(1LL*C(i-1)*C(n-i-1)+sum)%P; ans=(1LL*(P-a[n-i])*sum+ans)%P; ans=(1LL*a[i+1]*sum+ans)%P; } printf("%d\n",1LL*ans*po(C(n),P-2)%P); } }