POJ 2440 DNA

链接:http://poj.org/problem?id=2440

DNA

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 3498 Accepted: 1375

Description

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?

Input

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

Output

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

Sample Input

4

Sample Output

9

Source

POJ Monthly,Static

大意——一种病毒入侵一个星球,这种病毒有两种DNA,分别是111和101,这个星球上的生物有L位的基因,也是由0和1组成的。如果生物的基因中含有与病毒相同的子串,就会遭受影响。问:给定一个L,求解有多少种可能的基因组合不会遭受影响,结果对2005取模。

思路——L的取值比较大,最大为10^8,但是结果要对2005取模,说明必定存在循环节,而且必有递推关系。注意到:f(0)=1,f(1)=2,f(2)=4,f(3)=6,f(4)=9,f(5)=15,f(6)=25,f(7)=40,从而可以得到f(n)=f(n-1)+f(n-3)+f(n-4),n>3。再运用递推关系找到循环节即可解决。

复杂度分析——时间复杂度:O(n),空间复杂度:O(n)

附上AC代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <iomanip>
#include <ctime>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
typedef unsigned int UI;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
const double PI = 3.14159265;
const double E = 2.71828182846;
const int mod = 2005;
const int look = 1000;
int fib[look] = {1, 2, 4, 6, 9, 15};

int main()
{
	ios::sync_with_stdio(false);
	int loop;
	for (int i=6; i<=look; i++)
	{
		fib[i] = (fib[i-1]+fib[i-3]+fib[i-4])%2005;
		if (fib[i] == 2 && fib[i-1] == 1)
		{
			loop = i-1;
			break;
		}
	}
	int L;
	while (cin >> L)
	{
		cout << fib[L%loop] << endl;
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-07 00:08:38

POJ 2440 DNA的相关文章

POJ 1007:DNA排序

AC CODE: 1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 8 // 得到数据 9 int n = input.nextInt(); 10 int m = input.nextInt(); 11 String temp = input.nextLine();

poj 2778 DNA Sequence(AC自动机+矩阵快速幂)

题目链接:poj 2778 DNA Sequence 题目大意:给定一些含有疾病的DNA序列,现在给定DNA长度,问有多少种不同的DNA序列是健康的. 解题思路:对DNA片段建立AC自动机,因为最多10个串,每个串最长为10,所以最多可能有100个节点,在长度为n时 以每个节点终止的健康字符串个数形成一个状态集,通过AC自动机形成的边可以推导出n+1的状态集,走到单词节点是 非法的,所以同样的我们可以先走到单词节点,但是从单词节点不向后转移.这样可以构造一个矩阵,剩下的就是矩阵 快速幂.注意的一

POJ 3691 DNA repair 基于AC自动机的DP

dp[i][j] 表示长度为 i 的前缀到达第 j 个节点的最小更改数目. 很显然有dp[0][0] = 0; dp[ i ][ j ] = min(dp[ i ][ j ],dp[i-1][k] + (j == k ? 0 : 1)),当且仅当j,k满足下列条件时. j 不为某条模式串的末节点 且 j 到 root 的由失败指针组成的路径上无末节点. j 是k的儿子节点 或者 j 的父节点可由 k 沿着失败指针找到. #include <algorithm> #include <ios

[poj 3691]DNA repair

好久没刷 poj 了,今天练习 AC 自动机时去水了一发喵~ 在 poj 上 A 题的感觉并没有 BZOJ 上那么愉悦,准确的说是痛不欲生 真是应了那句老话,你再慢也有比你慢的,你再快也有比你快的…… 跪求那些 0ms 的代码啊,还有那么多人都只跑了 32ms 啊!! 果然还是我太弱了吗?一定是我还太弱了 TAT 一道裸裸的 AC 自动机上 dp 令 dp[i][j] 表示母串的前 i 个字母遍历 AC 自动机,使之到达 j 节点,至少要修改多少个字母 dp[i+1][k]=min(dp[i+1

POJ POJ 2778 DNA Sequence AC自动机 + 矩阵快速幂

首先建立Trie和失败指针,然后你会发现对于每个节点 i 匹配AGCT时只有以下几种情况: i 节点有关于当前字符的儿子节点 j 且安全,则i 到 j找到一条长度为 1的路. i 节点有关于当前字符的儿子节点 j 且 不安全,则i 到 j没有路. i 节点没有关于当前字符的儿子节点 但是能通过失败指针找到一个安全的节点j,那么 i 到 j 找到一条长度为1的路. 关于节点安全的定义: 当前节点不是末节点且当前节点由失败指针指回跟节点的路径上不存在不安全节点,那么这个节点就是安全节点. 然后问题就

poj 4086:DNA排序

poj 4086:DNA排序 题目 描述 现在有一些长度相等的DNA串(只由ACGT四个字母组成),请将它们按照逆序对的数量多少排序. 逆序对指的是字符串A中的两个字符A[i].A[j],具有i < j 且 A[i] > A[j] 的性质.如字符串"ATCG"中,T和C是一个逆序对,T和G是另一个逆序对,这个字符串的逆序对数为2. 输入 第1行:两个整数n和m,n(0<n<=50)表示字符串长度,m(0<m<=100)表示字符串数量 第2至m+1行:

【noi 2.6_9270】&amp;【poj 2440】DNA(DP)

题意:问长度为L的所有01串中,有多少个不包含"101"和"111"的串. 解法:f[i][j]表示长度为i的01串中,结尾2位的十进制数是j的合法串的个数.那么,便由f[i-1][ ]逐个推出. 1 #include<cstdio> 2 #include<cstdlib> 3 #include<cstring> 4 #include<iostream> 5 using namespace std; 6 #define

POJ 2778 DNA Sequence

DNA Sequence Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 277864-bit integer IO format: %lld      Java class name: Main It's well known that DNA Sequence is a sequence only contains A, C, T and G, and it's

POJ 2778 DNA Sequence(AC自动机+矩阵快速幂)

题目链接:http://poj.org/problem?id=2778 题意:有m种DNA序列是有疾病的,问有多少种长度为n的DNA序列不包含任何一种有疾病的DNA序列.(仅含A,T,C,G四个字符) 思路:Trie图的状态转移,用矩阵mat[i][j]来表示从结点i到j只走一步有几种走法,那么mat的n次幂就表示从结点i到j走n步有几种走法,题目要求解的就是从头节点走n步且不包含危险结点的走法. mat = mat^n   ans = (mat[0][0] + mat[0][1] + ...