列表

详情


NC14365. Expected Waiting Time

描述

DreamGrid is a famous coach of competitive programming. He is so kind that many competitors are willing to ask him for advice. 

DreamGrid meets exactly n  competitors every day in his office, and thus n "arriving" events and n "selecting" events will happen. An arriving event at time t indicates that a competitor arrives at the waiting room of the office at time , and a selecting event at time indicates that DreamGrid randomly selects (with equal probability) a competitor from the waiting room to talk with. Of course, if the selecting event happens, the waiting room must not be empty. After the talk, the competitor leaves the office and never comes back. 

After several days, DreamGrid starts to be curious about the average of the total expected waiting time of every competitor in all valid cases. The waiting time of a competitor is the time he is selected by DreamGrid minus the time he arrives at the waiting room. A case is a sequence of length 2n consisting of n arriving events andselecting events, where the i -th event will happen at time ai . For a valid case, it must be satisfied that when a selecting event happens, the waiting room must not be empty. 

For example, let's denote an arriving event as 'A', and a selecting event as 'S'. If n = 2,a1 = 1,a2 = 2,a3 = 3,  and a4 = 4, then the sequence "AASS" is valid, but the sequence "ASSA" is not valid, as the "selecting" event happening at time a3 = 3 is not valid. 
As the answer may not be an integer, you are supposed to calculate ab-1 mod p , where ( a and b are coprime) is the answer, p > 2n  and is p prime, and is the modular multiplicative inverse  b of with respect to the modulus p. It's easy to prove that the prime factors of b will never be larger than 2n.

输入描述

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 ,b,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 a= 0, a= ai-1 + bi + 1  and b= (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

说明:

Let's denote the arriving event as 'A', and the selecting event as 'S'.

In the first test case, we have a1 = 1 and a2 = 2,and there is only one valid sequence "AS". The waiting time of the only competitor is 2-1 = 1, so the answer is 1, and we need to output 1 mod 1000000007 = 1.

In the second test case, we have In the second test case, we have ,a= 5 , a= 9 and a= 14.  There are two valid sequences "ASAS" and "AASS".

For the first sequence, the expected waiting time of the first arriving competitor is 5-2 = 3, and the expected waiting time of the second arriving competitor is 14-9 = 5.

For the second sequence, the expected waiting time of the first arriving competitor is ((9-2)+(14-2))/2 = 9.5, and the expected waiting time of the second arriving competitor is ((9-5)+(14-5))/2 = 6.5. So the answer is ((3+5)+(9.5+6.5))/2 = 12, and we need to output 12 mod 1000000007 = 12.

In the third test case, we have a1 = 7,a2 = 9 , a3 = 15 and a4 = 22. Just like the analysis for the 2nd sample test case, the total expected waiting time for the sequence "ASAS" is (9-7)+(22-15) = 9, and the total expected waiting time for the sequence "AASS" is ((15-7)+(22-7))/2+((15-9)+(22-9))/2 = 21. So the answer is (9+21)/2 = 15, and we need to output 15 mod 7 = 1.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

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);
    }
}

上一题