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. CentOS7 发布 ASP.NET MVC 4 --- mono 4.6.0 + jexus 5.8.1

    yum -y install gcc gcc-c++ yum -y install bison pkgconfig glib2-devel gettext make libpng-devel libj ...

  2. c数组和指针的理解

    #include<stdio.h> int main(void) { ,,,,}; ); printf(,*(p-)); // ] = &a; √ // ] = a; × // ] ...

  3. tomcat服务器使用简介

    tomcat服务器的应用与部署:1:下载tomcat服务器可以到http://tomcat.apache.org/下载apache服务器,左侧有各种版本的服务器,可以根据自己的需要下载,如果是是Lin ...

  4. gitHub添加公钥

    gitHub添加公钥 gitHub是通过SSH公钥连接到现有仓库的 设置SSH Key ssh-keygen -t rsa -C "your_email@example.com"G ...

  5. 关于Eclipse开发插件(三)

    视图之间实现事件监听 两个视图中的组件之间的互动,在开发插件的时候是经常碰到的问题.点击视图1列表的某项时,视图2的文本框显示相应的字符. 第一种主动式: 主动式就是在视图1的代码块中获取对视图2的对 ...

  6. Linux系统下安装ISO文件

    1.在/mnt目录下,创建相应的iso文件夹,例如cd1,cd2. 2.然后,用命令:mount -o loop /home/kinglu/Matlab/matlab1.iso(iso文件路径名) / ...

  7. tcpdf MultiCell line break

    在程序中,我遇到MultiCell中显示三个字符串,开始时 $pdf->MultiCell(63.5, 30, $name."\n".$address."\n&qu ...

  8. ajax_jsonp —— 跨域

    JSONP:原理是script标签 一.抓包 二.不用每次都连接 localhost 的方法   三.抓包后所需的参数 su?:后面跟的是传递过去的参数. cb:是 callback 后面跟的是对返回 ...

  9. hive外部表自动读取文件夹里的数据

    我们在创建表的时候可以指定external关键字创建外部表,外部表对应的文件存储在location指定的目录下,向该目录添加新文件的同时,该表也会读取到该文件(当然文件格式必须跟表定义的一致),删除外 ...

  10. Oracle存储过程学习使用

    存储过程创建语法: create or replace procedure 存储过程名(param1 in type,param2 out type) as 变量1 类型(值范围); 变量2 类型(值 ...