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)

Sample Input

2
3 3
2 1

Sample Output

125000001
750000006

题意:

给出T组n和m n代表你有n发子弹 敌人有两个 一个血量无穷大 一个为m 每次击中敌人都会掉1滴血 问把血量为m的敌人打死的几率有多大 每次开枪随机打中其中一个敌人

思路:

概率问题 先找所有情况 2^n 击中的概率 c(n,m)+c(n,m+1)+……+c(n,n)

所以p=c(n,m)+c(n,m+1)+……+c(n,n)/2^n

看到组合数求和就有点怕了 想到了组合数打表 之前只是听到过

杨辉三角打表组合数、组合数求和:

仔细观察杨辉三角 有如下规律:

第一行表示c(0,0)n=0

第二行:c(1,0)c(1,1)n=1

以后可以递推,并且每个数的值是其肩膀上两个值的和 根据这个可以打表 代码如下:

void getc()
{
c[0][0]=1,sum[0][0]=1;
for(int i=1;i<=1001;i++){
for(int j=0;j<=i;j++){
if(j==0||j==i){
c[i][j]=1;
}
else{
c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
}
if(j==0){
sum[i][j]=1;
}
else{
sum[i][j]=(sum[i][j-1]+c[i][j])%mod;
}
}
}
}

复杂度高了点 但用这个题还好没T

但如果不去先sum求和打好表 就不好说了

AC代码:

下面代码里面也用到了取余的公式 特别是减法 WA了好多次:https://mp.csdn.net/postedit/89529166

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string>
#include<string.h>
#include<map>
using namespace std;
typedef long long LL;
const int MAX=1e6;
const LL mod=1e9+7;
const LL INF=0x3f3f3f3f;
LL qpow(LL m,LL q)
{
LL ans=1;
while(q){
if(q%2){
ans=ans*m%mod;
}
m=m*m%mod;
q/=2;
}
return ans%mod;
}
LL getinv(LL x,LL y)
{
LL ans=qpow(x,y-2)%mod;
return ans;
}
LL c[1005][1005],sum[1005][1005];
void getc()
{
c[0][0]=1,sum[0][0]=1;
for(int i=1;i<=1001;i++){
for(int j=0;j<=i;j++){
if(j==0||j==i){
c[i][j]=1;
}
else{
c[i][j]=(c[i-1][j-1]+c[i-1][j])%mod;
}
if(j==0){
sum[i][j]=1;
}
else{
sum[i][j]=(sum[i][j-1]+c[i][j])%mod;
}
}
}
}
int main()
{
getc();
LL T,n,m;
cin>>T;
while(T--){
LL num=0,sum1=0;
cin>>n>>m;
num=(sum[n][n]-sum[n][m-1]+mod)%mod;
sum1=getinv(qpow(2,n),mod);
sum1=(sum1*num)%mod;
cout<<sum1<<"\n";
}
return 0;
}

