8838: Secret of Chocolate Poles
http://exam.upc.edu.cn/problem.php?id=8838
时间限制: 1 Sec 内存限制: 128 MB
提交: 151 解决: 63
[提交] [状态] [讨论版] [命题人:admin]
题目描述
Wendy, the master of a chocolate shop, is thinking of displaying poles of chocolate disks in the showcase. She can use three kinds of chocolate disks: white thin disks, dark thin disks, and dark thick disks. The thin disks are 1 cm thick, and the thick disks are k cm thick. Disks will be piled in glass cylinders.
Each pole should satisfy the following conditions for her secret mission, which we cannot tell.
• A pole should consist of at least one disk.
• The total thickness of disks in a pole should be less than or equal to l cm.
• The top disk and the bottom disk of a pole should be dark.
• A disk directly upon a white disk should be dark and vice versa.
As examples, six side views of poles are drawn in Figure A.1. These are the only possible side views she can make when l = 5 and k = 3.
Figure A.1. Six chocolate poles corresponding to Sample Input 1
Your task is to count the number of distinct side views she can make for given l and k to help her accomplish her secret mission.
输入
The input consists of a single test case in the following format.
l k
Here, the maximum possible total thickness of disks in a pole is l cm, and the thickness of the thick disks is k cm. l and k are integers satisfying 1 ≤ l ≤ 100 and 2 ≤ k ≤ 10.
输出
Output the number of possible distinct patterns.
样例输入
5 3
样例输出
6
题意:有三种圆板:1 cm黑板,1 cm白板,k cm白板。有一个高 l cm的容器,满足:
(1)至少1个板在容器里。
(2)总长度 <= l (即可不装满容器)。
(3)顶部和底部是黑板。
(4)白板和黑板必须交替放。
问你有几种放法。
思路:用dp。
dp[i][j]表示高度为i,颜色为j时的方案数。j=0,1。 0白 1黑
因为没装满容器时也算,所以每个高度都要加上。
代码:
#include<iostream>
#include<cstring>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<cstdio>
#define inf 0x3f3f3f3f
using namespace std;
long long dp[105][2]; //1黑 0白
int main()
{
int l,k;
cin>>l>>k;
memset(dp,0,sizeof(dp));
dp[0][0]=1;
for(int i=1;i<=l;i++)
{
dp[i][0]=dp[i-1][1];
dp[i][1]=i>=k?dp[i-1][0]+dp[i-k][0]:dp[i-1][0];
}
long long sum=0;
for(int i=1;i<=l;i++)
sum+=dp[i][1];
cout<<sum<<endl;
return 0;
}