Flag(Ural_1225)
来源:互联网 发布:网络女主播被骗26万 编辑:程序博客网 时间:2024/06/05 23:57
On the Day of the Flag of Russia a shop-owner decided to decorate the show-window of his shop with textile stripes of white, blue and red colors. He wants to satisfy the following conditions:
Stripes of the same color cannot be placed next to each other.
A blue stripe must always be placed between a white and a red or between a red and a white one.
Determine the number of the ways to fulfill his wish.
Example. For N = 3 result is following:
Problem illustration
Input
N, the number of the stripes, 1 ≤ N ≤ 45.
Output
M, the number of the ways to decorate the shop-window.
Sample input output
3
4
Problem Source
2002-2003 ACM Central Region of Russia Quarterfinal Programming Contest, Rybinsk, October 2002
Ural动规专题第一道小题,一直不懂是怎么推出来的,直到又把动规学了一遍加上看了别人的题解才豁然开朗。
用dp[i]代表n = i时的方法数,那么我们怎样可以得到它呢?根据题意,每种方法最后的颜色必定是红色或白色(因为蓝色只能放在它们俩中间),所以,我们可以把n = i - 1时的每种方法最后加上红色或白色来得到n = i的一部分方法数,即dp[i]有一部分是dp[i-1]。除了可以红白搭和白红搭,还有一种就是把蓝色放在红白或白红的中间,这种也很简单,我们可以在n = i - 2状态的最后添加两种颜色,蓝色+红/白色,而n = i - 2状态下的方法数就是dp[i - 2],所以总结起来dp[i]就等于dp[i-1] + dp[i-2];(弱语言障碍只能解释成这样了= =下面上代码)
代码
#include <iostream>#include <cstdio>#include <cstring>#define LL long longusing namespace std;int main(){ LL ans[50] = {0, 2, 2}; for(int i = 3; i < 46; i++) ans[i] = ans[i - 1] + ans[i - 2]; int n; scanf("%d", &n); printf("%lld\n", ans[n]); return 0;}
- Flag(Ural_1225)
- FLag
- Flag
- flag
- flag
- flag
- FLAG
- Flag
- Flag
- FLAG
- Flag
- flag
- Flag
- flag
- flag
- FLAG
- flag
- flag
- Oracle--分区表(范围分区、Hash分区、等)
- 一致性哈希算法(consistent hashing)
- Linphone架构及功能分析
- Hash魔法:一致性 hash 算法
- Scala教程:简单构建工具SBT
- Flag(Ural_1225)
- 在Windows上build Spark
- day06 web和http:Tomcat的认识,了解请求消息头和响应消息头
- Sbt仓库设置
- 去除ViewPager界面切换动画、禁止滑动
- spark1.4 Windows local调试环境搭建总结
- Kafka Consumer java api 配置
- Win7下安装Spark
- 一致性HASH算法的JAVA实现