首页 > 代码库 > ural 1260. Nudnik Photographer 规律dp

ural 1260. Nudnik Photographer 规律dp

点击打开链接

1260. Nudnik Photographer

Time limit: 1.0 second
Memory limit: 64 MB
If two people were born one after another with one second difference and one of them is a child, then the other one is a child too. We get by induction that all the people are children.
Everyone knows that the mathematical department of the Ural State University is a big family of Npersons, 1, 2, 3, …, N years old respectively.
Once the dean of the department ordered a photo if his big family. There were to be present all the students of the department arranged in one row. At first the dean wanted to arrange them by their age starting from the youngest student, but than he decided that it would look unnatural. Than he advised to arrange the students as follows:
  1. The 1 year old student is to sit at the left end of the row.
  2. The difference in ages of every two neighbors mustn’t exceed 2 years.
The dean decided that thereby the students would seem look as they were arranged by their ages (one can hardly see the difference in ages of 25 and 27 years old people). There exist several arrangements satisfying to the requirements. Photographer didn’t want to thwart dean’s desire and made the photos of all the possible mathematical department students’ arrangements.

Input

There is the integer number N, 1 ≤ N ≤ 55.

Output

the number of photos made by the photographer.

Sample

inputoutput
4
4

Hint

If N = 4 then there are following possible arrangements: (1,2,3,4), (1,2,4,3), (1,3,2,4) and (1,3,4,2).
Problem Author: Alexander Ipatov
Problem Source: Open collegiate programming contest for high school children of the Sverdlovsk region, October 11, 2003

给你1-n这n个数,让他们进行组合排序,要求:
1.1必须放在最左面
2.相邻两个数之间的差不能大于2
求有多少种情况满足要求

设dp[n]表示n个数时符合条件的情况,肯定由dp[n-1]这种情况组成,如果n和n-1相邻且在最后的两个位置的话,可以额外增加一种符合条件的方案,这种情况的数量就是dp[n-3],最后需要额外增加1种情况。

//0.015	202 KB
#include<stdio.h>
#include<string.h>
#define ll long long
using namespace std;
ll dp[70];
int main()
{
    int n;
    memset(dp,0,sizeof(dp));
    dp[1]=1;
    dp[2]=1;
    dp[3]=2;
    for(int i=4;i<=55;i++)
        dp[i]=dp[i-1]+dp[i-3]+1;
    while(scanf("%d",&n)!=EOF)
        printf("%lld\n",dp[n]);
    return 0;
}


ural 1260. Nudnik Photographer 规律dp