Inna and Binary Logic
Codeforces Round #234 (Div. 2) E:http://codeforces.com/problemset/problem/400/E
题意:给你n个数,然后每相邻的两个数可以通过and运算生成一个新的数,然后这些新生成的n-1个数每相邻的两个数又通过and运算成n-2个数,最后只会剩下一个数,然后让你求这n(n+1)/2个数的和,然后每一次会更新最底层的某个数,然后操作之后,输出刚才的总和。
题解:这一题,虽然是看题解,然后自己敲出来的,但是还是有点成就感和收获。首先,这一题的思路很巧妙。如果所有的数都是1或者0,加入说序列是1110001,通过计算,发现其实和就是(3+1)*3/2+(1+1)*1/2==7,与连续1的个数有关,加入连续1的个数是x,那么这连续的x个1,形成的和就是(x+1)*x/2;总和就是把所有连续的1和相加。想到这里,就可以知道,数的范围是1
e5,最多是2^17,所以可以把每个数拆成17位,每一位要么是0或者是1,这样只要统计底层的连续1有多少就可以了。num[i][j]表示第j个数的第i位,一开始我们可以计算出总和,然后更新时,如果更新数的这一位和原来相同则这一位不用变化,否则,如果是1要0,可以把ans先减去原来连续个一所形成的和,然后加上这个数左边连续1的和以及右边连续1的和,然后把这一位变成0,如果是0变1,则相反。这一要注意数据范围,在过程中有可能爆int,所以要用long long,并且在求和过程中使用的局部变量也要用long long,由于自己没有注意到这样的问题结果wa 2发,int和long long之间转换出了问题。也许,有人会问这样会不会t,首先事实上没有t,而且跑的很快。从理论上讲,也不会,因为要出现很长的连续的1是很难的,必须保证这个连续的数在某些位上都是1,并且连续,很难,这样的数据很难。所以很快。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=1e5+;
long long num[][N];
long long ans;
long long a[N],v;
int n,m,p;
long long tmp[];
int main(){
while(~scanf("%d%d",&n,&m)){
memset(num,,sizeof(num));
memset(a,,sizeof(a));
memset(tmp,,sizeof(tmp));
for(int i=;i<=n;i++){
scanf("%I64d",&a[i]);
for(int j=;j<=;j++){
num[j][i]=(a[i]&);
a[i]/=;
}
}
ans=;
for(int j=;j<=;j++){
long long temp=,tp=;
for(int i=;i<=n;i++){
if(num[j][i]==){
tp+=temp*(temp+)/;
temp=;
}
else{
temp++;
}
if(i==n){
tp+=temp*(temp+)/;
temp=;
}
}
ans+=(tp<<j);
}
for(int i=;i<=m;i++){
scanf("%d%I64d",&p,&v);
for(int j=;j<=;j++){
tmp[j]=(v&);
v/=;
}
for(int j=;j<=;j++){
long long sum=;
int tt=p;
if(tmp[j]==num[j][p])continue;
long long lnum=,rnum=;
while(num[j][--tt])
lnum++;
tt=p;
while(num[j][++tt])
rnum++;
if(tmp[j]==&&num[j][p]==){
sum-=(lnum+rnum+)*(lnum+rnum+)/;
sum+=(lnum+)*lnum/;
sum+=(rnum+)*rnum/;
num[j][p]=;
}
else{
sum-=(lnum+)*lnum/;
sum-=(rnum+)*rnum/;
sum+=(lnum+rnum+)*(lnum+rnum+)/;
num[j][p]=;
}
ans+=(sum<<j);
}
printf("%I64d\n",ans);
}
} }
Inna and Binary Logic的更多相关文章
- codeforces 400E. Inna and Binary Logic 线段树
题目链接 给出n个数, 定义a[1][i]为这初始的n个数, 然后a[i][j] = a[i-1][j]&a[i-1][j-1], 这样就可以得到一个三角形一共n*(n-1)/2个数. 给出一 ...
- cf div2 234 E
E. Inna and Binary Logic time limit per test 3 seconds memory limit per test 256 megabytes input sta ...
- CodeForces 400
A - Inna and Choose Options Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d &a ...
- A trip through the Graphics Pipeline 2011_09_Pixel processing – “join phase”
Welcome back! This post deals with the second half of pixel processing, the “join phase”. The pre ...
- Java TreeMap 源码解析
继上篇文章介绍完了HashMap,这篇文章开始介绍Map系列另一个比较重要的类TreeMap. 大家也许能感觉到,网络上介绍HashMap的文章比较多,但是介绍TreeMap反而不那么多,这里面是有原 ...
- Method, apparatus, and system for speculative abort control mechanisms
An apparatus and method is described herein for providing robust speculative code section abort cont ...
- Satisfying memory ordering requirements between partial reads and non-snoop accesses
A method and apparatus for preserving memory ordering in a cache coherent link based interconnect in ...
- Logic BIST
Logic BIST is crucial for many applications, in particular for life-critical and mission-critical ap ...
- [LeetCode#156] Binary Tree Upside Down
Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left ...
随机推荐
- HTTP协议 状态码详解
http://www.cnblogs.com/TankXiao/archive/2013/01/08/2818542.html
- linux-3.0下input_dev模型按键驱动
该代码在FL2440开发板上测试通过,为方便教学,将驱动中的platform_device和platform_driver故意分为两个驱动模块. [guowenxue@centos6 input_kb ...
- Shell - 文件运算符
文件运算符 文件运算符 描述 -b file 检测 file 是否为块设备文件 -c file 检测 file 是否为字符设备文件 -d file 检测 file 是否为目录 -e fil ...
- java编程思想-异常
DynamicFields类的setField方法里面的getField方法抛出的异常NoSuchFieldException 为什么是throw new RuntimeException(e);
- 利用抽象、多态实现无反射的绿色环保ORM框架
最近一直在忙新公司的基础库建设,对系统架构.开发框架及快速开发平台的设计实施都积累了一定的实践经验. 一般的中小型的软件开发公司,如果按照技术储备来衡量软件项目的技术含量的评定依据是可行的.但如果光是 ...
- ASP.NET 3.5路由总结篇
URL Routing是非常重要的一块技术体系,笔者将URL Routing的知识进行梳理后得出本文,旨在同大家分享,希望能够起到抛砖引玉的作用. 1. 什么是URL Routing? 所谓UR ...
- C# gridview分頁導出excel
#region 导出Excel方法 //导出到Excel按钮 protected void btnExport_Click(object sender, EventArgs e) { Export(& ...
- Express难点解析
app.js 应用程序入口文件1.// view engine setup 设置视图引擎app.set('views', path.join(__dirname, 'views'));//告诉expr ...
- 用java写bp神经网络(四)
接上篇. 在(一)和(二)中,程序的体系是Net,Propagation,Trainer,Learner,DataProvider.这篇重构这个体系. Net 首先是Net,在上篇重新定义了激活函数和 ...
- 欢迎使用 Markdown 编辑器写博客
本Markdown编辑器使用StackEdit修改而来,用它写博客,将会带来全新的体验哦: Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接和图片上传 LaTex数学公式 UML ...