Problem Description
At the start of this game, there are N knights on a road of length N+1. The knights are numbered from 1 to N, and the ith knight stands i unit from the left end of the road.
When the game begins, each knight moves to left or right at the same speed. Whenever a knight reaches to the end of the road, he instantly changes his direction.
Whenever two knights meet, they fight until one loses, where each one of them wins in 50% possibility. Then the winner keeps moving in the direction he was moving before the fight, and the loser quits the game. The fighting time is very short and can be ignored.
The game continues until only one knight remains, and that knight is the winner.
Now, we know the moving direction of each knight initially. Can you calculate the possibility that Nth knight win the game?
When the game begins, each knight moves to left or right at the same speed. Whenever a knight reaches to the end of the road, he instantly changes his direction.
Whenever two knights meet, they fight until one loses, where each one of them wins in 50% possibility. Then the winner keeps moving in the direction he was moving before the fight, and the loser quits the game. The fighting time is very short and can be ignored.
The game continues until only one knight remains, and that knight is the winner.
Now, we know the moving direction of each knight initially. Can you calculate the possibility that Nth knight win the game?
Input
The first line of the input gives the number of test cases T (T <= 10). In each test case, the first line is an integer N (1 <= N <= 1000). The second line contains N integers. The ith integer represents the ith knight’s moving direction, and 0 stands for left while 1 stands for right.
Output
Each test case contains one line and one integer. Let’s assume the possibility be equal to the irreducible fraction P / Q. Print the value of
P⋅Q−1
in the prime field of integers modulo
1 000 000 007(109+7)
. It is guaranteed that this modulo does not divide Q, thus the number to be printed is well-defined.
Sample Input
2 2 0 0 3 0 1 0
Sample Output
Case #1: 500000004 Case #2: 250000002
首先第一个骑士肯定向右,第
N
个肯定向左
第
N
个骑士获胜的条件,即为打败所有左边向右走的骑士
设
dp[i][j]
为前
i
个骑士有
j
个向右走的概率
- 如果第 i 个骑士向右走,那么 dp[i][j]=dp[i−1][j−1]
- 如果第
i
个骑士向左走,那么
dp[i][j]=∑k=ji−1dp[i−1][k]×(12)k−j+1
即原本有 k 个骑士向左走,第 i 个骑士阻挠了 k−j 个,并且最后失败的概率
这个转移方程还需要优化一下,由于是个类似等比数列的形式
所以 dp[i][j]=dp[i][j+1]+dp[i−1][j]2
还有一种情况是,第 i 个骑士向左走的时候,
打败了左边所有的骑士,最后改变方向向右走
dp[i][1]+=∑k=1i−1dp[i−1][k]×(12)k
最后的答案即为
dp[N][1]2
,表示最后只剩两个骑士决斗,第 N个骑士获胜的概率
由于优化了转移方程,所以总的时间复杂度是
O(N2)
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define L(i) i<<1
#define R(i) i<<1|1
#define pi acos(-1.0)
#define eps 1e-9
#define maxn 1001000
#define MOD 1000000007
#define LL long long
const LL mod = 1e9 +7;
const int INF = 100000000;
int a[1200];
LL dp[1200][1200];
LL inv2 = mod /2 + 1;
int T;
int n;
int main()
{
// freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int cas=1;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
a[n]=0; // wa 2 fa
printf("Case #%d: ",cas++);
if(n==1)
{
printf("1\n");
continue;
}
memset(dp,0,sizeof(dp));
dp[1][1]=1;
for(int i=2;i<=n;i++)
{
if(a[i]==1)
{
for(int j=2;j<=i;j++)
dp[i][j] = dp[i-1][j-1];
}else
{
for(int j=i-1;j>=1;j--)
dp[i][j] = ((dp[i][j+1] + dp[i-1][j])%mod * inv2)%mod;
dp[i][1] = (2 * dp[i][1]) % mod;
}
}
printf("%lld\n",(dp[n][1] * inv2 )%mod);
}
return 0;
}
Problem Description
At the start of this game, there are N knights on a road of length N+1. The knights are numbered from 1 to N, and the ith knight stands i unit from the left end of the road.
When the game begins, each knight moves to left or right at the same speed. Whenever a knight reaches to the end of the road, he instantly changes his direction.
Whenever two knights meet, they fight until one loses, where each one of them wins in 50% possibility. Then the winner keeps moving in the direction he was moving before the fight, and the loser quits the game. The fighting time is very short and can be ignored.
The game continues until only one knight remains, and that knight is the winner.
Now, we know the moving direction of each knight initially. Can you calculate the possibility that Nth knight win the game?
When the game begins, each knight moves to left or right at the same speed. Whenever a knight reaches to the end of the road, he instantly changes his direction.
Whenever two knights meet, they fight until one loses, where each one of them wins in 50% possibility. Then the winner keeps moving in the direction he was moving before the fight, and the loser quits the game. The fighting time is very short and can be ignored.
The game continues until only one knight remains, and that knight is the winner.
Now, we know the moving direction of each knight initially. Can you calculate the possibility that Nth knight win the game?
Input
The first line of the input gives the number of test cases T (T <= 10). In each test case, the first line is an integer N (1 <= N <= 1000). The second line contains N integers. The ith integer represents the ith knight’s moving direction, and 0 stands for left while 1 stands for right.
Output
Each test case contains one line and one integer. Let’s assume the possibility be equal to the irreducible fraction P / Q. Print the value of
P⋅Q−1
in the prime field of integers modulo
1 000 000 007(109+7)
. It is guaranteed that this modulo does not divide Q, thus the number to be printed is well-defined.
Sample Input
2 2 0 0 3 0 1 0
Sample Output
Case #1: 500000004 Case #2: 250000002