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. Android_listView_BaseAdapter

    layout.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" x ...

  2. EXT.NET学习笔记(一) 下载配置使用

    新公司使用ext.net开发,开始学习该知识: 首先下载ext.net,目前我使用的版本为1.7,该版本免费,基本的功能也够用,使用ext.net进行开发时强烈建议使用VS2015,能便捷的提示,大大 ...

  3. 20160512--hibernate--缓存

    缓存 缓存的作用主要用来提高性能,可以简单的理解成一个Map:使用缓存涉及到三个操作:把数据放入缓存.从缓存中获取数据.删除缓存中的无效数据.   原理模拟分析:(不能运行,只是模拟)(缓存实现复杂, ...

  4. Java语言编写计算器(简单的计算器)

    Java编写的一个简单计算器,本人还比较菜,只能这样了,有点代码冗余,不能连续计算. import javax.swing.*; import java.awt.*; import java.awt. ...

  5. Android四大组件之BroadcastReceiver

    什么是BroadcastReceiver? BroadcastReceiver也就是“广播接收者”的意思,顾名思义,它就是用来接收来自系统和应用中的广播. 在Android系统中,广播体现在方方面面, ...

  6. SSH无密码登陆问题解决

    转载 http://my.oschina.net/hunzi/blog/10687 安装好Cygwin后,SSH需要设置为无密码登陆, 首先查看是ssh还是ssh2:ls -l `which ssh` ...

  7. ExtJs owner.componentLayoutCounter问题解

    owner.componentLayoutCounter问题解:listeners : {                                render : function(grid) ...

  8. powerdesigner 使用的几点问题

    一.powerdesigner 没有DataBase?: powerdesigner 只有在选择物理模型PDM的时候才会出现数据库菜单. 二.PowerDesigner版本控制功能? 1.首先介绍一下 ...

  9. oracle里面的时间转字符串to_char(),字符串转时间to_date(),以及substr和instr的使用。

    工作中编写过的一条语句 select * from Bt_Holiday where to_char(Setting_DATE,'YYYY')=Substr('2015-03-00',1,4) AND ...

  10. MySQL生产库主从重新同步操作注意事项

    因为一些原因,我们会遇到生产主从库重新同步的时候.重新同步MYSQL主从的时候有有一些注意的地方. 从库还原前一定要记得reset,因为重启mysql并不影响复制进程,如果忘记reset,会导致你一边 ...