题目链接:

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;

思路:

像二进制那样都变成0,-1,+1,都转移到n位上;再从高位到低位计算;

AC代码:

/*
2014300227 658D - 26 GNU C++11 Accepted 576 ms 5308 KB
*/
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+;
long long a[N],b[N+];
int n,l=,k;
int main()
{ scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
{
cin>>a[i];
b[i]=a[i];
}
for(int i=;i<n;i++)
{
a[i+]+=a[i]/;
a[i]=a[i]%;
}
for(int i=;i<=n;i++)
{
if(a[i])
{
l=i;
break;
}
}
int answ=;
long long sum=;
for(int i=n;i>=;i--)
{
sum=sum*+a[i];
if(abs(sum)>1e10)break;
if(i<=l)
{
long long x=abs(sum-b[i]);
if(x==&&i==n)continue;
if(x<=k)answ++;
}
}
cout<<answ<<"\n";
return ;
}

codeforces 658D D. Bear and Polynomials(数学)的更多相关文章

  1. CodeForces 639C Bear and Polynomials

    Bear and Polynomials 题解: 如果改变一个其中的一个数,那么需要知道的是,前面的数都可以进到当前位来,如果过不来的话,那么就会因为前面有数导致无法变成0. 所以我们将前面的数不断向 ...

  2. VK Cup 2016 - Round 1 (Div. 2 Edition) D. Bear and Polynomials

    D. Bear and Polynomials 题目连接: http://www.codeforces.com/contest/658/problem/D Description Limak is a ...

  3. Codeforces 658D Bear and Polynomials【数学】

    题目链接: http://codeforces.com/contest/658/problem/D 题意: 给定合法多项式,改变一项的系数,使得P(2)=0,问有多少种方法? 分析: 暴力求和然后依次 ...

  4. Codeforces 902D/901B - GCD of Polynomials

    传送门:http://codeforces.com/contest/902/problem/D 本题是一个数学问题——多项式整除. 对于两个整数a.b,求最大公约数gcd(a,b)的辗转相除法的函数如 ...

  5. 【32.89%】【codeforces 574D】Bear and Blocks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. Codeforces 735C:Tennis Championship(数学+贪心)

    http://codeforces.com/problemset/problem/735/C 题意:有n个人打锦标赛,淘汰赛制度,即一个人和另一个人打,输的一方出局.问这n个人里面冠军最多能赢多少场, ...

  7. codeforces 680C C. Bear and Prime 100(数论)

    题目链接: C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input s ...

  8. codeforces 680B B. Bear and Finding Criminals(水题)

    题目链接: B. Bear and Finding Criminals //#include <bits/stdc++.h> #include <vector> #includ ...

  9. codeforces 680A A. Bear and Five Cards(水题)

    题目链接: A. Bear and Five Cards //#include <bits/stdc++.h> #include <vector> #include <i ...

随机推荐

  1. SAS学习经验总结分享:篇二—input语句

    SAS编程语言中input语句的应用         SAS数据步的建立离不开input语句,在读入外部数据或cards语句后面的数据块时需要通过input语句定义变量.下面介绍input语句定义变量 ...

  2. 可以添加自定义的Select控件

    1.控件dom <select name="WebSiteTarget" id="WebSiteTarget" class="w1" ...

  3. Html5 meta 笔记

    摘抄:原文地址:http://www.kmapk.com/html/help/02/127.html 一.天猫 <title>天猫触屏版</title> <meta co ...

  4. 企业级API设计

    最近对service的API设计,在team内有些讨论,主要集中在API是足够抽象.通用好呢, 还是具体.易用好? 其实这个是要折衷的,通用的好处是以后更改API的可能性小,但坏处是想要通用,里面的字 ...

  5. wifi认证Portal开发系列(三):portal协议

    中国移动WLAN业务PORTAL协议规范介绍 一.用户上线认证流程 上线流程完成用户账号的认证,并把认证结果通知Portal Server,Portal server将会通知WLAN用户并且显示相应的 ...

  6. 使用Docker开发NodeJs APP

    英文版原文地址 这是两篇连载文章的第一篇,讲解了如何使用 Docker 替代 Vagrant 开发基于 Express 框架的NodeJs App的部分细节.不过,这次要增加点难度:我们要使用 con ...

  7. Java异常 - Exception总结

    这篇blog总结的比较详细了. 如下图所示,在Java中所有异常的基类均为Throwable类.有两个子类,分别为Exception和Error.其中Error主要由JVM处理,比如OutOfMemo ...

  8. js中insertAdjacentHTML的玩法

    原型:insertAdajcentHTML(swhere,stext) insertAdjacentHTML方法:在指定的地方插入html标签语句 参数:swhere: 指定插入html标签语句的地方 ...

  9. CentOS7时间设置及ntp同步配置(转)

    出处:http://www.centoscn.com/CentOS/config/2015/1105/6385.html http://www.centoscn.com/CentOS/config/2 ...

  10. winform学习

    1:http://www.cnblogs.com/yieryi/category/704334.html 系列文章 2:http://www.easyicon.net/iconsearch/login ...