【VK Cup 2016 - Round 1 (Div 2 Edition)D】【二进制思想 扫描贪心】Bear and Polynomials 2的幂次多项式改变一位使得和为0

Bear and Polynomials
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Limak is a little polar bear. He doesn't have many toys and thus he often plays with polynomials.

He considers a polynomial valid if its degree is n and its coefficients are integers not exceeding k by the absolute value. More formally:

Let a0, a1, ..., an denote the coefficients, so . Then, a polynomial P(x) is valid if all the following conditions are satisfied:

  • ai is integer for every i;
  • |ai| ≤ k for every i;
  • an ≠ 0.

Limak has recently got a valid polynomial P with coefficients a0, a1, a2, ..., an. He noticed that P(2) ≠ 0 and he wants to change it. He is going to change one coefficient to get a valid polynomial Q of degree n that Q(2) = 0. Count the number of ways to do so. You should count two ways as a distinct if coefficients of target polynoms differ.

Input

The first line contains two integers n and k (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 109) — the degree of the polynomial and the limit for absolute values of coefficients.

The second line contains n + 1 integers a0, a1, ..., an (|ai| ≤ k, an ≠ 0) — describing a valid polynomial . It's guaranteed that P(2) ≠ 0.

Output

Print the number of ways to change one coefficient to get a valid polynomial Q that Q(2) = 0.

Examples
input
3 1000000000
10 -9 -3 5
output
3
input
3 12
10 -9 -3 5
output
2
input
2 20
14 -7 19
output
0
Note

In the first sample, we are given a polynomial P(x) = 10 - 9x - 3x2 + 5x3.

Limak can change one coefficient in three ways:

  1. He can set a0 =  - 10. Then he would get Q(x) =  - 10 - 9x - 3x2 + 5x3 and indeed Q(2) =  - 10 - 18 - 12 + 40 = 0.
  2. Or he can set a2 =  - 8. Then Q(x) = 10 - 9x - 8x2 + 5x3 and indeed Q(2) = 10 - 18 - 32 + 40 = 0.
  3. Or he can set a1 =  - 19. Then Q(x) = 10 - 19x - 3x2 + 5x3 and indeed Q(2) = 10 - 38 - 12 + 40 = 0.

In the second sample, we are given the same polynomial. This time though, k is equal to 12 instead of 109. Two first of ways listed above are still valid but in the third way we would get |a1| > k what is not allowed. Thus, the answer is 2 this time.


#include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<ctype.h>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<bitset>
#include<algorithm>
#include<time.h>
using namespace std;
void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }
#define MS(x,y) memset(x,y,sizeof(x))
#define MC(x,y) memcpy(x,y,sizeof(x))
#define MP(x,y) make_pair(x,y)
#define ls o<<1
#define rs o<<1|1
typedef long long LL;
typedef unsigned long long UL;
typedef unsigned int UI;
template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }
template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }
const int N = 2e5+1000, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;
int n, k;
LL o[N], a[N];
bool zero[N];
const int inf = 4e9;
int solve()
{
	int ret = 0;
	for (int i = n + 100; i >= 1; --i)
	{
		if (abs(a[i] - o[i]) > inf)return ret;
		if (abs(a[i] - o[i]) <= k && zero[i - 1] == 0 && (i <= n || i == n + 1 && abs(a[i] - o[i]) != 0))++ret;
		a[i - 1] += a[i] * 2;
	}
	return ret;
}
int main()
{
	while (~scanf("%d%d", &n,&k))
	{
		MS(o, 0);
		for (int i = 1; i <= n + 1; ++i)scanf("%lld", &o[i]);
		MC(a, o);

		for (int i = 1; i <= n + 100; ++i)
		{
			a[i + 1] += a[i] / 2;
			a[i] %= 2;
			zero[i] = zero[i - 1] | a[i];
		}
		printf("%d\n", solve());
	}
	return 0;
}
/*
【题意】
有一个多项式P,这个多项式被定义为合法的,即——

1,其最高次项为n(2e5),不可变为0
2,每项系数绝对值均不可超过k
3,各项系数均为整数

现在P(2)!=0,我们想只改变一个系数,同时使得——
1,P(2)=0
2,a[n]!=0(即不能使最高项的系数为0)

【类型】
二进制思想

【分析】
多项式P(2),每一项都是——系数*2^k的形式

我们发现,如果改变某一项,要使得这一项之前的和恰好为0
而这个要如何查证呢?
我们不妨把这个多项式的系数转化为严格的二进制表示法。
这样,我们就知道从低位开始向上数,第一个低位之和不为0的位置是哪里了。
比如a[1~p-1]==0都成立,而a[p]==1,那么我们改变多项式的系数,必然只能改变[1,p]中的某一位。

而我们要改变使得这个多项式为0.
我们还要从高位向低位滚动。
把高位的二进制base转化到低位,如果这个低位以下全部为0,我们就可以计算低位消除高位需要把数改变多少。
如果变成的数的绝对值符合要求,就达标。

然而要注意——
我们低位的数对高位的消除作用是有限的,[1~p-1]数全部加起来,对[p]的消除作用不过也就1e9而已。
所以,如果目前消除的成本过高,超过2e9,传递下去也必然GG,我们就不再传递了
否则的话——
	1,消除改变后的数在[-k,k]之间
	2,如果是改变最高位的话,改变后的数不能为0
	3,低位数之和为0

【时间复杂度&&优化】
O(2n)

【数据】
1 1000
16 8 

*/


你可能感兴趣的:(codeforces,贪心,题库-CF,进制拆分)