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();
	}
}

还有没加大数模版的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;
}

Water Treatment Plants

Description

River polution control is a major challenge that authorities face in order to ensure future clean water supply. Sewage treatment plants are used to clean-up the dirty water
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

The input consists of a sequence of values, one per line, where each value represents the number of cities.

Output

Your output should be a sequence of values, one per line, where each value represents the number of possible set-ups for the corresponding number of cities read in the same
input line.

Sample Input

2
3
20

Sample Output

3
8
102334155

POJ 1205 Water Treatment Plants(递推)

时间: 2024-10-10 11:17:16

POJ 1205 Water Treatment Plants(递推)的相关文章

POJ 1737 Connected Graph (大数+递推)

题目链接: http://poj.org/problem?id=1737 题意: 求 \(n\) 个点的无向简单连通图的个数.\((n<=50)\) 题解: 这题你甚至能OEIS. http://oeis.org/A001187 但不支持这样做.TAT 间接做. 总方案数减去不合法方案. 因为\(n\)个点的完全图有 \(C(n,2)={n(n-1) \over 2}\) 条边,显然就有 \(2^{C(n,2)}\) 种子图,即枚举每条边是否选择. 设$ f[i]$ 表示每个点都和点 \(1\)

poj(1088)——滑雪(经典递推型动归)

题意: 中文题,就是在所有的点中找一个点作为起点,然后叫你找出从起点开始的最长路径是多少. 这里高度必须严格递减. 思路: 一开始我碰到这题时,没有思路,是看题解写的. 但是今天我回头再去看时,发现自己能够独立写出来了,而且和上次的方法不一样.也许这就是进步吧! 其实就是一个递推型动归,如果理解了上一题,那么这题也好做了. 这是第一次写的: #include<stdio.h> #include<string.h> #include<iostream> #include&

POJ 2506 Tiling(高精度+递推)

高精度模版(bin神的) /* * 高精度,支持乘法和加法 */ struct BigInt { const static int mod = 10000; const static int DLEN = 4; int a[600],len; BigInt() { memset(a,0,sizeof(a)); len = 1; } BigInt(int v) { memset(a,0,sizeof(a)); len = 0; do { a[len++] = v%mod; v /= mod; }w

POJ 1664 放苹果 (递推)

题目链接:http://poj.org/problem?id=1664 dp[i][j]表示i个盘放j个苹果的方案数,dp[i][j] 可以由 dp[i - 1][j] 和 dp[i][j - i] 递推而来. 当盘子的个数大于等于苹果的个数: dp[i - 1][j] :i - 1个盘子放j个苹果,说明i个盘子里最少有一个盘子是空的 dp[i][j - i] :i个盘子都放了苹果,说明有j - i个苹果是随便放置的 否则: dp[i][j] = dp[i - 1][j] 然后没有苹果的盘子的方

POJ 3517 And Then There Was One(约瑟夫环-递推or模拟)

POJ 3517 题目: n  k m 数字1到n成环,先叉数字m,往下数k个,直到最后只有一个数字,输出它. 链表模拟: #include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cmath> #include<vector> #incl

POJ 2506 Tiling(递推+大整数加法)

http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 using namespace std; 6 7 int n; 8 char s[255][255]; 9 10

poj 2506 Tiling 递推

题目链接: http://poj.org/problem?id=2506 题目描述: 有2*1和2*2两种瓷片,问铺成2*n的图形有多少种方法? 解题思路: 利用递推思想,2*n可以由2*(n-1)的状态加上一块竖放2*1的瓷片转移得来,也可以由2*(n-2)的状态加上一块2*2的瓷片或者加上两块横放的2*1的瓷片转移得来. 可得出递推公式:dp[n] = dp[n-1] + dp[n-2]*2: ac秘诀: (1):从输出样例可以看出要用大数来表示,大概需要90位左右. (2):2*0不是零种

[ACM] POJ 2506 Tiling (递推,大数)

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7487   Accepted: 3661 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

poj 2229 【完全背包dp】【递推dp】

poj 2229 Sumsets Time Limit: 2000MS   Memory Limit: 200000K Total Submissions: 21281   Accepted: 8281 Description Farmer John commanded his cows to search for different sets of numbers that sum to a given number. The cows use only numbers that are an