(File IO): input:problem2.in output:problem2.out
時間限制: 1000 ms 空間限制: 262144 KB 具體限制
Goto ProblemSet
題目描述
從原點出發,一步只能向右走、向上走或向左走。恰好走步且不經過已走的點共有多少種走法?
輸入
一個數字
輸出
一個數表示所求答案.
樣例輸入
2
樣例輸出
7
數據範圍限制
解題思路
當確定時,走法的種類是確定的。
設時,走法的種類爲,
則當時,;當時,
所以,多推幾個可以發現遞推關係式:
例如 當時,
當時,
代碼
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
const int INF=12345;
long long a[1100];
int n;
int main()
{
freopen("problem2.in","r",stdin);
freopen("problem2.out","w",stdout);
scanf("%d",&n);
a[0]=1,a[1]=3;
for(int i=2;i<=n;i++)
a[i]=(a[i-1]*2+a[i-2])%INF;
cout<<a[n];
}