题目链接:
ACdream 1667
题解:
很显然是
dp题
。
当时状态可以由左边的跑道,右边的跑道,跑道不变这三种状态转换而来。
设
dp[i][j]
表示人跑了
i
步跑到
那么,
dp[i][j]=dp[i−1][j+1]+dp[i−1][j]+dp[i−1][j−1]
.
注意:这道题大数啊…套个C++大数类,或者上Java吧…我一般不写Java…
WA代码:
/*
* this code is made by LzyRapx
* Problem: 1667
* Verdict: Wrong Answer
* Submission Date: 2017-06-04 16:24:13
* Time: 32MS
* Memory: 13564KB
*/
#include <bits/stdc++.h>
using namespace std;
long long dp[1234][1234];
int main()
{
int n;
dp[0][0]=1;
for(int i=1;i<=1001;i++)
{
for(int j=1;j<=1001;j++)
{
dp[i][j] = dp[i-1][j+1]+(dp[i-1][j]+dp[i-1][j-1]);
}
}
while(cin>>n)
{
cout<<dp[n+1][1]<<endl;
}
return 0;
}
大数C++代码:
/*
* this code is made by LzyRapx
* Problem: 1667
* Verdict: Accepted
* Submission Date: 2017-06-04 19:56:33
* Time: 1252MS
* Memory: 3332KB
*/
#include <bits/stdc++.h>
using namespace std;
const int N=1100,M=1009;
const int MX = 150;
const int MAXN = 9999;
const int DLEN = 4;
class Big {
public:
int a[MX], len;
Big(const int b = 0) {
int c, d = b;
len = 0;
memset(a, 0, sizeof(a));
while(d > MAXN) {
c = d - (d / (MAXN + 1)) * (MAXN + 1);
d = d / (MAXN + 1);
a[len++] = c;
}
a[len++] = d;
}
Big(const char *s) {
int t, k, index, L, i;
memset(a, 0, sizeof(a));
L = strlen(s);
len = L / DLEN;
if(L % DLEN) len++;
index = 0;
for(i = L - 1; i >= 0; i -= DLEN) {
t = 0;
k = i - DLEN + 1;
if(k < 0) k = 0;
for(int j = k; j <= i; j++) {
t = t * 10 + s[j] - '0';
}
a[index++] = t;
}
}
Big operator/(const int &b)const { //大数除以整数
Big ret;
int i, down = 0;
for(i = len - 1; i >= 0; i--) {
ret.a[i] = (a[i] + down * (MAXN + 1)) / b;
down = a[i] + down * (MAXN + 1) - ret.a[i] * b;
}
ret.len = len;
while(ret.a[ret.len - 1] == 0 && ret.len > 1) ret.len--;
return ret;
}
bool operator>(const Big &T)const {
int ln;
if(len > T.len) return true;
else if(len == T.len) {
ln = len - 1;
while(a[ln] == T.a[ln] && ln >= 0) ln--;
if(ln >= 0 && a[ln] > T.a[ln]) return true;
else return false;
} else return false;
}
Big operator+(const Big &T)const {
Big t(*this);
int i, big;
big = T.len > len ? T.len : len;
for(i = 0; i < big; i++) {
t.a[i] += T.a[i];
if(t.a[i] > MAXN) {
t.a[i + 1]++;
t.a[i] -= MAXN + 1;
}
}
if(t.a[big] != 0) t.len = big + 1;
else t.len = big;
return t;
}
Big operator-(const Big &T)const {
int i, j, big;
bool flag;
Big t1, t2;
if(*this > T) {
t1 = *this;
t2 = T;
flag = 0;
} else {
t1 = T;
t2 = *this;
flag = 1;
}
big = t1.len;
for(i = 0; i < big; i++) {
if(t1.a[i] < t2.a[i]) {
j = i + 1;
while(t1.a[j] == 0) j++;
t1.a[j--]--;
while(j > i) t1.a[j--] += MAXN;
t1.a[i] += MAXN + 1 - t2.a[i];
} else t1.a[i] -= t2.a[i];
}
t1.len = big;
while(t1.a[t1.len - 1] == 0 && t1.len > 1) {
t1.len--;
big--;
}
if(flag) t1.a[big - 1] = 0 - t1.a[big - 1];
return t1;
}
int operator%(const int &b)const { //大数对余数取模
int i, d = 0;
for(i = len - 1; i >= 0; i--) {
d = ((d * (MAXN + 1)) % b + a[i]) % b;
}
return d;
}
Big operator*(const Big &T) const {
Big ret;
int i, j, up, temp, temp1;
for(i = 0; i < len; i++) {
up = 0;
for(j = 0; j < T.len; j++) {
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if(temp > MAXN) {
temp1 = temp - temp / (MAXN + 1) * (MAXN + 1);
up = temp / (MAXN + 1);
ret.a[i + j] = temp1;
} else {
up = 0;
ret.a[i + j] = temp;
}
}
if(up != 0) {
ret.a[i + j] = up;
}
}
ret.len = i + j;
while(ret.a[ret.len - 1] == 0 && ret.len > 1) ret.len--;
return ret;
}
void print() {
printf("%d", a[len - 1]);
for(int i = len - 2; i >= 0; i--) printf("%04d", a[i]);
puts("");
}
};
int main()
{
Big one("1");
Big zero("0");
//注意:内存不够,要用滚动数组
Big dp[3][M];
dp[1][1]=one;
dp[1][2]=one;
dp[0][1]=dp[1][1];
for(int i=2;i<M;i++)
{
for(int j=1;j<M;j++)
{
if(j>1)dp[2][j] = dp[1][j - 1]; //左边
else dp[2][j]=zero;
dp[2][j] = dp[2][j] + dp[1][j]; //不变
if(j+1<1005) dp[2][j] = dp[2][j] + dp[1][j + 1]; //r右边
}
for(int j=1;j<=M;j++) dp[1][j]=dp[2][j];
dp[0][i]=dp[1][1];
}
int n;
while(cin>>n)
{
dp[0][n].print();
}
return 0;
}