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.


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.


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





Sample Output




题链接UVA10862 Connect the Cable Wires







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








/* 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;
            fib[i][len++] = carry;

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


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

    return 0;




Problem DescriptionnEngineer Bob is a strange man, and he makes his floor a simple polygon and installs various fantastic machines on his floor.nnOn Bob’s floor, there are n machines and n sockets. Each machine is fixed at a certain location and each socket is fixed at a certain position. Different machines and socket may be located at the same position.nnBob wants to connect one machine with exactly one socket by using wire and he hopes that the total length of the wires is minimal.nIn addition, we assume that:nBob’s floor can be described by m vertices in a 2-dimentional coordinate system. The m vertices are always given by clockwise or counter-clockwise. For simplicity, the edges of the polygon are always horizontal or vertical. For example, (0,0) (0,4) (4,4) (4,0) (2,0) (2,1) (3, 1) (3,2) (1,2) (1,0) describe the polygon which is shown in Figure 1, in which M1 and M2 are machines, S1 and S2 are sockets.nnWires can only be equipped within the polygon or on the edges of the polygon. Wires can be intercrossed.nn![](http://acm.hdu.edu.cn/data/images/con210-1008-1.JPG)nnnnnNow please find out the minimal length of all the wires needed.n nnInputnThe input contains several cases. The first line of each case contains one integer m (1 ≤ m ≤ 100) indicating the number of vertices of the polygon. This is then followed by m lines, in which the ith line contains two integers x (0 ≤ x ≤ 10,000) and y (0 ≤ y ≤ 10,000), indicating the coordinate of the ith vertex of the polygon. And this is followed by one line, containing one integer n (1 ≤ n ≤ 16), indicating the number of machines (and also sockets). nThis is followed by n lines, in which the ith line contains two integers x (0 ≤ x ≤ 10,000) and y (0 ≤ y ≤ 10,000), indicating the coordinate of the ith machine. And this is followed by n lines, in which the ith line contains two integers x (0 ≤ x ≤ 10,000) and y (0 ≤ y ≤ 10,000), indicating the coordinate of the ith socket. The input ends with m=0. It is guaranteed that the coordinates of all machines and sockets are always given within the polygon or on the edges of the polygon.n nnOutputnFor each case, output a floating-point number to two decimal places, indicating the minimal length of wires needed.n nnSample Inputn10n0 0n0 4n4 4n4 0n2 0n2 1n3 1n3 2n1 2n1 0n2n2 3n1 0n3 3n2 0n0n nnSample Outputn7.41n


  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他