Bear and Polynomials 639 C

来源:互联网 发布:centos如何安装ssh 编辑:程序博客网 时间:2024/06/05 09:21

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 100000000010 -9 -3 5
output
3
input
3 1210 -9 -3 5
output
2
input
2 2014 -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);}



0 0
原创粉丝点击