链接: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; }
版权声明:本文为博主原创文章,未经博主允许不得转载。