C.Collection Game | |||||
| |||||
Description | |||||
POI and POJ are pair of sisters, one is a master in “Kantai Collection”, another is an excellent competitor in ACM programming competition. One day, POI wants to visit POJ, and the pace that between their homes is made of square bricks. We can hypothesis that POI’s house is located in the NO.1 brick and POJ’s house is located in the NO.n brick. For POI, there are three ways she can choose to move in every step, go ahead one or two or three bricks. But some bricks are broken that couldn’t be touched. So, how many ways can POI arrive at POJ’s house? | |||||
Input | |||||
There are multiple cases. In each case, the first line contains two integers N(1<=N<=10000) and M (1<=M<=100), respectively represent the sum of bricks, and broke bricks. Then, there are M number in the next line, the K-th number a[k](2<=a[k]<=n-1) means the brick at position a[k] was broke. | |||||
Output | |||||
Please output your answer P after mod 10007 because there are too many ways. | |||||
Sample Input | |||||
5 1 3 | |||||
Sample Output | |||||
3 |
#include <iostream>
#include<stdio.h>
#include<cstring>
const int mod=10007;
using namespace std;
int a[10001],b[10001],c[10001];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
for(int i=1;i<=m;i++)
{
scanf("%d",&a[i]);
b[a[i]]=1;
}
c[1]=1;
if(!b[2])
{
c[2]=1;
}
if(!b[3])
{
c[3]=c[2]+c[1];
}
if(!b[4])
{
c[4]=c[3]+c[2]+c[1];
}
for(int i=5;i<=n;i++)
{
if(!b[i])
{
c[i]=(c[i-1]+c[i-2]+c[i-3])%mod;
}
}
cout<<c[n]<<endl;
}
return 0;
}
自己之前写的代码是递归超时,
然后这里给了个官方题解
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int dp[100005], vis[100005]; const int mod = 10007; int main() { int n, m; while(~scanf("%d%d", &n, &m)) { memset(vis, 0, sizeof(vis)); memset(dp, 0, sizeof(dp)); for(int i = 0, x; i < m; i++) { scanf("%d", &x); if(x < n) vis[x] = 1; } dp[1] = 1; for(int i = 2; i <= n; i++) { if(!vis[i]) { dp[i] = (dp[i] + dp[i-1])%mod; if(i > 2) dp[i] = (dp[i] + dp[i-2])%mod; if(i > 3) dp[i] = (dp[i] + dp[i-3])%mod; } else dp[i] = 0; } printf("%d\n", dp[n]); } return 0; }