D. Bear and Polynomials

题目连接:

http://www.codeforces.com/contest/658/problem/D

Description

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.

Sample Input

3 1000000000

10 -9 -3 5

Sample Output

3

Hint

题意

给你一个多项式,然后告诉你P(2)!=0

你可以改变其中某一项的系数,使得P(2)=0,问你有多少种改变方法

题解:

先正面扫一遍,把所有的系数都往后传,这样除了最后一个数的系数以外,其他的系数都是+-1,0这种

然后我们再倒着扫一遍,判断这个数的系数应该是多少就好了。

对了,在从后面往前面走的过程中,如果某个位置的数大于了某个值的时候,就可以直接break了

因为会在不断的乘以2,不可能产生答案了。

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2e5+6; long long a[maxn];
long long c[maxn];
int n,flag;
long long k;
int main()
{
scanf("%d%lld",&n,&k);
for(int i=0;i<=n;i++)
{
scanf("%lld",&a[i]);
c[i]=a[i];
}
for(int i=0;i<n;i++)
{
a[i+1]+=a[i]/2LL;
a[i]%=2LL;
}
for(int i=0;i<=n;i++)
if(a[i])
{
flag = i;
break;
}
long long sum = 0;
long long ans = 0;
for(int i=n;i>=0;i--)
{
sum = sum * 2LL + a[i];
if(abs(sum)>1ll*1e9*1e7)break;
if(i<=flag)
{
long long p = c[i]-sum;
if(abs(p)<=k)
{
if(i==n&&p==0)continue;
ans++;
}
}
}
cout<<ans<<endl;
}

VK Cup 2016 - Round 1 (Div. 2 Edition) D. Bear and Polynomials的更多相关文章

  1. VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3

    C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input ...

  2. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D Bear and Two Paths

    题目链接: http://codeforces.com/contest/673/problem/D 题意: 给四个不同点a,b,c,d,求是否能构造出两条哈密顿通路,一条a到b,一条c到d. 题解: ...

  3. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C - Bear and Colors

    题目链接: http://codeforces.com/contest/673/problem/C 题解: 枚举所有的区间,维护一下每种颜色出现的次数,记录一下出现最多且最小的就可以了. 暴力n*n. ...

  4. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths 构造

    D. Bear and Two Paths 题目连接: http://www.codeforces.com/contest/673/problem/D Description Bearland has ...

  5. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C. Bear and Colors 暴力

    C. Bear and Colors 题目连接: http://www.codeforces.com/contest/673/problem/C Description Bear Limak has ...

  6. Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A. Bear and Game 水题

    A. Bear and Game 题目连接: http://www.codeforces.com/contest/673/problem/A Description Bear Limak likes ...

  7. VK Cup 2016 - Round 1 (Div. 2 Edition) E. Bear and Contribution 单调队列

    E. Bear and Contribution 题目连接: http://www.codeforces.com/contest/658/problem/E Description Codeforce ...

  8. VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3 构造

    C. Bear and Forgotten Tree 3 题目连接: http://www.codeforces.com/contest/658/problem/C Description A tre ...

  9. VK Cup 2016 - Round 1 (Div. 2 Edition) B. Bear and Displayed Friends 树状数组

    B. Bear and Displayed Friends 题目连接: http://www.codeforces.com/contest/658/problem/B Description Lima ...

随机推荐

  1. OkHttp与Cookie及Cookie的持久化

    http://blog.csdn.net/u011150924/article/details/52780931 http://blog.csdn.net/chen19960724/article/d ...

  2. 商城项目(ssm+dubbo+nginx+mysql统合项目)总结(1)

    我不会在这里贴代码和详细步骤什么的,我觉得就算我把它贴出来,你们照着步骤做还是会出很多问题,我推荐你们去看一下黑马的这个视频,我个人感觉很不错,一步一步走下来可以学到很多东西.另外,视频和相关文档的话 ...

  3. flask插件系列之SQLAlchemy基础使用

    sqlalchemy是一个操作关系型数据库的ORM工具.下面研究一下单独使用和其在flask框架中的使用方法. 直接使用sqlalchemy操作数据库 安装sqlalchemy pip install ...

  4. Balanced and stabilized quicksort method

    The improved Quicksort method of the present invention utilizes two pointers initialized at opposite ...

  5. linux用户修改用户shell

    要拒绝系统用户登录,可以将其shell设置为/usr/sbin/nologin或者/bin/false # usermod -s /usr/sbin/nologin username 或者 # use ...

  6. 63.UniquePaths II---dp

    题目链接 题目大意:与62题类似,只是这个题中间有障碍. 法一:dfs,依旧超时.代码如下: public int uniquePathsWithObstacles(int[][] obstacleG ...

  7. 关于函数strtok和strtok_r的使用要点和实现原理

    strtok函数的使用是一个老生常谈的问题了.该函数的作用很大,争议也很大.以下的表述可能与一些资料有区别或者说与你原来的认识有差异,因此,我尽量以实验为证.交代一下实验环境是必要的,winxp+vc ...

  8. Caffe学习系列(8):solver,train_val.prototxt,deploy.prototxt及其配置

    solver是caffe的核心. net: "examples/mnist/lenet_train_test.prototxt" test_iter: 100 test_inter ...

  9. python【项目】:工资管理(简易版)

    功能要求: 登录系统用户认证通过后才能列出下一级菜单员工信息表 登录系统要有用户登录.注册账号.删除账号.修改密码.退出 登录密码要有加密功能 从info.txt文件读取员工及工资信息,最后通过增加, ...

  10. LeetCode 260. Single Number III(只出现一次的数字 III)

    LeetCode 260. Single Number III(只出现一次的数字 III)