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

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





本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5064183.html,如需转载请自行联系原作者

相关文章
|
IDE API 开发工具
FastAPI快速搭建一个博客系统
FastAPI快速搭建一个博客系统
1022 0
FastAPI快速搭建一个博客系统
|
XML 监控 Android开发
Android Studio App开发入门之文本输入EditText的讲解及使用(附源码 包括编辑框、焦点变更监听器、文本变化监听器 )
Android Studio App开发入门之文本输入EditText的讲解及使用(附源码 包括编辑框、焦点变更监听器、文本变化监听器 )
734 0
|
5月前
|
消息中间件 存储 缓存
RocketMQ原理—4.消息读写的性能优化
本文详细解析了RocketMQ消息队列的核心原理与性能优化机制,涵盖Producer消息分发、Broker高并发写入、Consumer拉取消息流程等内容。重点探讨了基于队列的消息分发、Hash有序分发、CommitLog内存写入优化、ConsumeQueue物理存储设计等关键技术点。同时分析了数据丢失场景及解决方案,如同步刷盘与JVM OffHeap缓存分离策略,并总结了写入与读取流程的性能优化方法,为理解和优化分布式消息系统提供了全面指导。
RocketMQ原理—4.消息读写的性能优化
|
6月前
|
存储 算法 Java
G1原理—4.G1垃圾回收的过程之Young GC
本文详细解析了G1垃圾回收器中YGC(Young Generation Collection)的完整流程,包括并行与串行处理阶段。内容涵盖YGC相关参数设置、YGC与Mixed GC及FGC的关系、新生代垃圾回收的具体步骤(如标记存活对象、复制到Survivor区、动态调整Region数量等),以及并行阶段的多线程操作和串行阶段的关键任务(如处理软引用、整理卡表、重构RSet)。
G1原理—4.G1垃圾回收的过程之Young GC
|
存储 DataWorks 安全
DataWorks产品使用合集之数据视图如何创建
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
8月前
|
监控 架构师 安全
企业架构(EA)项目开发综合指南
企业架构(EA)是一种全面的方法,用于对齐企业的业务目标与其 IT 战略和资源。EA 涵盖了企业的各个层面,包括业务流程、信息流、应用系统和技术基础设施。本指南将详细探讨 EA 项目开发的关键步骤、[EA](https://www.visual-paradigm.com/features/enterprise-architecture-diagram-tool/) 与 TOGAF、ArchiMate 以及其他建模图(如 BPMN 和 UML)之间的关系,以及推荐 Visual Paradigm 作为 EA 团队的最佳解决方案。
359 3
|
9月前
|
弹性计算 人工智能 自然语言处理
云工开物:阿里云弹性计算走进高校第2期,与北京大学研一学生共探AI时代下的应用创新
阿里云高校合作、弹性计算团队​于北京大学,开展了第2届​【弹性计算进校园】​交流活动。
|
小程序 开发者
【微信小程序-原生开发】实用教程05-首页(含自定义调试模式、插入图片、图文排版、底部留白、添加本地图片)
【微信小程序-原生开发】实用教程05-首页(含自定义调试模式、插入图片、图文排版、底部留白、添加本地图片)
245 0
|
网络安全 云计算
在哪找出来阿里云服务器代码
本文介绍了在阿里云上查找服务器代码的方法,包括通过控制台搜索实例并进入详情页查找相关信息,利用 `ssh` 和 `cat` 等命令行工具远程访问和查看文件,以及联系阿里云技术支持获得进一步帮助,助您轻松定位和操作服务器代码。
248 2
|
机器学习/深度学习 人工智能 算法
【服装识别系统】图像识别+Python+人工智能+深度学习+算法模型+TensorFlow
服装识别系统,本系统作为图像识别方面的一个典型应用,使用Python作为主要编程语言,并通过TensorFlow搭建ResNet50卷积神经算法网络模型,通过对18种不同的服装('黑色连衣裙', '黑色衬衫', '黑色鞋子', '黑色短裤', '蓝色连衣裙', '蓝色衬衫', '蓝色鞋子', '蓝色短裤', '棕色鞋子', '棕色短裤', '绿色衬衫', '绿色鞋子', '绿色短裤', '红色连衣裙', '红色鞋子', '白色连衣裙', '白色鞋子', '白色短裤')数据集进行训练,最后得到一个识别精度较高的H5格式模型文件,然后基于Django搭建Web网页端可视化操作界面,实现用户在界面中
527 1
【服装识别系统】图像识别+Python+人工智能+深度学习+算法模型+TensorFlow