codeforces 658D. Bear and Polynomials
2016-03-30 23:00
441 查看
D. Bear and Polynomials
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
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.
Examples
Input
Output
Input
Output
Input
Output
Note
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.
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<stack>
#include <queue>
#include <set>
using namespace std;
const int inf = 2147483647;
const double PI = acos(-1.0);
const int mod = 1000000007;
long long a[200005], b[200005];
int main()
{
long long n, k;
while (~scanf("%I64d%I64d", &n, &k))
{
for (long long i = 0; i <= n; ++i)
{
scanf("%I64d", &a[i]);
b[i] = a[i];
}
for (long long i = 0; i < n; ++i)
{
b[i + 1] += b[i] / 2;
b[i] %= 2;
}
long long x = 0;
for (long long i = 0; i <= n; ++i)
{
if (b[i])
{
x = i;
break;
}
}
long long ans = 0;
long long s = 0;
for (long long i = n; i >= 0; --i)
{
s = s * 2 + b[i];
if (abs(s) > 1e10)
break;
if (i <= x)
{
long long p = abs(s - a[i]);
if (p == 0 && i == n)
continue;
if (p <= k)
ans++;
}
}
printf("%I64d\n", ans);
}
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
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.
Examples
Input
3 1000000000 10 -9 -3 5
Output
3
Input
3 12 10 -9 -3 5
Output
2
Input
2 20 14 -7 19
Output
0
Note
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, 问有几种可能;
思路:直接求和肯定是不行的,太大了, 又因为它叫我的求的是Q(2),所以我们可以根据二进制吧所有值都放到最高位来计算。
如果进位是遇到奇数, 那么能改的系数位数只能在0 - 该位, 因为无论怎么改后面的,前面都不会为0;
遍历的时候从后往前,如果遇到值某一位的值很大,那么直接break即可,因为前面怎么改都不会为0;
#include<iostream>#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<stack>
#include <queue>
#include <set>
using namespace std;
const int inf = 2147483647;
const double PI = acos(-1.0);
const int mod = 1000000007;
long long a[200005], b[200005];
int main()
{
long long n, k;
while (~scanf("%I64d%I64d", &n, &k))
{
for (long long i = 0; i <= n; ++i)
{
scanf("%I64d", &a[i]);
b[i] = a[i];
}
for (long long i = 0; i < n; ++i)
{
b[i + 1] += b[i] / 2;
b[i] %= 2;
}
long long x = 0;
for (long long i = 0; i <= n; ++i)
{
if (b[i])
{
x = i;
break;
}
}
long long ans = 0;
long long s = 0;
for (long long i = n; i >= 0; --i)
{
s = s * 2 + b[i];
if (abs(s) > 1e10)
break;
if (i <= x)
{
long long p = abs(s - a[i]);
if (p == 0 && i == n)
continue;
if (p <= k)
ans++;
}
}
printf("%I64d\n", ans);
}
}
相关文章推荐
- 单源最短路径Dijstra算法
- 服务启动初始化相关配置 如XML、properties、log等文件
- HDU-2639 Bone Collector II
- 0330复利计算4.0(改)
- AndroidStudio 放弃Gradle使用Android.mk
- 环境变量——关于PATH与CLASSPATH的区别
- 04自定义控制器
- 2016.3.21html标题,段落与格式化
- JVM垃圾回收之我见
- static详解
- FZU 2147 A-B Game
- 解决 adb not responding. if you'd like to retry then please manually kill adb.
- 设计模式之 简单工厂模式(C++实现)
- 程序员老梗
- 如何更改SQL Server 2008 登陆验证方式
- [android] 服务的生命周期(混合方式)
- #java读书笔记#多线程3
- JS样式--切换特效
- 8--项目采购管理、信息管理和配置
- android home键,back返回键监听,Window窗体监听返回键