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. Spring 4.2 annotation event Publisher/Listener

    http://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2 Better applicatio ...

  2. S2-020 Struts ClassLoader Manipulation安全限制绕过(CVE-2014-0094)

    受影响系统: Apache Group Struts 2.x 描述: ----------------------------------------------------------------- ...

  3. mysql:错误日志log_error:

    1.启动错误日志: 在不同的情况下,错误日志会记录在不同的位置,如果没有配置文件指定文件名,则默认为hostname.err 在mysql5.6的RPM发布的方式中,错误日志被放在/var/log/m ...

  4. Redis命令参考(Keys & String)

    r = redis.Redis(ip, port, index) 如此实例化一个redis对象,index取值0-15,一个redis对象有16个库. Keys 函数 功能 返回值 备注 keys(s ...

  5. C#扫盲之:==/Equals /ReferenceEquals 异同的总结,相等性你真的知道吗?

    1.前言 == Equals ReferenceEquals 三个相等性测试,是.NET提供给程序员使用的三个方法,他们之间有什么联系和区别,你真的仔细研究过?虽然之前也多多少少知道一点,但是有时候又 ...

  6. C#下利用封包、拆包原理解决Socket粘包、半包问题(新手篇)

    介于网络上充斥着大量的含糊其辞的Socket初级教程,扰乱着新手的学习方向,我来扼要的教一下新手应该怎么合理的处理Socket这个玩意儿. 一般来说,教你C#下Socket编程的老师,很少会教你如何解 ...

  7. 汇总文件数据 VBA

    1 读取30个文件的数据信息 2 根据4个key值,判断累计数据 3 做sum , avg Sub 月汇总() Dim MyPath, MyName, AWbName Dim Wb As Workbo ...

  8. 如何在ANDROID JNI 的C++中打Log

    http://blog.csdn.net/pkigavin/article/details/8583537 最近在研究Android 2.3.3源代码的C/C++层,需要对代码进行一些调试,但是奇怪的 ...

  9. (十)Hibernate 查询方式

     所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 第一节:Hibernate 查询方式简介 1,导航对象图查询方式: 2 ...

  10. Objective-C 学习笔记(Day 2)

    ------------------------------------------- 如何根据题目准确完整清晰的声明一个类并实现给定的行为 /*  //下面这个程序教大家如何根据题目去声明一个类,并 ...