cf 575H Bots

H. Bots
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
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?

Input

The first and only line contains integer N.

  • 1 ≤ N ≤ 106
Output

Output should contain a single integer – number of possible states modulo 109 + 7.

Sample test(s)
input
2
output
19
Note

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.


题意:形成如图的树,即每条根到叶子的路径都包含相同的蓝和红(0和1)边的所有情况,存在多少个节点。

思路:0~n深度形成的树,是一颗满二叉树,即节点数为pow(2,n+1)-1个节点。

从n+1深度开始,每层分为单支节点和双支节点,可知第i层单支节点的数目为pd(i)=2*C(i,n) ——即走过的i条边中n边为同一色的方案数,所以第i+1层的支数为 2*第i层的双支数(counti-pd(i))+第i层的单支数pd(i)。

最后c(i,n)通过逆元求解即可。下面为官方题解,count公式存在错误。


//#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <conio.h>
#include <iostream>
using namespace std;
#define rd(x) scanf("%d",&x)
#define rd2(x,y) scanf("%d%d",&x,&y)
#define ll long long int
#define maxn 100005
#define mod 1000000007
#define pii pair<int,int>
#define maxn 100005
int n,x;
int inv(int a,int m){//  (x/a)%m的逆元
    int p=1,q=0,b=m,c,d;
    while(b>0){
        c=a/b;
        d=a; a=b; b=d%b;
        d=p; p=q; q=d-c*q;
    }
    return p<0?p+m:p;
}
ll pow(int a,int b){
    ll res=1;
    ll k=a;
    while(b){
        if(b&1){
            res=res*k%mod;
        }
        k=k*k%mod;
        b>>=1;
    }
    return res;
}
ll c[2000005];
int main()
{
    rd(n);
    c[n]=1;
    for(int i=n+1;i<=2*n;i++){//C(i,n)
        c[i]=c[i-1]*i%mod*inv(i-n,mod)%mod;
    }
    ll res=(pow(2,n+1)-1+mod)%mod;
    ll Count=pow(2,n);
    for(int i=n+1;i<=2*n;i++){
        Count=(2*Count-2*c[i-1]+mod*2)%mod;
        res=(res+Count)%mod;
    }
    printf("%I64d\n",res);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值