POJ 1205 Water Treatment Plants(递推)
题意 建设一条河岸的污水处理系统 河岸有n个城市 每一个城市都能够自己处理污水 V 也能够把污水传到相邻的城市处理 >或< 除了你传给我我也传给你这样的情况 其他都是合法的 两端的城市不能传到不存在的城市
令d[i]表示有i个城市时的处理方法数 最后一个城市的处理方法有
1.V 自己处理自己的 与前i-1个城市的处理方法无关 有d[i-1]种方法
2.< 给左边的城市去处理 也与前i-1个城市的处理方法无关 把自己的污水给第i-1个城市即可了 有d[i-1]种方法
3.>V 左边有污水传过来 和自己的一起处理 这时第i-1个城市能够向右传了 假设这样的情况发生的话 那么第i-1个城市肯定不可能是向左传的 但前i-2个城市的处理方法没有影响 所以第i-1个城市向左传的方法有d[i-2]种 然后第i-1个城市不向左传就有d[i-1]-d[i-2]种方法了
所以有递推公式d[i]=3*d[i-1]-d[i-2];
增长速度非常快要用大数处理 大数就用java了
import java.util.*;
import java.math.*; public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
BigInteger d[] = new BigInteger[105];
d[1] = BigInteger.ONE;
d[2] = BigInteger.valueOf(3);
for (int i = 3; i <= 100; ++i)
d[i] = d[i - 1].multiply(d[2]).subtract(d[i - 2]);
while (in.hasNext())
System.out.println(d[in.nextInt()]);
in.close();
}
<span style="font-family:Microsoft YaHei;">}</span>
还有没加大数模版的c++代码
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 105;
int main()
{
int d[N] = {0, 1, 3}, n;
for (int i = 3; i <= 100; ++i)
d[i] = 3 * d[i - 1] - d[i - 2];
while (~scanf ("%d", &n))
printf ("%d\n", d[n]);
return 0;
}
Description
comming from cities before being discharged into the river.
As part of a coordinated plan, a pipeline is setup in order to connect cities to the sewage treatment plants distributed along the river. It is more efficient to have treatment plants running at maximum capacity and less-used ones switched off for a period.
So, each city has its own treatment plant by the river and also a pipe to its neighbouring city upstream and a pipe to the next city downstream along the riverside. At each city's treatment plant there are three choices:
- either process any water it may receive from one neighbouring city, together with its own dirty water, discharging the cleaned-up water into the river;
- or send its own dirty water, plus any from its downstream neighbour, along to the upstream neighbouring city's treatment plant (provided that city is not already using the pipe to send it's dirty water
downstream); - or send its own dirty water, plus any from the upstream neighbour, to the downstream neighbouring city's plant, if the pipe is not being used.
The choices above ensure that:
every city must have its water treated somewhere and
at least one city must discharge the cleaned water into the river.
Let's represent a city discharging water into the river as "V" (a downwards flow), passing water onto its neighbours as ">" (to the next city on its right) or else "<" (to the left). When we have several cities along the river bank, we assign a symbol to each
(V, < or >) and list the cities symbols in order. For example, two cities, A and B, can
each treat their own sewage and each discharges clean water into the river. So A's action is denoted V as is B's and we write "VV" ;
or else city A can send its sewage along the pipe (to the right) to B for treatment and discharge, denoted ">V";
or else city B can send its sewage to (the left to) A, which treats it with its own dirty water and discharges (V) the cleaned water into the river. So A discharges (V) and B passes water to the left (<), and we denote this situation as "V<".
We could not have "><" since this means A sends its water to B and B sends its own to A, so both are using the same pipe and this is not allowed. Similarly "<<" is not possible since A's "<" means it sends its water to a non-existent city on its left.
So we have just 3 possible set-ups that fit the conditions:
A B A > B A < B V V V V RIVER~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~RIVER "VV" ">V" "V<"
If we now consider three cities, we can determine 8 possible set-ups.
Your task is to produce a program that given the number of cities NC (or treatment plants) in the river bank, determines the number of possible set-ups, NS, that can be made according to the rules define above.
You need to be careful with your design as the number of cities can be as large as 100.
Input
Output
input line.
Sample Input
2
3
20
Sample Output
3
8
102334155
POJ 1205 Water Treatment Plants(递推)的更多相关文章
- poj 3744 Scout YYF I(递推求期望)
poj 3744 Scout YYF I(递推求期望) 题链 题意:给出n个坑,一个人可能以p的概率一步一步地走,或者以1-p的概率跳过前面一步,问这个人安全通过的概率 解法: 递推式: 对于每个坑, ...
- POJ 1737 Connected Graph (大数+递推)
题目链接: http://poj.org/problem?id=1737 题意: 求 \(n\) 个点的无向简单(无重边无自环)连通图的个数.\((n<=50)\) 题解: 这题你甚至能OEIS ...
- POJ 1664 放苹果 (递推思想)
原题链接:http://poj.org/problem?id=1664 思路:苹果m个,盘子n个.假设 f ( m , n ) 代表 m 个苹果,n个盘子有 f ( m , n ) 种放法. 根据 n ...
- 【POJ】2229 Sumsets(递推)
Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissions: 20315 Accepted: 7930 Descrip ...
- POJ 3734 Blocks (线性递推)
定义ai表示红色和绿色方块中方块数为偶数的颜色有i个,i = 0,1,2. aij表示刷到第j个方块时的方案数,这是一个线性递推关系. 可以构造递推矩阵A,用矩阵快速幂求解. /*********** ...
- POJ 3046 Ant Counting(递推,和号优化)
计数类的问题,要求不重复,把每种物品单独考虑. 将和号递推可以把转移优化O(1). f[i = 第i种物品][j = 总数量为j] = 方案数 f[i][j] = sigma{f[i-1][j-k], ...
- poj 1205 :Water Treatment Plants (DP+高精度)
题意:有n个城市,它们由一个污水处理系统连接着,每个城市可以选择 1.将左边城市过来的污水和右边城市过来的污水连同本身的污水排到河里 >V< 2.将左边来的污水连同自己的污水排到右边 ...
- POJ 3597 种类数 数学+递推
http://poj.org/problem?id=3597 题目大意:把一个正多边形分成数个三角形或者四边形,问有多少种方案. 思路:http://www.cnblogs.com/Ritchie/p ...
- poj 1664 放苹果(递推)
题目链接:http://poj.org/problem? id=1664 放苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions ...
随机推荐
- HTML5,微信开发原码社区
HTML5开发助手,快速查看HTML及javascript接口文档 http://www.9miao.com/thread-60966-1-1.html 简洁的手机wap公司产品展示网站模板下载htm ...
- Java加入背景音乐
近期有几个师妹找我给她们的Java期末作业加入背景音乐,非常久不琢磨Java的我花费整晚才搞定,羞愧.在博客中记录下来.警示自己.也帮助一下大家. Java中能够通过AudioClip类来实现音乐播放 ...
- 让notepad.exe的utf8不添加BOM
实在是厌烦了notepad的utf8模式了,于是决定修改之,方案如下: 使用任何支持hex模式的编辑器打开%SystemRoot%/system32/notepad.exe查找二进制串56 8D 45 ...
- Eclipse代码字体、颜色美化,更改字体大小、颜色
先看效果: 感觉如何,是否比你的eclipse编辑器显示的代码要漂亮简洁呢?呵呵.这个是我原来ADT Eclipse的效果,现在去下居然更新掉了,找不到了.于是我就参照我原来的配置对这个新的Eclip ...
- Tengine中的proxy_upstream_tries
upsream xxx { server 192.168.100.100; server 192.168.100.101; server 192.168.100.102; } server { loc ...
- [Android学习笔记]Android中多线程开发的一些概念
线程安全: 在多线程的情况下,不会因为线程之间的操作而导致数据错误. 线程同步: 同一个资源,可能在同一时间被多个线程操作,这样会导致数据错误.这是一个现象,也是一个问题,而研究如何解决此类问题的相关 ...
- jquery clone方法
引用自http://www.w3school.com.cn/tiy/t.asp?f=jquery_manipulation_clone <html> <head> <sc ...
- Ubuntu下安装MySQL 5.6.23
Ubuntu下安装MySQL 5.6.23 1.下载相应Linux-generic的源代码包.解压,将解压后的文件夹重命名为mysql.移动到/usr/local文件夹下: tar –xzf mysq ...
- KMP(Knuth-Morris-Pratt)算法
一.朴素匹配算法 也就是暴力匹配算法.设匹配字符串的长度为n,模式串的长度为m,在最坏情况下,朴字符串匹配算法执行时间为O((n - m + 1)m). 假设m = n / 2, 那么该算法的复杂度就 ...
- 程序缩小到托盘后系统就无法关机(解决方案)——处理WM_QUERYENDSESSION消息,并把它标识为处理过了
程序缩小到托盘后系统就无法关机(解决方案) 老帅 程序最小化到托盘后,会出现系统无法关闭的问题,常见于WinXP系统中,这里提供一个解决方案!一.解决 ...