3769. 【NOI2015模拟8.14】A+B

Time Limits: 1500 ms  Memory Limits: 262144 KB  Detailed Limits  

Description

对于每个数字x,我们总可以把它表示成一些斐波拉切数字之和,比如8 = 5 + 3,  而22 = 21 + 1,因此我们可以写成  x = a1 * Fib1 + a2 * Fib2 + a3 * Fib3 + … + an * Fibn, 其中,Fib1 = 1, Fib2 = 2…. Fib[i] = Fib[i – 1] + Fib[I - 2],  且a[n] > 0.那么我们称ai为x的一种斐波拉切表示,由于表示方法有很多种,我们要求最大化a[1…n],即,如果b[1…n]和a[1…m]都可以表示x,若m >  n 则a更大,若  m  =  n,  则从高位到低位比,第一个不同处i,若ai  > bi  则a比b大。

你的任务很简单,给你两个用斐波拉切数最大化表示的两个数字,输出他们相加后用斐波那契最大化表示的数字。

Input

两行,分别表示两个数字

每一行开头一个n,表示长度

然后紧接着n个数字,为从低位到高位。

Output

同输入格式。一行。

Sample Input

4 0 1 0 1

5 0 1 0 0 1

Sample Output

6 1 0 1 0 0 1

Data Constraint

对于30%的数据  长度  <= 1000

对于100%的数据  长度  <= 1000000

 

题解:

注意比较是从高位到低位, 高位是权值大的, 即从左到右

将a +b后, 发现得到的数组c位置只有0和1和2三种情况

 

首先, 对于每个2 我们把他拆给前面和后面(显然更优) 

a, b, c, d四个位置

若c = 1

则将c = 0, a = 1, d = 1, 证明略

然后, 对于两个连续的1, 把他合并到后面去(显然更优) 

 

发现合并后可能有新的2出现且此方案不一定最优, 我们重复上述步骤直至数列中的2都被消去。

 

O(玄学)

#include<cstdio>
#include<cstring>
#include<algorithm>
#define open(x) freopen(x".in", "r", stdin);freopen(x".out", "w", stdout)
#define mem(a, b) memset(a, b, sizeof(a))
#define mcy(a, b) memcpy(a, b, sizeof(a))
#define pf printf
#define fo(i, a, b) for(register int i = a; i <= b; ++i) 
#define fown(i, a, b) for(register int i = a; i >= b; --i)
#define em(p, x) for(int p=tail[x];p;p=e[p].fr)
#define ll long long
#define wt(a, c, d, s) fo(i, c, d) pf((s), a[i]); puts("")
#define rd(a, c, d) fo(i, c, d) in(a[i])
#define N 2000010
#define inf 2147483647
#define mod (int)(1e9 + 7)
using namespace std;

template<class T> 
T in(T &x) {
	x=0;
	char ch = getchar(); ll f = 0;
	while(ch < '0' || ch > '9') f |= ch == '-', ch = getchar();
	while(ch >= '0' && ch <= '9') x = (x<<1) + (x<<3) + ch - '0', ch = getchar();
	x = f ? -x : x;
	return x;
}

int maxn;
int a[N], b[N], c[N];

inline int check() {
	fo(i, 1, maxn) if(c[i] == 2) return 0;
	return 1;
}

int main() {
	open("feb");
	in(a[0]); rd(a, 1, a[0]);
	in(b[0]); rd(b, 1, b[0]);
	maxn = max(a[0], b[0]);
	fo(i, 1, maxn) c[i] = a[i] + b[i];
	do {
		if(c[maxn + 1])++maxn;
		fo(i, 1, maxn) if(c[i] > 1) {
			c[i + 1]++;
			if(i - 2 > 0) c[i - 2]++; else if(i == 2) c[1]++;
			c[i] -= 2;
		}
		if(c[maxn + 1])++maxn;
		fo(i, 1, maxn - 1) if(c[i] && c[i + 1]) {
			int cnt = min(c[i], c[i + 1]);
			c[i + 2] += cnt;
			c[i] -= cnt, c[i + 1] -= cnt;
		}
	}while(check() == 0);
	pf("%d ", maxn);
	fo(i, 1, maxn) pf("%d ", c[i]);
	
	return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值