HOJ 2148&POJ 2680(DP递推,加大数运算)
Computer Transformation
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4561 Accepted: 1738
Description
A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, after the first time step, the sequence 0 1 is obtained; after the second, the sequence 1 0 0 1, after the third, the sequence 0 1 1 0 1 0 0 1 and so on.
How many pairs of consequitive zeroes will appear in the sequence after n steps?
Input
Every input line contains one natural number n (0 < n <= 1000).
Output
For each input n print the number of consequitive zeroes pairs that will appear in the sequence after n steps.
Sample Input
2
3
Sample Output
1
1
Source
这是我第一次用java进行大数运算
递推很简单,00只可能是上一个的01产生,上一个的01只可能是上上一个的00 1产生
hoj的测试机好像有问题,poj里面ac的代码提交不上去hoj
import java.math.BigInteger;
import java.util.Scanner;
/**
*
* @author chenyongkang
*/
public class Main {
public static BigInteger a;
public static BigInteger b[]=new BigInteger[1010];
public static void init()
{
BigInteger x=BigInteger.valueOf(0);
BigInteger y=BigInteger.valueOf(1);
for(int i=0;i<=1000;i++)
b[i]=BigInteger.valueOf(0);
b[1]=x;b[2]=y;
for(int i=3;i<=1000;i++)
{
a=BigInteger.valueOf(1);
for(int j=1;j<=i-3;j++)
{
a=a.multiply(BigInteger.valueOf(2));
}
b[i]=b[i].add(b[i-2]);
b[i]=b[i].add(a);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
init();
Scanner cin=new Scanner(System.in);
while(cin.hasNext())
{
int n=cin.nextInt();
System.out.println(b[n]);
}
}
}
HOJ 2148&POJ 2680(DP递推,加大数运算)的更多相关文章
- uva 10328 - Coin Toss 投硬币(dp递推,大数)
题意:抛出n次硬币(有顺序),求至少k个以上的连续正面的情况的种数. 思路:转换成求抛n个硬币,至多k-1个连续的情况种数,用所有可能出现的情况种数减去至多k-1个的情况,就得到答案了.此题涉及大数加 ...
- hdu2089(数位DP 递推形式)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- UVA 10559 Blocks(区间DP&&递推)
题目大意:给你玩一个一维版的消灭星星,得分是当前消去的区间的长度的平方,求最大得分. 现在分析一下题目 因为得分是长度的平方,不能直接累加,所以在计算得分时需要考虑前一个状态所消去的长度,仅用dp[l ...
- poj 2506 Tiling 递推
题目链接: http://poj.org/problem?id=2506 题目描述: 有2*1和2*2两种瓷片,问铺成2*n的图形有多少种方法? 解题思路: 利用递推思想,2*n可以由2*(n-1)的 ...
- hdu 2604 Queuing(dp递推)
昨晚搞的第二道矩阵快速幂,一开始我还想直接套个矩阵上去(原谅哥模板题做多了),后来看清楚题意后觉得有点像之前做的数位dp的水题,于是就用数位dp的方法去分析,推了好一会总算推出它的递推关系式了(还是菜 ...
- Power oj2498/DP/递推
power oj 2498 /递推 2498: 新年礼物 Time Limit: 1000 MS Memory Limit: 65536 KBTotal Submit: 12 Accepted: 3 ...
- BZOJ4321queue2——DP/递推
题目描述 n 个沙茶,被编号 1~n.排完队之后,每个沙茶希望,自己的相邻的两 人只要无一个人的编号和自己的编号相差为 1(+1 或-1)就行: 现在想知道,存在多少方案满足沙茶们如此不苛刻的条件. ...
- Shell Necklace (dp递推改cdq分治 + fft)
首先读出题意,然后发现这是一道DP,我们可以获得递推式为 然后就知道,不行啊,时间复杂度为O(n2),然后又可以根据递推式看出这里面可以拆解成多项式乘法,但是即使用了fft,我们还需要做n次多项式乘法 ...
- hdu 1723 DP/递推
题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacc ...
随机推荐
- servlet3.0 新特性和springboot Listener和filter案例
1.filter package com.newtouch.zxf.filter; import java.io.IOException; import javax.servlet.Filter; i ...
- c++单例设计模式---17
原创博文,转载请标明出处--周学伟 http://www.cnblogs.com/zxouxuewei/ 全局变量在项目中是能不用就不用的,它是一个定时炸弹,是一个不安全隐患,特别是在多线程程序中, ...
- C# base和this的用法
/** this关键字* this关键字引用类的当前实例* 注意:静态成员方法中不能使用this关键字,this关键字只能在实例构造函数.实例方法或实例访问器中使用*/ /** base关键字* ba ...
- 绑定方式开始服务&调用服务的方法
1.编写activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/androi ...
- springJdbc in 查询,Spring namedParameterJdbcTemplate in查询
springJdbc in 查询,Spring namedParameterJdbcTemplate in查询, SpringJdbc命名参数in查询,namedParameterJdbcTempla ...
- 安装RVDS2.2
本人经过一晚上的折腾,已经将rvds2.2成功部署在为AMD平台的CPU上面,除了些许小BUG外,编译程序无任何错误,可成功将产上的AXF文件通过Jlink烧制到开发板上. 感谢cdly7475为我们 ...
- Flask中向前端传递或者接收Json文件的方法
1. 利用flask的request.form.get()方法 这一中方法主要利用flask的request.form.get方法,获得前端发送给后台的json文件 Python 端代码: @app. ...
- SVN的基本原理 配置自动更新WEB服务器
SVN的基本原理 配置自动更新WEB服务器 最近有个小项目,需要用SVN来进行版本控制.项目组的同僚有8个人,大家都在本地开发,然后提交到服务器——服务器就是其中一台机器.专门安排一个测试员来进行项目 ...
- WP8.1学习系列(第二十三章)——到控件的数据绑定
在本文中 先决条件 将控件绑定到单个项目 将控件绑定到对象的集合 通过使用数据模板显示控件中的项目 添加详细信息视图 转换数据以在控件中显示 相关主题 本主题介绍了如何在使用 C++.C# 或 Vis ...
- 三个 CSS 预处理器(框架):Sass、LESS 和 Stylus
CSS 预处理器技术已经非常的成熟,而且也涌现出了越来越多的 CSS 的预处理器框架.本文向你介绍使用最为普遍的三款 CSS 预处理器框架,分别是 Sass.Less CSS.Stylus. 首先我们 ...