Chosen by god FZU - 2301

Chosen by god

 

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)

题目大意:

有n次攻击回合,每个回合的攻击有一点伤害,但是可能攻击到敌方无限血的英雄,也可能攻击到敌方的一个血量为m的小兵,问在n次攻击回合内把敌方小兵杀死的概率为多少?答案对1e9+7取膜。

分析:

求概率,那就是需要牵扯到除法和模数的问题,一看见答案那么大,那就是需要求逆元了,关于逆元的求法我就不在这里赘述了,这题我要提出的是组合数的,可以先打一个表,这样省时间,剩下的就按照求概率的方法做就好了

下面是AC的参考代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
const int N = 1e6+10;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const int MOD=1e9+7;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define Abs(x) ((x)>=0?(x):-(x))
ll c[1010][1010];
ll epow(ll base,ll p){
    ll ans=1;
    while(p){
        if(p&1)
            ans=(ans*base)%MOD;
        p>>=1;
        base=(base*base)%MOD;
    }
//    cout<=m;i--)
            e=(e+c[n][i])%MOD;
        printf("%lld\n",e*epow(p,MOD-2)%MOD);
    }
    return 0;
 }

 

 

 

你可能感兴趣的:(逆元)