NOI 2.6 动态规划 9270:Pku2440 DNA

题目来源:http://noi.openjudge.cn/ch0206/9270/

9270:Pku2440 DNA

总时间限制: 4000ms  单个测试点时间限制: 1000ms  内存限制: 131072kB

描述

A kind of virus has attacked the Xplanet, and many lives are infected. After weeks of study, The CHO (CreatureHealthy Organization) of X planet finally finds out that this kind of virus hastwo kind of very simple DNA, and can be represented by 101 and 111.Unfortunately, the lives on the planet also have DNA formed by 0s and 1s. If acreature's DNA contains the virus' DNA, it will be affected; otherwise it willnot. Given an integer L, it is clear that there will be 2 ^ L different lives,of which the length of DNA is L. Your job is to find out in the 2 ^ L lives howmany won't be affected? 

输入

The input contains several test cases.For each test case it contains a positive integer L (1 <= L <= 10 ^ 6).The end of input is indicated by end-of-file.

输出

For each test case, output K mod 2005,here K is the number of lives that will not be affected.

样例输入

4

样例输出

9

-----------------------------------------------------

解题思路

对L做动态规划。用4个int分别表示以00/01/10/11结尾的长度为i的DNA序列,找到i和i+1之间的递推关系即可。

-----------------------------------------------------

代码

//9270:Pku2440 DNA
//总时间限制: 4000ms 单个测试点时间限制: 1000ms 内存限制: 131072kB
//描述
//A kind of virus has attacked the X planet, and many lives are infected. After weeks of study, The CHO (Creature Healthy Organization) of X planet finally finds out that this kind of virus has two kind of very simple DNA, and can be represented by 101 and 111. Unfortunately, the lives on the planet also have DNA formed by 0s and 1s. If a creature's DNA contains the virus' DNA, it will be affected; otherwise it will not. Given an integer L, it is clear that there will be 2 ^ L different lives, of which the length of DNA is L. Your job is to find out in the 2 ^ L lives how many won't be affected? 
//
//输入
//The input contains several test cases. For each test case it contains a positive integer L (1 <= L <= 10 ^ 6). The end of input is indicated by end-of-file.
//
//输出
//For each test case, output K mod 2005, here K is the number of lives that will not be affected.
//
//样例输入
//4
//样例输出
//9

#include<iostream>
#include<vector>
using namespace std;

int main()
{
	int n,i,tmp00,tmp01,tmp10,tmp11,a00, a01, a10, a11;
	while (cin >> n)
	{
		a00 = 2;
		a01 = 1;
		a10 = 2;
		a11 = 1;
		if (n==3)
		{
			cout << 6 << endl;
			continue;
		}
		else
		{
			for (i=4; i<=n; i++)
			{
				tmp00 = a00;
				tmp01 = a01;
				tmp10 = a10;
				tmp11 = a11;
				a00 = (tmp00+tmp10)%2005;
				a01 = tmp00%2005;
				a10 = (tmp11+tmp01)%2005;
				a11 = tmp01%2005;
			}
			cout << (a00 + a01 + a10 + a11)%2005 << endl;
		}
	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/80487159
DNA