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

poj 2440 (找递推公式)的更多相关文章

  1. poj 3304 找一条直线穿过所有线段

    题目链接:http://poj.org/problem?id=3304 #include<cstdio> #include<cstring> #include<cmath ...

  2. poj 3372(找规律)

    Candy Distribution Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6033   Accepted: 335 ...

  3. POJ 2096 找bug 期望dp

    题目大意: 一个人受雇于某公司要找出某个软件的bugs和subcomponents,这个软件一共有n个bugs和s个subcomponents,每次他都能同时随机发现1个bug和1个subcompon ...

  4. poj 1655 找树的重心

    树形DP 求树的重心,即选择一个结点删去,使得分出的 若干棵树的结点数 的最大值最小 #include<map> #include<set> #include<cmath ...

  5. 【noi 2.6_9270】&【poj 2440】DNA(DP)

    题意:问长度为L的所有01串中,有多少个不包含"101"和"111"的串. 解法:f[i][j]表示长度为i的01串中,结尾2位的十进制数是j的合法串的个数.那 ...

  6. POJ 3207 Ikki&#39;s Story IV - Panda&#39;s Trick (2-SAT)

    职务地址:id=3207">POJ 3207 找好矛盾关系.矛盾关系是(2,5)和(3,6)这两个仅仅能一个在外边,一个在里边.利用这个矛盾关系来建图. 能够用在外边和里边来当1和0, ...

  7. [LeetCode] Find the Derangement of An Array 找数组的错排

    In combinatorial mathematics, a derangement is a permutation of the elements of a set, such that no ...

  8. 【转载】ACM总结——dp专辑

    感谢博主——      http://blog.csdn.net/cc_again?viewmode=list       ----------  Accagain  2014年5月15日 动态规划一 ...

  9. 【DP专辑】ACM动态规划总结

    转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 ...

随机推荐

  1. 【转载】Hadoop和大数据:60款顶级大数据开源工具

    一.Hadoop相关工具 1. Hadoop Apache的Hadoop项目已几乎与大数据划上了等号.它不断壮大起来,已成为一个完整的生态系统,众多开源工具面向高度扩展的分布式计算. 支持的操作系统: ...

  2. MATLAB 误差函数erf(x)

    误差函数: 1.误差函数定义为:   它的性质如下: 2 互补误差函数定义为: 它具有如下性质: 下表给出了误差函数的部分数值: 0.00 0.00000 0.05 0.05637 0.10 0.11 ...

  3. 如何用java实现使用电子邮件控制你的电脑

    上两天看到一篇文章,用python实现电子邮件控制电脑的有趣的小程序 python 实现微信控制电脑     python版的视频教程 但是苦于自己没接触过python于是想到能不能用java实现,于 ...

  4. android loadlibrary 更改libPath 路径,指定路径加载.so

    http://www.jianshu.com/p/f751be55d1fb 字数549 阅读177 评论0 喜欢0 需求很简单 ,就是加载指定文件夹下的.so. 原因:android在程序运行的状态下 ...

  5. 华为在线OJ_找7

    描述 输出7有关数字的个数,包括7的倍数,还有包含7的数字(如17,27,37...70,71,72,73...)的个数 知识点 循环 运行时间限制 0M 内存限制 0 输入 一个正整数N.(N不大于 ...

  6. ubuntu中安装monodevelop

    sudo apt-get install monoDevelop sudo apt-get install build-essentialsudo apt-get install mono-devel ...

  7. 浅析foreach原理

    在日常开发工作中,我们发现很多对象都能通过foreach来遍历,比如HashTable.Dictionary.数组等数据类型.那为何这些对象能通过foreach来遍历呢?如果写一个普通的Person类 ...

  8. ios Swift 资源池

    Swift入门教程: http://www.cocoachina.com/applenews/devnews/2014/0604/8661.html Swift视频教程: http://www.coc ...

  9. JQuery 之 Ajax 异步和同步浅谈

    Ajax 同步和异步的区别 同步是当 JS 代码加载到当前 Ajax 的时候会把页面里所有的代码停止加载,页面出现假死状态:当这个 Ajax 执行完毕后才会继续运行其他代码此时页面假死状态才会解除.反 ...

  10. Brackets 配置

    插件 Brackets Icons  左侧导航的文件图标 FuncDocr  注释工具 QuickDocsJS  js帮助文档 Beautify  格式化代码 Brackets Git  git支持 ...