题目链接:

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
  1. 3 1000000000
    10 -9 -3 5
output
  1. 3
input
  1. 3 12
    10 -9 -3 5
output
  1. 2
input
  1. 2 20
    14 -7 19
output
  1. 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代码:

  1. /*
  2. 2014300227 658D - 26 GNU C++11 Accepted 576 ms 5308 KB
  3. */
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. const int N=2e5+;
  7. long long a[N],b[N+];
  8. int n,l=,k;
  9. int main()
  10. {
  11.  
  12. scanf("%d%d",&n,&k);
  13. for(int i=;i<=n;i++)
  14. {
  15. cin>>a[i];
  16. b[i]=a[i];
  17. }
  18. for(int i=;i<n;i++)
  19. {
  20. a[i+]+=a[i]/;
  21. a[i]=a[i]%;
  22. }
  23. for(int i=;i<=n;i++)
  24. {
  25. if(a[i])
  26. {
  27. l=i;
  28. break;
  29. }
  30. }
  31. int answ=;
  32. long long sum=;
  33. for(int i=n;i>=;i--)
  34. {
  35. sum=sum*+a[i];
  36. if(abs(sum)>1e10)break;
  37. if(i<=l)
  38. {
  39. long long x=abs(sum-b[i]);
  40. if(x==&&i==n)continue;
  41. if(x<=k)answ++;
  42. }
  43. }
  44. cout<<answ<<"\n";
  45. return ;
  46. }

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. centos7 安装rocketmq(quick start)

    Quick Start This quick start guide is a detailed instruction of setting up RocketMQ messaging system ...

  2. php计算两个经纬度地点之间的距离(转)

    php计算两个指定的经纬度地点之间的距离,这个在做计算给定某个地点的经纬度,计算其附近的商业区,以及给定地点与附近各商业区之间的距离的时候,还是用的到的.下面是具体的函数代码以及用法示例. 关于如何获 ...

  3. Java NIO之Charset类字符编码对象

    介绍 java中使用Charset来表示编码对象 This class defines methods for creating decoders and encoders and for retri ...

  4. 删除反复行SQL举例

    删除反复行SQL实验简单举例 说明:实验按顺序进行.前后存在关联性.阅读时请注意.打开文件夹更便于查看. 构造实验环境: SQL> select count(*) from emp;   COU ...

  5. Content Security Policy

    资料来源:阮一峰博客 一.背景 XSS最常见,危害最大的网页安全漏洞,“网页安全政策”从根本上解决问题 二.简介 CSP的实质是白名单制度,明确告诉客户端那些外部资源可以加载和执行. CSP 大大增强 ...

  6. intellij 开发webservice

    最近项目中有用到WebService,于是就研究了一下,但是关于intellij 开发 WebService 的文章极少,要不就是多年以前,于是研究一下,写这篇博文.纯属记录,分享,中间有不对的地方, ...

  7. PowerBuilder -- 结构类型(structure)

    http://bbs.csdn.net/topics/3501120743楼答复 PB的structure分两种,全局的和局部的,两者只有作用域不同. 全局的在file/new/pb object/s ...

  8. PowerBuilder -- 变更某列的背景色

    记得把background.mode设置为2 li_col++ ls_col[li_col] = ls_fit_no ls_column = ' col' + String(li_col) ls_co ...

  9. python MySQLdb Windows下安装教程及问题解决方法(python2.7)

    使用python访问mysql,需要一系列安装 linux下MySQLdb安装见  Python MySQLdb在Linux下的快速安装http://www.jb51.net/article/6574 ...

  10. python 基础 6.1 异常处理方法

      一. Excepthion 异常类    Excepthion 是所有的异常基础类(),对于python 的标准异常,我们列出如下,以做参考:   异常名称                     ...