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. php菜刀分析学习

    这里以eval为例 我们知道, php中的eval能把字符串当代码执行: eval('phpcode'); 注意, 这里的代码要有分号结尾, 我们测试: 我们创建一个最简单的SHELL: <?p ...

  2. 【swupdate文档 五】从可信的来源更新镜像

    从可信的来源更新镜像 现在越来越重要的是,设备不仅要能安全地进行更新操作, 而且要能够验证发送的图像是否来自一个已知的源, 并且没有嵌入恶意软件. 为了实现这个目标,SWUpdate必须验证传入的镜像 ...

  3. Linux是对用户的密码的复杂度要求设置【转】

    那么Linux是如何实现对用户的密码的复杂度的检查的呢?其实系统对密码的控制是有两部分组成: 1 cracklib 2 /etc/login.defs pam_cracklib.so 才是控制密码复杂 ...

  4. Entity Framework 5.0 Code First全面学习 (转)

    原文地址:感谢原文作者 http://blog.csdn.net/gentle_wolf/article/details/14004345 不贴图片了,太累. Code First 约定 借助 Cod ...

  5. 关于"轉淚點"与"轉捩點"

    经常看台湾偶像剧或台湾综艺节目的人,一定听过"转泪点"这个词,虽然我一直不知道这三个字具体是怎么写, 但其意思很容易明白,就是"转折点"的意思.今天无聊在看凤凰 ...

  6. POJ 3278 Catch That Cow(简单BFS)

    题目链接:http://poj.org/problem?id=3278 题目大意:给你两个数字n,k.可以对n执行操作(n+1,n-1,n*2),问最少需要几次操作使n变成k. 解题思路:bfs,每次 ...

  7. linux下设置opencv环境变量

    一.安装opencv(这里不再讲述) 二.添加库路径(创建opencv.conf文件) 输入命令:vi /etc/ld.so.conf.d/opencv.conf 输入/usr/local/lib,并 ...

  8. NIO-2通道(Channel)

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...

  9. PHP 权威代码风格规范

    1.常规 尽量统一ide 比如phpstream 配置文件(Settings → Code Style → PHP → Set from... → Predefined Style → PSR1/PS ...

  10. jquery datatable的详细用法

    1,首先需要引用下面两个文件 <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css ...