大数斐波那契JAVA_hdu 2044 2501 一只小蜜蜂#DP#斐波那契#java水大数

Problem Description

有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行。请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数。

其中,蜂房的结构如下所示。

0818b9ca8b590ca3270a3433284dd417.png

Input

输入数据的第一行是一个整数N,表示测试实例的个数,然后是N 行数据,每行包含两个整数a和b(0

Output

对于每个测试实例,请输出蜜蜂从蜂房a爬到蜂房b的可能路线数,每个实例的输出占一行。

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

import java.util.*;

import java.io.*;

import java.math.*;

public class Main {

public static void main(String []args){

Scanner cin = new Scanner(System.in);

Integer N = 50,i,n,a,b;

BigInteger[] fm = new BigInteger[N];

fm[1] = fm[2] = BigInteger.valueOf(1);

for(i = 3; i < N; ++i)

fm[i] = fm[i-1].add(fm[i-2]);

n = cin.nextInt();

for(i = 0; i < n; ++i){

a = cin.nextInt();

b = cin.nextInt();

b = b - a + 1;

System.out.println(fm[b]);

}

}

}

Problem Description

有一个大小是 2 x n 的网格,现在需要用2种规格的骨牌铺满,骨牌规格分别是 2 x 1 和 2 x 2,请计算一共有多少种铺设的方法。

Input

输入的第一行包含一个正整数T(T<=20),表示一共有 T组数据,接着是T行数据,每行包含一个正整数N(N<=30),表示网格的大小是2行N列。

Output

输出一共有多少种铺设的方法,每组数据的输出占一行。

Sample Input

3

2

8

12

Sample Output

3

171

2731

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

import java.util.*;

import java.io.*;

import java.math.*;

public class Main {

public static void main(String []args){

Scanner cin = new Scanner(System.in);

Integer N = 50,i,n,a,b;

BigInteger[] fm = new BigInteger[N];

fm[0] = BigInteger.valueOf(2);

fm[1] = BigInteger.valueOf(1);

fm[2] = BigInteger.valueOf(3);

for(i = 3; i < N; ++i)

fm[i] = fm[i-1].add(fm[i-2].multiply(fm[0]));

n = cin.nextInt();

for(i = 0; i < n; ++i){

b = cin.nextInt();

System.out.println(fm[b]);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值