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;
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章