Bear and Polynomials 639 C
来源:互联网 发布:centos如何安装ssh 编辑:程序博客网 时间:2024/06/05 09:21
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 100000000010 -9 -3 5
3
3 1210 -9 -3 5
2
2 2014 -7 19
0
题意:有一个多项式ai*2^i,其和不为0,让你改变其中一个ai使得和为0,问一共有多少种改变方式。
思路:我们把这个和转化成二进制的形式,简便处理,让每一位上可以是-1,0,1,如果某一个ai可以作为一种答案改变,那么其低位上的数字肯定全是0。这样我们设只有低位的pos个可能成为答案。然后从高位往低位做运算,算出这个ai需要改变多少。当需要改变的量>3*K的时候(可能2*K也可以),低位是不可能把数目给补回来的,也就不用继续算了,这样能保证在long的范围内。
AC代码如下:
#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<cmath>#include<algorithm>#include<map>using namespace std;typedef long long ll;int T,t,n,m;ll num[210000],s[210000],K,ans,ret,ret2,temp;int main(){ int i,j,k,pos; scanf("%d%I64d",&n,&K); for(i=0;i<=n;i++) scanf("%I64d",&num[i]); for(i=0;i<=n+40;i++){ s[i]+=num[i]; s[i+1]+=s[i]/2; s[i]=s[i]%2; } for(i=0;i<=n+40;i++) if(s[i]!=0) break; pos=min(n,i); ret=0; j=n; for(i=n+40;i>pos;i--){ ret=ret*2+s[i]; if(ret>K*3 || ret<-K*3){ printf("0\n"); return 0; } } for(i=pos;i>=0;i--){ ret=ret*2+s[i]; if(ret>K*3 || ret<-K*3) break; ret2=num[i]-ret; if(-K<=ret2 && ret2<=K && !(i==n &&ret2==0)) ans++; } printf("%d\n",ans);}
- Bear and Polynomials 639 C
- codeforces 639C Bear and Polynomials(数学)
- Codeforces 639C Bear and Polynomials 【思维】
- Codeforces 658D Bear and Polynomials【数学】
- codeforces 658D. Bear and Polynomials
- CodeForces 658D Bear and Polynomials
- Codeforces 658D Bear and Polynomials
- CodeForces 657B Bear and Polynomials
- C. Bear and Different Names
- VK Cup 2016 - Round 1 (Div. 2 Edition) D. Bear and Polynomials
- 【VK Cup 2016 - Round 1 (Div 2 Edition)D Bear and Polynomials
- Codeforces 385 C. Bear and Prime Numbers
- CodeForces 385C Bear and Prime Numbers
- Codeforces 385C Bear and Prime Numbers
- Codeforces 385 C Bear and Prime Numbers
- codeforces 385C Bear and Prime Numbers
- CodeForces 574C - Bear and Poker
- Codeforces Round #318 C - Bear and Poker
- Longest Substring with At Most K Distinct Characters
- 五种Button监听方式总结
- npm介绍
- HttpUtils
- JS编写冒泡排序法和二分查找
- Bear and Polynomials 639 C
- 设计模式:组合模式(Composite)
- 先根递归建立二叉树(顺序表示法)
- yii2分页
- Ubuntu安装Torch
- 遗传算法在游戏中的应用——概述
- spring-boot项目在外部tomcat环境下部署
- 一道笔试题
- 安卓学习笔记(三)使用Intent在活动中穿梭