VK Cup 2016 - Round 1 (Div. 2 Edition) D. Bear and Polynomials
2016-03-29 10:06
591 查看
D. Bear and Polynomials
题目连接:
http://www.codeforces.com/contest/658/problem/DDescription
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 100000000010 -9 -3 5
Sample Output
3Hint
题意
给你一个多项式,然后告诉你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; }
相关文章推荐
- 《Javascript权威指南》学习笔记之十九--HTML5 DOM新标准---处理文档元信息和管理交互能力
- Ubuntu下设置Eclipse支持GBK和GB2312
- oracle basic sql
- 密码强中弱,短信提示时间按钮
- 【BZOJ3100】排列【杂项】
- hibernate.properties配置文件
- AOP实现原理
- 2.1TCP Socket(含SSL)通讯库GCDAsyncSocket(二)
- android 5.0 录像模式下只有手动对焦没有自动对焦的问题
- 深入Java集合学习之HashMap的实现原理详解
- Hadoop集群的配置的主机和IP
- 利用 LibWebP-NET 解码与编码 WebP 格式图片
- 【JS】javascript 正则表达式 大全 总结
- failed to lazily initialize a collection of role
- guava学习笔记-本地缓存工具
- Python中的字典排序
- 红眼病治愈方法
- OPTIMIZE TABLE
- 判断字符串是否为空的方法
- 类的初始化