Bear and Polynomials 639 C

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

题意:有一个多项式ai*2^i,其和不为0,让你改变其中一个ai使得和为0,问一共有多少种改变方式。

思路:我们把这个和转化成二进制的形式,简便处理,让每一位上可以是-1,0,1,如果某一个ai可以作为一种答案改变,那么其低位上的数字肯定全是0。这样我们设只有低位的pos个可能成为答案。然后从高位往低位做运算,算出这个ai需要改变多少。当需要改变的量>3*K的时候(可能2*K也可以),低位是不可能把数目给补回来的,也就不用继续算了,这样能保证在long的范围内。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
int T,t,n,m;
ll num[210000],s[210000],K,ans,ret,ret2,temp;
int main(){
    int i,j,k,pos;
    scanf("%d%I64d",&n,&K);
    for(i=0;i<=n;i++)
        scanf("%I64d",&num[i]);
    for(i=0;i<=n+40;i++){
        s[i]+=num[i];
        s[i+1]+=s[i]/2;
        s[i]=s[i]%2;
    }
    for(i=0;i<=n+40;i++)
        if(s[i]!=0)
            break;
    pos=min(n,i);
    ret=0;
    j=n;
    for(i=n+40;i>pos;i--){
        ret=ret*2+s[i];
        if(ret>K*3 || ret<-K*3){
            printf("0\n");
            return 0;
        }
    }
    for(i=pos;i>=0;i--){
        ret=ret*2+s[i];
        if(ret>K*3 || ret<-K*3)
            break;
        ret2=num[i]-ret;
        if(-K<=ret2 && ret2<=K && !(i==n &&ret2==0))
            ans++;
    }
    printf("%d\n",ans);
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值