codeforces 658D D. Bear and Polynomials(数学)
题目链接:
2 seconds
256 megabytes
standard input
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.
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.
Print the number of ways to change one coefficient to get a valid polynomial Q that Q(2) = 0.
- 3 1000000000
10 -9 -3 5
- 3
- 3 12
10 -9 -3 5
- 2
- 2 20
14 -7 19
- 0
In the first sample, we are given a polynomial P(x) = 10 - 9x - 3x2 + 5x3.
Limak can change one coefficient in three ways:
- He can set a0 = - 10. Then he would get Q(x) = - 10 - 9x - 3x2 + 5x3 and indeed Q(2) = - 10 - 18 - 12 + 40 = 0.
- Or he can set a2 = - 8. Then Q(x) = 10 - 9x - 8x2 + 5x3 and indeed Q(2) = 10 - 18 - 32 + 40 = 0.
- 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代码:
- /*
- 2014300227 658D - 26 GNU C++11 Accepted 576 ms 5308 KB
- */
- #include <bits/stdc++.h>
- using namespace std;
- const int N=2e5+;
- long long a[N],b[N+];
- int n,l=,k;
- int main()
- {
- scanf("%d%d",&n,&k);
- for(int i=;i<=n;i++)
- {
- cin>>a[i];
- b[i]=a[i];
- }
- for(int i=;i<n;i++)
- {
- a[i+]+=a[i]/;
- a[i]=a[i]%;
- }
- for(int i=;i<=n;i++)
- {
- if(a[i])
- {
- l=i;
- break;
- }
- }
- int answ=;
- long long sum=;
- for(int i=n;i>=;i--)
- {
- sum=sum*+a[i];
- if(abs(sum)>1e10)break;
- if(i<=l)
- {
- long long x=abs(sum-b[i]);
- if(x==&&i==n)continue;
- if(x<=k)answ++;
- }
- }
- cout<<answ<<"\n";
- return ;
- }
codeforces 658D D. Bear and Polynomials(数学)的更多相关文章
- CodeForces 639C Bear and Polynomials
Bear and Polynomials 题解: 如果改变一个其中的一个数,那么需要知道的是,前面的数都可以进到当前位来,如果过不来的话,那么就会因为前面有数导致无法变成0. 所以我们将前面的数不断向 ...
- 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 ...
- Codeforces 658D Bear and Polynomials【数学】
题目链接: http://codeforces.com/contest/658/problem/D 题意: 给定合法多项式,改变一项的系数,使得P(2)=0,问有多少种方法? 分析: 暴力求和然后依次 ...
- Codeforces 902D/901B - GCD of Polynomials
传送门:http://codeforces.com/contest/902/problem/D 本题是一个数学问题——多项式整除. 对于两个整数a.b,求最大公约数gcd(a,b)的辗转相除法的函数如 ...
- 【32.89%】【codeforces 574D】Bear and Blocks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 735C:Tennis Championship(数学+贪心)
http://codeforces.com/problemset/problem/735/C 题意:有n个人打锦标赛,淘汰赛制度,即一个人和另一个人打,输的一方出局.问这n个人里面冠军最多能赢多少场, ...
- 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 ...
- codeforces 680B B. Bear and Finding Criminals(水题)
题目链接: B. Bear and Finding Criminals //#include <bits/stdc++.h> #include <vector> #includ ...
- codeforces 680A A. Bear and Five Cards(水题)
题目链接: A. Bear and Five Cards //#include <bits/stdc++.h> #include <vector> #include <i ...
随机推荐
- centos7 安装rocketmq(quick start)
Quick Start This quick start guide is a detailed instruction of setting up RocketMQ messaging system ...
- php计算两个经纬度地点之间的距离(转)
php计算两个指定的经纬度地点之间的距离,这个在做计算给定某个地点的经纬度,计算其附近的商业区,以及给定地点与附近各商业区之间的距离的时候,还是用的到的.下面是具体的函数代码以及用法示例. 关于如何获 ...
- Java NIO之Charset类字符编码对象
介绍 java中使用Charset来表示编码对象 This class defines methods for creating decoders and encoders and for retri ...
- 删除反复行SQL举例
删除反复行SQL实验简单举例 说明:实验按顺序进行.前后存在关联性.阅读时请注意.打开文件夹更便于查看. 构造实验环境: SQL> select count(*) from emp; COU ...
- Content Security Policy
资料来源:阮一峰博客 一.背景 XSS最常见,危害最大的网页安全漏洞,“网页安全政策”从根本上解决问题 二.简介 CSP的实质是白名单制度,明确告诉客户端那些外部资源可以加载和执行. CSP 大大增强 ...
- intellij 开发webservice
最近项目中有用到WebService,于是就研究了一下,但是关于intellij 开发 WebService 的文章极少,要不就是多年以前,于是研究一下,写这篇博文.纯属记录,分享,中间有不对的地方, ...
- PowerBuilder -- 结构类型(structure)
http://bbs.csdn.net/topics/3501120743楼答复 PB的structure分两种,全局的和局部的,两者只有作用域不同. 全局的在file/new/pb object/s ...
- PowerBuilder -- 变更某列的背景色
记得把background.mode设置为2 li_col++ ls_col[li_col] = ls_fit_no ls_column = ' col' + String(li_col) ls_co ...
- python MySQLdb Windows下安装教程及问题解决方法(python2.7)
使用python访问mysql,需要一系列安装 linux下MySQLdb安装见 Python MySQLdb在Linux下的快速安装http://www.jb51.net/article/6574 ...
- python 基础 6.1 异常处理方法
一. Excepthion 异常类 Excepthion 是所有的异常基础类(),对于python 的标准异常,我们列出如下,以做参考: 异常名称 ...