Poj 1205 water treatment plants (recurrence)

Source: Internet
Author: User

Build a riverbank sewage treatment system. The riverbank has n cities. Each city can handle the sewage by itself. V. It can also transmit the sewage to adjacent cities for treatment. in this case, other cities at either end are valid and cannot be transferred to non-existent cities.

D [I] indicates the number of processing methods used in the first city.

1. V self-processing of their own and the former I-1 city of the treatment method has nothing to do with d [I-1] Methods

2. <to the left of the city to deal with the former I-1 city of the treatment method has nothing to do with their own sewage to the I-1 city on the line of d [I-1] A Way

3.> v The left side of the sewage and their processing together at this time the city of I-1 can be transferred to the right if this happens, then the I-1 city is certainly not to the left but the former I-2 there is no impact on the city's treatment method, so the I-1 city to the left of the method D [I-2] And then the I-1 city not to the left there is d [I-1]-d [I-2] method

So there is a recursive formula D [I] = 3 * d [I-1]-d [I-2];

Java is used to process large numbers quickly.

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 ++ code without a large number Template

#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 neighbor, 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 neighbor, 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 neighbors 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 cocould 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 abve.

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 shoshould 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

2320

Sample output

38102334155


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.