Codeforces 639C Bear and Polynomials 【思维】

题目链接:Codeforces 639C Bear and Polynomials

C. Bear and Polynomials
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

题意:给定一个2的n项式的系数。每次可以将任意一个系数修改为绝对值不超过k的值,问你有多少种方案使得该多项式结果为0。

处理多项式学会了新的技能,ORZ。。。

思路:对于多项式 ni=0a[i]2i ,我们可以将 a[i] 的值加权到 2i+1 上,方法就是 a[i+1]=a[i+1]+a[i]2 a[i]=a[i]mod2 。这样处理后,多出了第n+1次幂,且前面所有幂次的系数为-1、0、1中的一个。之后我们可以倒着消去高次幂的系数,加到低次幂的系数上,具体是 a[i]=a[i]+a[i+1]2 a[i+1]=0
发现我们总是可以将后面项的系数合并到第i项上(0<=i<=n),假设合并到第i项系数为xx,此时第i项系数只有取a[i]-xx且前面所有项系数均为0才可以使得整个多项式结果为0。因为前面所有项的值sum满足 2i+1<=sum<=2i1 ,二者的和不可能为0。若标记第一个非0系数项为F,只有当项数i <= F时才是合法的,判定下是否合法即可。这里有两个trick
1,我们倒着向低次幂添加系数时,每次累乘2,若不加限制乘下去会爆,所以当系数 > 2*k时就跳出;
2,当n <= F时,说明0 - n-1项的系数全为0,这时当系数与原来的系数一样时是不合法的方案。

AC代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#define fi first
#define se second
#define ll o<<1
#define rr o<<1|1
#define CLR(a, b) memset(a, (b), sizeof(a))
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
const int MOD = 1e9 + 7;
const int MAXN = 2*1e5 + 10;
void add(LL &x, LL y) { x += y; x %= MOD; }
LL x[MAXN], y[MAXN];
int main()
{
    int n, k;
    while(scanf("%d%d", &n, &k) != EOF) {
        for(int i = 0; i <= n; i++) {
            scanf("%lld", &x[i]);
            y[i] = x[i];
        }
        y[n+1] = 0;
        for(int i = 0; i <= n; i++) {
            y[i+1] += y[i] / 2;
            y[i] %= 2;
            //printf("%lld\n", y[i+1]);
        }
        int F = n + 1;
        for(int i = 0; i <= n; i++) {
            if(y[i]) {
                F = i; break;
            }
        }
//        for(int i = 0; i <= n; i++) {
//            cout << y[i] << endl;
//        }
        int ans = 0; LL xx = y[n+1];
        for(int i = n; i >= 0; i--) {
            xx = 2 * xx + y[i];
            if(abs(xx) > 2 * k) break;
            if(i > F) continue;
            if(i == n && x[i] == xx) continue;
            ans += (abs(x[i] - xx) <= k);
        }
        printf("%d\n", ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值