http://poj.org/problem?id=2440
DNA
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3254 | Accepted: 1285 |
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 思路:
这题拿在手里就感觉是找规律,看数据10^8太大,一般暴力方法肯定是超时的,而且结果mod2005,于是就想到应该是个找规律的题
先写个暴力代码打印一下前面几项:
输入 输出 规 律
1 ---> 2 1*2
2 ---> 4 2*2
3 ---> 6 2*3
4 ---> 9 3*3
5 ---> 15 3*5
6 ---> 25 5*5
7 ---> 40 5*8
8 ---> 64 8*8
9 ---> 104 8*13
10 ---> 169 13*13
不难发现规律为斐波拉契数列:1 2 3 5 8 13 21 。。。。中间的相邻两项 或者 和本身的乘积
因为数据在 L (1 <= L <= 10 ^ 8) 斐波拉契数将超出整型范围
利用mod的性质:
(m*n)%k == ((m%k)*(n%k))%k
将斐波拉契简化,可是数列还是过长,打表无法打这么长,然后就想,mod之后应该可能出现循环的
接下来的任务就是找到这个循环节:
f[0]=1;
f[1]=2;
for(i=2;i<100000;i++)
{
f[i] = (f[i-1] + f[i-2])%2005;
if(f[i]==2&&f[i-1]==1) // 找到循环节 再次出现 1 2 3 5 8 。。。 的时候
break;
}
cout<<i-1<<endl;
得到循环点为 200 的时候
#include <iostream>
#include <stdio.h>
#include <string.h> using namespace std;
int f[]; int main()
{
int n,i;
f[] = ;
f[] = ;
for(i=;i<;i++) f[i] = (f[i-] + f[i-])%; //知道循环点在200处,故打表210就够了
while(~scanf("%d",&n))
{
printf("%d\n",(f[(n/+n%)%]*f[n/%])%); //利用找到的规律以及mod的性质求解
}
return ;
}