UVA10862 Connect the Cable Wires【大数+递推】

Asif is a student of East West University and he is currently working for the EWUISP to meet his relatively high tuition fees. One day, as a part of his job, he was instructed to connect cable wires to N houses. All the houses lie in a straight line. He wants to use only the minimum number of cable wires required to complete his task such that all the houses receive the cable service. A house can either get the connection from the main transmission center or it can get it from a house to its immediate left or right provided the latter house is already getting the service.

  You are to write a program that determines the number of different combinations of the cable wires that is possible so that every house receives the service.

Example: If there are two houses then 3 combinations are possible as shown in the figure.


Figure: circles represent the transmission center and the small rectangles represent the houses.

Input

Each line of input contains a positive integer N (N ≤ 2000). The meaning of N is described in the above paragraph. A value of 0 for N indicates the end of input which should not be processed.

Output

For each line of input you have to output, on a single line, the number of possible arrangements. You can safely assume that this number will have less than 1000 digits.

Sample Input

1

2

3

0

Sample Output

1

3

8


题链接UVA10862 Connect the Cable Wires

问题简述:(略)

问题分析

  这个问题的关键是数列的递推式,同时需要注意起始项的值。递推式如下:

  f0=1

  f1=1

  ......

  fn=3*fn-1 - fn-2。

  大数计算是用模拟法来实现的,参见程序。

程序说明

  用Java程序来完成大数计算是最为方便的。

题记:(略)

参考链接:(略)


AC的C++语言程序如下:


AC的C++语言程序(模拟法)如下:

/* UVA10862 Connect the Cable Wires */

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 2000;
const int N2 = N / 16;
const LL BASE = 1e16;
LL fib[N + 1][N2 + 1];

void setfib()
{
    fib[1][0] = 1;
    fib[2][0] = 3;
    int len = 1;
    for(int i = 3; i <= N; i++) {
        int carry = 0;
        for(int j = 0; j < len; j++) {
            fib[i][j] = 3 * fib[i - 1][j] - fib[i - 2][j] + carry;
            if(fib[i][j] < 0) {
                carry = -1;
                fib[i][j] += BASE;
            } else {
                carry = fib[i][j] / BASE;
                fib[i][j] %= BASE;
            }
        }
        if(carry)
            fib[i][len++] = carry;
    }
}


int main()
{
    memset(fib, 0, sizeof(fib));

    setfib();

    int n;
    while(~scanf("%d", &n) && n) {
        int k = N2;
        while(fib[n][k] == 0)
            k--;
        printf("%lld", fib[n][k]);
        for(int i = k - 1; i >= 0; i--)
            printf("%016lld", fib[n][i]);
        printf("\n");
    }

    return 0;
}


AC的Java语言程序如下:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值