UVA 10862 - Connect the Cable Wires(数论 递推 高精度)

Input: Standard In
Output: Standard Out

Next Generation Contest 1 

Time Limit: 1 second

Problem G

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.

                       cableServicetoHouse1,cableServicetoHouse2             cableServicetoHouse1,House1toHouse2               cableServicetoHouse2, House2toHouse1
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 0for 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


题意:看图很好理解

参考了 http://www.cnblogs.com/staginner/archive/2011/12/15/2288868.html

代码:

#include <stdio.h>
#include <string.h>
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
const int N = 1005;
const int MAXBIGN = 1005;


struct bign {
    int s[MAXBIGN];
    int len;
    bign() {
		len = 1;
		memset(s, 0, sizeof(s));
    }
	
    bign operator = (const char *number) {
		len = strlen(number);
		for (int i = 0; i < len; i++)
			s[len - i - 1] = number[i] - '0';
		return *this;
    }
    bign operator = (const int num) {
		char number[N];
		sprintf(number, "%d", num);
		*this = number;
		return *this;
    }
	
    bign (int number) {*this = number;}
    bign (const char* number) {*this = number;}
	
    bign operator + (const bign &c){  
		bign sum;
		int t = 0;
		sum.len = max(this->len, c.len);
		for (int i = 0; i < sum.len; i++) {
			if (i < this->len) t += this->s[i];
			if (i < c.len) t += c.s[i];
			sum.s[i] = t % 10;
			t /= 10;
		}
		
		while (t) {
			sum.s[sum.len++] = t % 10;
			t /= 10;
		}
		
		return sum;  
    }
	
	bign operator * (const bign &c){  
		bign sum; bign zero;
		if (*this == zero || c == zero)
			return zero;
		int i, j;
		sum.len = this->len + c.len;
		for (i = 0; i < this->len; i++) {
			for (j = 0; j < c.len; j ++) {
				sum.s[i + j] += this->s[i] * c.s[j];
			}
		}
		for (i = 0; i < sum.len; i ++) {
			sum.s[i + 1] += sum.s[i] / 10;
			sum.s[i] %= 10;
		}
		sum.len ++;
		while (!sum.s[sum.len - 1]) {
			sum.len --;
		}
		return sum;  
    }	
    bign operator - (const bign &c) {
		bign ans;
		ans.len = max(this->len, c.len);
		int i;
		
		for (i = 0; i < c.len; i++) {
			if (this->s[i] < c.s[i]) {
				this->s[i] += 10;
				this->s[i + 1]--;
			}
			ans.s[i] = this->s[i] - c.s[i];
		}
		
		for (; i < this->len; i++) {
			if (this->s[i] < 0) {
				this->s[i] += 10;
				this->s[i + 1]--;
			}
			ans.s[i] = this->s[i];
		}
		while (ans.s[ans.len - 1] == 0) {
			ans.len--;
		}
		if (ans.len == 0) ans.len = 1;
		return ans;
    }
	
    void put() {
		if (len == 1 && s[0] == 0) {
			printf("0");
		} else {
			for (int i = len - 1; i >= 0; i--)
				printf("%d", s[i]);
		}
    }
	
    bool operator < (const bign& b) const {
		if (len != b.len)
			return len < b.len;
		
		for (int i = len - 1; i >= 0; i--)
			if (s[i] != b.s[i])
				return s[i] < b.s[i];
			return false;
    }
    bool operator > (const bign& b) const { return b < *this; }
    bool operator <= (const bign& b) const { return !(b < *this); }
    bool operator >= (const bign& b) const { return !(*this < b); }
    bool operator != (const bign& b) const { return b < *this || *this < b;}
    bool operator == (const bign& b) const { return !(b != *this); }
};

bign f[2005];
int n;

void init() {
	f[1] = 1; f[2] = 3; bign three = 3;
	for (int i = 3; i <= 2000; i ++) {
		f[i] = three * f[i - 1] - f[i - 2];
	}
}

int main() {
	init();
	while (~scanf("%d", &n) && n) {
		f[n].put();
		printf("\n");
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值