codeforces 658D. Bear and Polynomials

D. 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.


题意:改变一个系数使Q(2) = 0, 问有几种可能;


思路:直接求和肯定是不行的,太大了, 又因为它叫我的求的是Q(2),所以我们可以根据二进制吧所有值都放到最高位来计算。

如果进位是遇到奇数, 那么能改的系数位数只能在0 - 该位, 因为无论怎么改后面的,前面都不会为0;

遍历的时候从后往前,如果遇到值某一位的值很大,那么直接break即可,因为前面怎么改都不会为0;



#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<stack>
#include <queue>
#include <set>
using namespace std;
const int inf = 2147483647;
const double PI = acos(-1.0);
const int mod = 1000000007;
long long a[200005], b[200005];
int main()
{
	long long n, k;
	while (~scanf("%I64d%I64d", &n, &k))
	{
		for (long long i = 0; i <= n; ++i)
		{
			scanf("%I64d", &a[i]);
			b[i] = a[i];
		}
		for (long long i = 0; i < n; ++i)
		{
			b[i + 1] += b[i] / 2;
			b[i] %= 2;
		}
		long long x = 0;
		for (long long i = 0; i <= n; ++i)
		{
			if (b[i])
			{
				x = i;
				break;
			}
		}
		long long ans = 0;
		long long s = 0;
	
		for (long long i = n; i >= 0; --i)
		{
			s = s * 2 + b[i];
			if (abs(s) > 1e10)
				break;
			if (i <= x)
			{
				long long p = abs(s - a[i]);
				if (p == 0 && i == n)
					continue;
				if (p <= k)
					ans++;
			}
		}
		printf("%I64d\n", ans);

	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值