前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HOJ 2148&POJ 2680(DP递推,加大数运算)

HOJ 2148&POJ 2680(DP递推,加大数运算)

作者头像
ShenduCC
发布2018-04-26 11:26:51
4200
发布2018-04-26 11:26:51
举报
文章被收录于专栏:算法修养算法修养

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

代码语言:javascript
复制
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]);
        }
    }


}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档