Chosen by god FZU - 2301

Everyone knows there is a computer game names "hearth stone", recently xzz likes this game very much. If you want to win, you need both capacity and good luck.

There is a spell card names "Arcane Missiles", which can deal 3 damages randomly split among all enemies.

xzz have a "Arcane Missiles Plus" can deal n damage randomly split among all enemies. The enemy battle field have only two characters: the enemy hero and enemy minion.

Now we assume the enemy hero have infinite health, the enemy minion has m health.

xzz wants to know the probability of the "Arcane Missiles Plus" to kill the enemy minion (the minion has not more than 0 health after "Arcane Missiles Plus"), can you help him?

Input

The first line of the input contains an integer T, the number of test cases. T test cases follow. (1 ≤ T ≤ 100000)

Each test case consists of a single line containing two integer n and m. (0 ≤ m ≤ n ≤ 1000)

Output

For each test case, because the probability can be written as x / y, please output x * y^-1 mod 1000000007. .(y * y^-1 ≡ 1 mod 10^9 + 7)

Sample Input

2
3 3
2 1

Sample Output

125000001
750000006

题意:你有一个法术具有n次伤害,每次伤害可以随机的对宠物或者英雄减少一滴血,问你杀死宠物的概率是多少?把最后的结果mod 1000000007

思路:你可以杀死宠物的情况为C(n,m),C(n,m+1),.....C(n,n) 所以分子为C(n,m)+C(n,m+1)+C(n,m+2)+...+C(n,n),

由于你有n 次机会,每次有两种选择,所以分母为 pow(2,n) ;由于除法取模 所以用到逆元,最后答案为C(n,m)+C(n,m+1)+C(n,m+2)+...+C(n,n)*pow(pow(2,n),mod-2)%mod;

注意取模

代码如下:

#include
#include
#include
#define ll long long
using namespace std;
const int mod =1e9+7;
const int maxn =1005;
ll C[maxn][maxn];
ll ans[maxn][maxn];
ll sum[maxn];
ll quck_pow(ll a,ll b)
{
    if(!b)return 1;
    ll mid=(quck_pow(a,b/2))%mod;
    if(b&1)return (mid%mod*mid%mod*a%mod)%mod;
    else return (mid%mod*mid%mod)%mod;
}
void init()
{
    for(int i=0;i<=maxn;i++)
    {
        C[i][0]=1;
        C[i][i]=1;
    }
    for(int i=2;i<=maxn;i++)
    {
        for(int j=1;j

 

你可能感兴趣的:(Chosen by god FZU - 2301)