hdu 6239 前三次方求和

Alice and Bob are going to Tenbaba for an interview. There are totally N candidates(including Alice and Bob) applying for this job. The recruitment process of Tenbaba is strict. So all of them need to have an interview with the department manager. N candidates will get a unique integer from 1 to N with equal probability. And they will go to interview with the manager according to the number they get. The candidate with number 1 will be interviewed firstly, the candidate with number 2 will be interviewed secondly and so on.

What’s more, HR(the staff to arrange the interview for you) will randomly choose a nonnegative integer K in range [0..N] with equal probability. The candidates whose numbers are not greater than K will be interviewed in order of their numbers on the first day. Remaining candidates will be interviewed in order of their numbers on the second day. Candidates don’t know K. But each one will knows on which day he or she will be interviewed.

Sadly, Alice forgot her number. The only thing she remember is that she will be interviewed on the second day. Alice also knows on which day Bob will be interviewed. But she doesn’t know the exact number Bob has. Assume that Alice would be the Y-th(1≤Y≤N−K) candidate to interview with the manager on the second day. Please help Alice to calculate the expectation of Y because Alice doesn’t want to go out too early.

Input
The first line is the number of test cases. It’s guaranteed that the test cases is not greater than 105.

Each test case contains two integers N and D (2≤N≤109,D= 1 or 2). D=1 means Bob will be interviewed on the first day and D=2 means Bob will be interviewed on the second day.

Output
Each test case contains one line with one integer. Let¡ˉs assume the possibility be equal to the irreducible fraction P/Q. Print the value of P⋅Q−1 in the prime field of integers modulo 1000000007(109+7).

Sample Input
3
2 1
3 2
100 1

Sample Output
1
875000008
500000029

题意:求期望,枚举可行的k,然后枚举A,B两个人的状态,求和,再除以所有的状态数,当d=1时公式是:i*sum[n-i] (1<=i< n,sum[x]代表1到x的和)对于每一种k,B可以是前k个人的任意一个,而A也可以是后n-k的任意一个,组合一下求和。拆开就是i*(n-i)(n-i+1)/2(1<=i< n);这里用到前n为平方和,前n位3次方和。当d=2时,公式就是(sum[n-i](n-i-1))(0<=i< n-1);同上拆开就好了。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,b;
const int mod = 1e9+7;

ll qpow(int x,int b){
    ll sum =1;
    ll now =x;
    while(b){
        if(b&1) sum =sum*now%mod;
        now = now*now%mod;
        b >>= 1;
    }
    return sum;
}
ll q1(ll x){
    return x*(x+1)/2%mod;
}
ll q2(ll x){
    return x*(x+1)%mod*(2*x+1)%mod*qpow(6,mod-2)%mod;
}

ll q3(ll x){
    ll now= x*(x+1)/2%mod;
    return now*now%mod;
}
void add(ll &x,ll y){
    x+= y;
    if(x > mod) x -= mod;
    if(x < 0) x += mod;
}

int main(){
    int T;
    cin >> T;
    while(T--){
        scanf("%lld %lld",&n,&b);
        ll a1=0,a2=0;
        if(b == 1){
/*
            ll a1 = q3(n-1);
            a1 = (a1-(2*n+1)*q2(n-1))%mod;
            a1 = (a1+(n*n+n)%mod*q1(n-1))%mod;
            a1 = (a1+mod)%mod*qpow(2,mod-2)%mod;
            ll a2 = (n*q1(n-1)-q2(n-1))%mod;
            a2 = (a2+mod)%mod;
            ans = a1*qpow(a2,mod-2)%mod;
*/
            add(a1,q3(n-1));
            add(a1,-(2*n+1)*q2(n-1)%mod);
            add(a1,(n*n+n)%mod*q1(n-1)%mod);
            a1 = a1*qpow(2,mod-2)%mod;
            add(a2,n*q1(n-1)%mod);
            add(a2,-q2(n-1));
        }
        else{

/*
            ll a1 = (q3(n)-q1(n))%mod;
            a1 = (a1+mod)%mod*qpow(2,mod-2)%mod;
            ll a2 = (q2(n)-q1(n))%mod;
            a2 = (a2+mod)%mod;
            ans = a1*qpow(a2,mod-2)%mod;
*/
            add(a1,q3(n));
            add(a1,-q1(n));
            a1 = a1*qpow(2,mod-2)%mod;
            add(a2,q2(n));
            add(a2,-q1(n));
        }
        printf("%d\n",a1*qpow(a2,mod-2)%mod);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值