| 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 | ||||||
| Source | ||||||
| "尚学堂杯"哈尔滨理工大学第七届程序设计竞赛 |
题意:上楼梯,起点在一阶,每次可走一阶两阶或三阶,但有一些台阶坏了不能走,问到第n阶楼梯有多少种走法。
思路:类似于斐波那契数列,a[i] = a[i-1] + a[i-2] + a[i-3] 。
#include<bits/stdc++.h>
using namespace std;
#define Mod 10007
const int N = 10000 + 10 ;
int a[N],b[N];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)==2)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;i<m;i++)
{
int x;
scanf("%d",&x);
b[x]=1;
}
a[2]=b[2]?0:1;
a[3]=b[3]?0:a[2]+1;
a[4]=b[4]?0:a[2]+a[3]+1;
for(int i=5;i<=n;i++)
{
if(b[i])continue ;
if(!b[i-1])
a[i]+=a[i-1];
if(!b[i-2])
a[i]+=a[i-2];
if(!b[i-3])
a[i]+=a[i-3];
a[i]%=Mod;
}
printf("%d\n",a[n]);
}
}

本文介绍了一个经典的上楼梯问题,并提出了一种改进的斐波那契数列算法来解决该问题。具体而言,该问题考虑了某些台阶损坏的情况,目标是在避开这些损坏的台阶的情况下,计算从起点到达终点的不同路径数量。


421

被折叠的 条评论
为什么被折叠?



