Decoding of Varints

Statements

Varint is a type used to serializing integers using one or more bytes. The key idea is to have smaller values being encoded with a smaller number of bytes.

First, we would like to encode some unsigned integer x. Consider its binary representation x = a0a1a2... ak - 1, where ai-th stands for the i-th significant bit, i.e. x = a0·20 + a1·21 + ... + ak - 1·2k - 1, while k - 1 stands for the index of the most significant bit set to 1 or k = 1 if x = 0.

To encode x we will use bytes b0, b1, ..., bm - 1. That means one byte for integers from 0 to 127, two bytes for integers from 128 to 214 - 1 = 16383 and so on, up to ten bytes for 264 - 1. For bytes b0, b1, ..., bm - 2 the most significant bit is set to 1, while for byte bm - 1 it is set to 0. Then, for each i from 0 to k - 1, i mod 7 bit of byte is set to ai. Thus,

x = (b0 - 128)·20 + (b1 - 128)·27 + (b2 - 128)·214 + ... + (bm - 2 - 128)·27·(m - 2) + bm - 1·27·(m - 1)

In the formula above we subtract 128 from b0, b1, ..., bm - 2 because their most significant bit was set to 1.

For example, integer 7 will be represented as a single byte b0 = 7, while integer 260 is represented as two bytes b0 = 132 and b1 = 2.

To represent signed integers we introduce ZigZag encoding. As we want integers of small magnitude to have short representation we map signed integers to unsigned integers as follows. Integer 0 is mapped to 0,  - 1 to 1, 1 to 2,  - 2 to 3, 2 to 4,  - 3 to 5, 3 to 6 and so on, hence the name of the encoding. Formally, if x ≥ 0, it is mapped to 2x, while if x < 0, it is mapped to  - 2x - 1.

For example, integer 75 is mapped to 150 and is encoded as b0 = 150, b1 = 1, while  - 75 will be mapped to 149 and will be encoded as b0 = 149, b1 = 1. In this problem we only consider such encoding for integers from  - 263 to 263 - 1 inclusive.

You are given a sequence of bytes that corresponds to a sequence of signed integers encoded as varints. Your goal is to decode and print the original sequence.

Input

The first line of the input contains one integer n (1 ≤ n ≤ 10 000) — the length of the encoded sequence. The next line contains n integers from 0 to 255. You may assume that the input is correct, i.e. there exists a sequence of integers from  - 263 to 263 - 1 that is encoded as a sequence of bytes given in the input.

Output

Print the decoded sequence of integers.

Example

Input
5
0 194 31 195 31
Output
0
2017
-2018 首先需要用unsigned long long,除2前值为long long的两倍。
再一个就是先除2再加1,避免先加后值越界。
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const int MAX = ; ll mi[];
ll a[MAX]; void init(){
mi[]=;
for(ll i=;i<=;i++){
mi[i]=mi[i-]*;
}
}
int main(void)
{
int t,i,j;
ll n,x;
init();
scanf("%I64u",&n);
for(i=;i<=n;i++){
scanf("%I64u",&a[i]);
}
ll ans=;int l=;
for(i=;i<=n;i++){
if(a[i]<){
ans+=a[i]*mi[l*];
if(ans&) printf("-%I64u\n",ans/+);
else printf("%I64u\n",ans/); ans=;l=;
continue;
}
ans+=(a[i]-)*mi[l*];
l++;
}
return ;
}

Gym - 101611D Decoding of Varints(边界值处理)的更多相关文章

  1. Gym - 101611D Decoding of Varints(阅读理解题 )

    Decoding of Varints ​ 题意&思路: 首先根据红色边框部分的公式算出x,再有绿色部分得知,如果x是偶数则直接除以2,x是奇数则(x+1)/-2. PS:这题有数据会爆掉un ...

  2. 2017-2018 ACM-ICPC, NEERC, Moscow Subregional Contest

    A. Advertising Strategy 最优策略一定是第一天用$y$元,最后一天再用$x-y$元补满. 枚举所有可能的$y$,然后模拟即可,天数为$O(\log n)$级别. 时间复杂度$O( ...

  3. go语言标准库 时刻更新

    Packages   Standard library Other packages Sub-repositories Community Standard library ▾ Name Synops ...

  4. 08 Packages 包

    Packages   Standard library Other packages Sub-repositories Community Standard library Name Synopsis ...

  5. Codeforces Gym 100002 D"Decoding Task" 数学

    Problem D"Decoding Task" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com ...

  6. varints

    Protocol Buffer技术详解(数据编码) - Stephen_Liu - 博客园 https://www.cnblogs.com/stephen-liu74/archive/2013/01/ ...

  7. Block Markov Coding & Decoding

    Block Markov coding在一系列block上进行.在除了第一个和最后一个block上,都发送一个新消息.但是,每个block上发送的码字不仅取决于新的信息,也跟之前的一个或多个block ...

  8. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  9. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

随机推荐

  1. Mac下下载 mysql8.0

    终端输入一下的命令,将文件下载下来 wget --header="Cookie:MySQL_S=u1ddsnr95sraoqjcu4og46ojrcapim37; MyGUID=59f3a5 ...

  2. php验证身份证号码有效性

    <?php // 18位身份证校验码有效性检查 // idcard_checksum18('...'); function idcard_checksum18($idcard) { if (st ...

  3. Hadoop-2.2.0中文文档—— MapReduce 下一代--容量调度器

    目的 这份文档描写叙述 CapacityScheduler,一个为Hadoop能同意多用户安全地共享一个大集群的插件式调度器,如他们的应用能适时被分配限制的容量. 概述 CapacitySchedul ...

  4. T_CODE I18N

    关于T-CODE I18N 最近由于看到很多人遇到SMARTFORMS不能拖拽字段的问题,这个的解决方案 I18N:解决SMARTFORMS的不能从Field name 那边直接把变量拖入右边编辑框 ...

  5. Javascript代码收集

    1.模仿jquery each 原文地址 function each(obj, fn){ var i; if(Object.prototype.toString.call(obj) === '[obj ...

  6. qemu仿真执行uboot和barebox

    先安装qemu: apt-get install qemu-system 交叉编译器可以选择友善之臂:http://arm9download.cncncn.com/mini2440/linux/arm ...

  7. 《CSS权威指南(第三版)》---第六章 文本属性

    本章主要的内容是: 1.文本缩进: text-indent.行内元素无法缩进,一般用左内边距或外边距来创造这种效果. 2.文本对齐:text-align .只应用于块状元素. 3.行高:一般line- ...

  8. 对C++指针的一个比喻

    假如你身在上海,你的电脑出了一点问题,你解决不了,这时你想起了你的远在北京的朋友小D,此时小D打开了他的心爱的笔记本... c++中函数的参数传指针就像你用teamviewer与你的朋友小D建立连接, ...

  9. HTML/CSS实现的搜索框

    谷歌和百度首页的搜索框都是<input>+<button>模式的,bing的搜索框感觉要好点儿.简言之,就是将提交按钮放到<input>中,其实这是做不到的,只能伪 ...

  10. db2move 数据导出整理

    db2move <database-name> <action> [<option>   <value>] 命令解释:1).database-name, ...