Chosen by god【组合数打表】的更多相关文章

  1. fjwc2019 D6T1 堆(组合数+打表)

    #193. 「2019冬令营提高组」堆 但是每个点都遍历一遍,有些点的子树完全相同却重复算了 忽然记起完全二叉树的性质之一:每个非叶节点的子树中至少有一个是满二叉树 那么我们预处理满二叉树的那一块,剩 ...

  2. HDU-5226 Tom and matrix(组合数求模)

    一.题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5226 二.题意 给一个大矩阵,其中,$a[i][j] = C_i^j$.输入5个参数,$x_1, ...

  3. NOIP2016 D2T1 组合数问题

    洛谷P2822 数学真重要啊…… 其实解这一题的关键就是组合恒等式:C(n,m)=C(n-1,m)+C(n-1,m-1),然后再知道组合数的矩阵(杨辉三角)和题中n,m的关系就很容易解决了(然而做这题 ...

  4. [MySQL5.6] 一个简单的optimizer_trace示例

    [MySQL5.6] 一个简单的optimizer_trace示例   前面已经介绍了如何使用和配置MySQL5.6中optimizer_trace(点击博客),本篇我们以一个相对简单的例子来跟踪op ...

  5. Round Numbers

    转载请注明出处:優YoU http://user.qzone.qq.com/289065406/blog/1301472836 大致题意: 输入两个十进制正整数a和b,求闭区间 [a ,b] 内有多少 ...

  6. Round Numbers(组合数学)

    Round Numbers Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Tota ...

  7. [SinGuLaRiTy] 复习模板-数学

    [SinGuLaRiTy-1047] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 质因数分解 void solve(int n) { == ...

  8. [HEOI2013]SAO ——计数问题

    题目大意: Welcome to SAO ( Strange and Abnormal Online).这是一个 VR MMORPG, 含有 n 个关卡.但是,挑战不同关卡的顺序是一个很大的问题. 有 ...

  9. [SDOI2009]Bill的挑战——全网唯一 一篇容斥题解

    全网唯一一篇容斥题解 Description Solution 看到这个题,大部分人想的是状压dp 但是我是个蒟蒻没想到,就用容斥切掉了. 并且复杂度比一般状压低, (其实这个容斥的算法,提出来源于y ...

随机推荐

  1. 手机短号(hdu2081)

    这里字符串的输入用gets_s()函数. #include<stdio.h> using namespace std; int main() { int N; scanf_s(" ...

  2. Python3高级核心技术97讲

    可以毫不夸张的说:这门课程是初中级Python开发人员向高级进阶的必学课程 许多Pythoner喜欢追求新的框架,但却不重视Python本身基础知识的学习, 他们不知道的是,语言本身的进阶优先于框架, ...

  3. web项目——org.apache.jasper.JasperException: /WEB-INF/content/mainForm.jsp (line: 3, column: 62) File "/WEB-INF/c.tld" not found

    报错信息: HTTP Status 500 – Internal Server Error Type Exception Report Message /WEB-INF/content/mainFor ...

  4. Ant Design of Vue 组件库的使用

    文档里面很清楚 安装步骤    这是全部引入的 1  有的组价涉及到汉化的问题 import moment from 'moment' import '../../../../node_modules ...

  5. C#基础之构造函数(构造器)

    在每个类里面默认都有一个构造方法,正式因为有了这些方法,你未赋值的变量才会有初始值,当然,我们也可以手动自己创建构造函数,可以创建多个构造函数,自己给出默认值或者!!!规定调用此类的程序对象必须要赋值 ...

  6. [JavaWeb基础] 030.dom4j读取xml的4种方法

    通常我们在项目开发的过程中经常要操作到xml文件,在JAVA这边,我们会很自然的联想到Dom4J这个apache的开源插件,那么我们使用Dom4J如何来读取xml文件呢?下面我们来看看以下4种方法 1 ...

  7. React面试题(超详细,附答案)

    生命周期 组件将要挂载时触发的函数:componentWillMount 组件挂载完成时触发的函数:componentDidMount 是否要更新数据时触发的函数:shouldComponentUpd ...

  8. 高吞吐量的分布式发布订阅消息系统Kafka之Producer源码分析

    引言 Kafka是一款很棒的消息系统,今天我们就来深入了解一下它的实现细节,首先关注Producer这一方. 要使用kafka首先要实例化一个KafkaProducer,需要有brokerIP.序列化 ...

  9. 一篇文章讲透Dijkstra最短路径算法

    Dijkstra是典型最短路径算法,计算一个起始节点到路径中其他所有节点的最短路径的算法和思想.在一些专业课程中如数据结构,图论,运筹学等都有介绍.其思想是一种基础的求最短路径的算法,通过基础思想的变 ...

  10. 【Storm】与Hadoop的区别

    1)Storm用于实时计算,Hadoop用于离线计算. 2)Storm处理的数据保存在内存中,源源不断:Hadoop处理的数据保存在文件系统中,一批一批处 理. 3)Storm的数据通过网络传输进来: ...