Bear and Polynomials 639 C
2016-03-29 21:24
696 查看
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
题意:有一个多项式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);
}
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
题意:有一个多项式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);
}
相关文章推荐
- leetcode之旅(9)-Reverse Linked List
- Error:Makefile:1449:*** mixed implicit and normal rules: deprecated syntax
- docker ubuntu容器更换阿里源
- [置顶] fiddler抓包HTTPS请求
- 远程线程的注入和注出
- leetcode之旅(9)-Reverse Linked List
- leetcode之旅(9)-Reverse Linked List
- Check failed: error == cudaSuccess (8 vs. 0) invalid device function
- 欧几里德算法求最大公约数
- iOS个人整理35-即时通信1 XMPP openfire 登录注册
- Android Studio--android:descendantFocusability用法简析
- c# 作业2
- 无向图的欧拉路
- BZOJ 3049: [Usaco2013 Jan]Island Travels
- java守护线程
- javascript 跨域请求
- Linux开发环境搭建 二 (Ubuntu串口工具minicom的安装及使用)
- Zlib库的安装与使用
- redis入门及java操作
- 软件测试homework3