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. 多维尺度变换MDS(Multidimensional Scaling)

    流形学习(Manifold Learning)是机器学习中一大类算法的统称,流形学习是非线性的降维方法(an approach to non-linear dimensionality reducti ...

  2. 33 - 并发编程-线程同步-Event-lock

    目录 1 线程同步 1.1 Event 1.1.1 什么是Flag? 1.1.2 Event原理 1.1.3 吃包子 1.2 Lock 1.2.1 lock方法 1.2.2 计数器 1.2.3 非阻塞 ...

  3. ubuntu12.04 svn ssl错误

    1,ubuntu12.04 svn ssl错误提示: OPTIONS of '<url>': SSL handshake failed: SSL error: Key usage viol ...

  4. shell读取文件的每一行内容并输出【转】

    写法一: #!/bin/bash while read line do echo $line done < file(待读取的文件) 写法二: #!/bin/bash cat file(待读取的 ...

  5. Geoserver WFS跨域设置

    测试版本为geoserver2.11.0. 两种方法都可以实现跨域设置: 第一种: 下载跨域jar包jetty-servlets.jar(下载geoserver使用的对应jetty版本——可以查看&l ...

  6. java版云笔记(五)

    下来是创建笔记本,创建笔记,这个没什么难点和前面是一样的. 创建笔记本 首先点击"+"弹出添加笔记的对话框,然后点击确定按钮创建笔记本. //点击"+"弹出添加 ...

  7. reverse和reverse_copy函数的应用

    reverse函数的作用是:反转一个容器内元素的顺序.函数参数:reverse(first,last);//first为容器的首迭代器,last为容器的末迭代器.它没有任何返回值. 这个函数比较简单, ...

  8. free之后将指针置为NULL

    free一个指针,只是将指针指向的内存空间释放掉了,并没有将指针置为NULL,指针仍指向被释放掉的内存的地址,在判断指针是否为NULL的时候,通常是通过if(pt == NULL) ,这时,导致指针成 ...

  9. Jmeter------将JDBC Request的查询结果作为另一个接口的请求参数

    一.前言 jmeter已配置连接成功数据库,不会的可查看:https://www.cnblogs.com/syw20170419/p/9832402.html 二.需求 将JDBC Request的r ...

  10. 想弄一弄tensorflow,先弄numpy

    现在晚上凉快点了, 下班回家可以学会东东了.. 这次的书是一个印度人写的. 按着示例代码弄起先.. #!/usr/bin/env python # -*- coding: utf-8 -*- impo ...