Bots(逆元,递推)
1.5 seconds
256 megabytes
standard input
standard output
Sasha and Ira are two best friends. But they aren’t just friends, they are software engineers and experts in artificial intelligence. They are developing an algorithm for two bots playing a two-player game. The game is cooperative and turn based. In each turn, one of the players makes a move (it doesn’t matter which player, it's possible that players turns do not alternate).
Algorithm for bots that Sasha and Ira are developing works by keeping track of the state the game is in. Each time either bot makes a move, the state changes. And, since the game is very dynamic, it will never go back to the state it was already in at any point in the past.
Sasha and Ira are perfectionists and want their algorithm to have an optimal winning strategy. They have noticed that in the optimal winning strategy, both bots make exactly N moves each. But, in order to find the optimal strategy, their algorithm needs to analyze all possible states of the game (they haven’t learned about alpha-beta pruning yet) and pick the best sequence of moves.
They are worried about the efficiency of their algorithm and are wondering what is the total number of states of the game that need to be analyzed?
The first and only line contains integer N.
- 1 ≤ N ≤ 106
Output should contain a single integer – number of possible states modulo 109 + 7.
2
19
Start: Game is in state A.
- Turn 1: Either bot can make a move (first bot is red and second bot is blue), so there are two possible states after the first turn – B and C.
- Turn 2: In both states B and C, either bot can again make a turn, so the list of possible states is expanded to include D, E, F and G.
- Turn 3: Red bot already did N=2 moves when in state D, so it cannot make any more moves there. It can make moves when in state E, F and G, so states I, K and M are added to the list. Similarly, blue bot cannot make a move when in state G, but can when in D, E and F, so states H, J and L are added.
- Turn 4: Red bot already did N=2 moves when in states H, I and K, so it can only make moves when in J, L and M, so states P, R and S are added. Blue bot cannot make a move when in states J, L and M, but only when in H, I and K, so states N, O and Q are added.
Overall, there are 19 possible states of the game their algorithm needs to analyze.

#include<bits/stdc++.h>
using namespace std;
const int M = 2e6 + 10 ;
const int mod = 1e9 + 7 ;
int F[M] , Finv[M] , inv[M] ;
int n ; void table () {
inv[1] = 1 ;
for (int i = 2 ; i < M ; i ++) inv[i] = (mod-mod/i) *1ll* inv[mod%i] % mod ;
Finv[0] = F[0] = 1 ;
for (int i = 1 ; i < M ; i ++) {
F[i] = 1ll*F[i-1]*i%mod ;
Finv[i] = 1ll*Finv[i-1]*inv[i]%mod ;
}
} int comb (int n , int m) {
if (m < 0 || m > n) return 0 ;
return F[n] * 1ll * Finv[n-m] % mod * Finv[m] % mod ;
} int main () {
table () ;
//printf ("comb(3,3)=%d\n" , comb(3,3)) ;
//printf ("F[3] = %d , Finv[0] = %d , Finv[3] = %d\n" , F[3] , Finv[0] , Finv[3] ) ;
//printf ("Finv[2] = %d , inv[3] = %d\n" , Finv[2] , inv[3]) ;
scanf ("%d" , &n) ;
int num = 1 ;
int sum = 1 ;
for (int i = 1 ; i <= 2*n-1 ; i ++) {
num = (comb(i,n) + ((num-comb(i,n))*1ll*2%mod + mod)% mod ) % mod ;
sum = (sum+num) % mod ;
//printf ("num = %d , comb(%d,%d)=%d\n" , num , i , n , comb(i,n)) ;
}
printf ("%d\n" , (1ll*sum*2+1)%mod) ;
return 0 ;
}
首先把产生的树对半开,那么你很容易就可以发现层与层之间是存在递推关系的。
画过图你就会发现,当你从第x从画到第x+1层时,有先点扩展出了两个子节点,有些点只扩展出了一个节点。
进一步观察,你很容易想到,有些点之所以至扩展出一个节点,是因为对于这个支路它的其中一种颜色已经用完了。
而且你可以知道第x层的点数 的物理意义为,走x步的所有方案数。(一直x层共有k个点)
其中只会延伸出一个节点的点数为C(x,n) 。
所以x+1层的点数为 C(x,n) + (k-C(x,n)) * 2 ;
另外,linyujun发现了一个通式:
答案为C(2*(n+1) , n+1) - 1 ; (用眼睛看出来的,6666)
Bots(逆元,递推)的更多相关文章
- 【bzoj 2339】[HNOI2011]卡农(数论--排列组合+逆元+递推)
		题意:从编号为 1~N 的音阶中可选任意个数组成一个音乐片段,再集合组成音乐篇章.要求一个音乐篇章中的片段不可重复,都不为空,且出现的音符的次数都是偶数个.问组成 M 个片段的音乐篇章有多少种.答案取 ... 
- BZOJ1951 [Sdoi2010]古代猪文 【费马小定理 + Lucas定理 + 中国剩余定理 + 逆元递推 + 扩展欧几里得】
		题目 "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久很久以前,在山的那 ... 
- 求逆元的两种方法+求逆元的O(n)递推算法
		到国庆假期都是复习阶段..所以把一些东西整理重温一下. gcd(a,p)=1,ax≡1(%p),则x为a的逆元.注意前提:gcd(a,p)=1; 方法一:拓展欧几里得 gcd(a,p)=1,ax≡1( ... 
- ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)
		Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N- ... 
- 逆元 组合A(n,m) C(n,m)递推 隔板法
		求逆元 https://blog.csdn.net/baidu_35643793/article/details/75268911 int inv[N]; void init(){ inv[] = ; ... 
- [BJOI2019]光线[递推]
		题意 题目链接 分析 令 \(f_i\) 表示光线第一次从第一块玻璃射出第 \(i\) 块玻璃的比率. 令 \(g_i\) 表示光线射回第 \(i\) 块玻璃,再射出第 \(i\) 块玻璃的比率. 容 ... 
- BZOJ4451 [Cerc2015]Frightful Formula 多项式 FFT 递推 组合数学
		原文链接http://www.cnblogs.com/zhouzhendong/p/8820963.html 题目传送门 - BZOJ4451 题意 给你一个$n\times n$矩阵的第一行和第一列 ... 
- AGC001E BBQ Hard 组合、递推
		传送门 题意:给出长度为$N$的两个正整数序列$A_i,B_i$,求$\sum\limits_{i=1}^N \sum\limits_{j=i+1}^N C_{A_i+A_j+B_i+B_j}^{A_ ... 
- [NOI2017]泳池——概率DP+线性递推
		[NOI2017]泳池 实在没有思路啊~~~ luogu题解 1.差分,转化成至多k的概率减去至多k-1的概率.这样就不用记录“有没有出现k”这个信息了 2.n是1e9,感觉要递推然后利用数列的加速技 ... 
- [洛谷P4723]【模板】线性递推
		题目大意:求一个满足$k$阶齐次线性递推数列$a_i$的第$n$项. 即:$a_n=\sum\limits_{i=1}^{k}f_i \times a_{n-i}$ 题解:线性齐次递推,先见洛谷题解, ... 
随机推荐
- AngularJs $interpolate 和 $parse
			$interpolate 将一个字符串编译成一个插值函数.HTML编译服务使用这个服务完成数据绑定. 使用:$interpolate(text,[mustHaveExpression],[truste ... 
- JSP实现数据传递与保存
			业务逻辑: 1.登陆login.jsp 2.判断登陆是否成功check.jsp 3.登陆成功页面newsDetail.jsp 4.登陆失败转发到login.jsp 代码如下: <%@ page ... 
- HTML5学习总结-11 IOS 控件WebView显示网页
			一 加载外部网页 1.使用UIWebView加载网页 运行XCode 新建一个Single View Application . 2 添加安全消息 添加以下消息到项目的 Info.plist &l ... 
- Untiy3D - 窗口界面2
			Component 组件 Mesh:网格 Mesh Filter: 网格滤镜 Mesh Renderer 网格渲染器 Mesh Collider: 网格碰撞器.网格碰撞节点 Convex: 凸的.凸面 ... 
- NOIp 0916 爆零记
			题目来自神犇chad 上次爆零是说着玩,这次真的爆零了QAQ 好吧貌似是TYVJ的模拟赛打多了..一直按照TYVJ的格式提交的压缩包.. 然后莫名其妙就AK了hhh 来的时候迟到了半小时,昨晚痛苦的补 ... 
- git如何放弃所有本地修改?
			问题描述: 本地做了一些修改,我用git rebase说有冲突.我现在想把本地的请求都干掉,可能有的已经commit过了(没有push过),完全同步成远程版本,应该用什么命令? 使用命令: git r ... 
- (总结)Nginx配置文件nginx.conf中文详解
			#定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 8; #全局错误日志定义类型,[ debug | ... 
- 转:在VS2010下编译、调试和生成mex文件
			最近帮人调了一个程序,是网上公开的代码,利用matlab与c++混合编程做三维模型关键点检测,发现他们可以用VS2010编译.调试.生成mexw32文件,因此觉得之前在Matlab上利用mex命令真是 ... 
- Linux开放1521端口允许网络连接Oracle Listene
			症状:1. TCP/IP连接是通的.可以用ping 命令测试. 2. 服务器上Oracle Listener已经启动. lsnrctl status 查看listener状态 lsnrctl s ... 
- adapter(转自Devin Zhang)
			1.概念 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的View(ListView,GridView)等地方都需要用到Adapter.如下图直 ... 
