Codeforces 369D.Valera and Fools

D. Valera and Fools
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One fine morning, n fools lined up in a row. After that, they numbered each other with numbers from 1 to n, inclusive. Each fool got a unique number. The fools decided not to change their numbers before the end of the fun.

Every fool has exactly k bullets and a pistol. In addition, the fool number i has probability of pi (in percent) that he kills the fool he shoots at.

The fools decided to have several rounds of the fun. Each round of the fun looks like this: each currently living fool shoots at another living fool with the smallest number (a fool is not stupid enough to shoot at himself). All shots of the round are perfomed at one time (simultaneously). If there is exactly one living fool, he does not shoot.

Let's define a situation as the set of numbers of all the living fools at the some time. We say that a situation is possible if for some integer number j (0 ≤ j ≤ k) there is a nonzero probability that after j rounds of the fun this situation will occur.

Valera knows numbers p1, p2, ..., pn and k. Help Valera determine the number of distinct possible situations.

Input

The first line contains two integers n, k (1 ≤ n, k ≤ 3000) — the initial number of fools and the number of bullets for each fool.

The second line contains n integers p1, p2, ..., pn (0 ≤ pi ≤ 100) — the given probabilities (in percent).

Output

Print a single number — the answer to the problem.

Sample test(s)
input
3 3
50 50 50
output
7
input
1 1
100
output
1
input
2 1
100 100
output
2
input
3 3
0 0 0
output
1
Note

In the first sample, any situation is possible, except for situation {1, 2}.

In the second sample there is exactly one fool, so he does not make shots.

In the third sample the possible situations are {1, 2} (after zero rounds) and the "empty" situation {} (after one round).

In the fourth sample, the only possible situation is {1, 2, 3}.

有N个人,每个人M个子弹,每次第2到最后一个人攻击第一个人,第一个人攻击第2个人,

每个人有命中率P,

问可能有多少不同的生存状态。

注意每次只有第一个和第二个人被攻击,原来第一个和第二个人之间的人,第一个前面的人全部死了,

其他人都没死。

也就是原来在第二个人前面的人中只有第一个人还没死。

所以状态可以表示为当前第一个人的编号i,第二个人的编号j。

如果第二个到最后一个人中有人命中率为100%,第一个人必死,否则可能活。


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

const int maxn=4700;
int p[maxn],n,m;

bool vis[maxn][maxn];
int P[maxn];
bool can[maxn];
bool fail[maxn];
void dfs(int dep,int fi,int se)     ///blank when fi==n+1&&se==n+2
{
    if(dep>m)   return;
    if(se>n+2)   return;
    vis[fi][se]=1;
    if(fail[se]&&p[fi]>0&&!vis[se+1][se+2])dfs(dep+1,se+1,se+2);   ///both die
    if(fail[se]&&p[fi]<100&&!vis[se][se+1])dfs(dep+1,se,se+1);     /// 1 die   2 alive
    if((!can[se])&&p[fi]>0&&!vis[fi][se+1])
        dfs(dep+1,fi,se+1);      ///1 alive  2 die
}
int main()
{
    while(cin>>n>>m)
    {for(int i=1;i<=n;i++)cin>>p[i];
    P[n]=p[n];
    fail[n+1]=0;

    for(int i=n;i>0;i--)fail[i]=(fail[i+1]|(p[i]>0));
     memset(can,0,sizeof can);
    can[n+1]=0;
    for(int i=n;i>0;i--)can[i]=can[i+1]|(p[i]==100);
    memset(vis,0,sizeof vis);

    if(n==1){cout<<1<<endl;return 0;}

    dfs(0,1,2);
    int cnt=0;
    for(int i=1;i<=n;i++)
        for(int j=i+1;j<=n+1;j++)
        {
            cnt+=vis[i][j];          
        }
  
    cnt+=vis[n+1][n+2];
    cout<<cnt<<endl;
    }
}
/**
3 3
50 50 50

2 1
100 100

3 3
0 0 0
*/



你可能感兴趣的:(Algorithm,dp,ACM,codeforces,CF)