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 <iostream>
#include <algorithm>
#include <string>
#include <map>
#include <cstring>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <set>
#include <stack>
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<<ans<<endl;
return ans;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif // ONLINE_JUDGE
for(int i=1;i<=1000;i++){
c[i][0]=1;
for(int j=1;j<=i-1;j++)
c[i][j]=(c[i-1][j]+c[i-1][j-1])%MOD;
c[i][i]=1;
}
int T;
scanf("%d",&T);
while(T--){int n,m;
scanf("%d%d",&n,&m);
ll p=epow(2,n);
ll e=0;
for(int i=n;i>=m;i--)
e=(e+c[n][i])%MOD;
printf("%lld\n",e*epow(p,MOD-2)%MOD);
}
return 0;
}