Decoding of Varints Gym - 101611D

本文解析了一道复杂的算法题,题目要求处理n个128进制系数的数,通过求和判断结果的奇偶性来决定输出格式。详细介绍了如何使用C++处理大数运算,避免溢出,并给出了具体的实现代码。

题目链接

这道题是真的长,果然学长们读题超厉害,我们都没读懂题目,实际上这道题是说给你n个数是几个数的128进制系数,求出来每个数非负数sum后,如果求出来是偶数,直接输出x为sum / 2,否则输出x为-sum / 2 + 1。由于x的范围刚好是long long即-2^63 ~ 2^63-1,因此sum的范围就是unsigned long long即0~2^64,这里有一点特殊技巧就是当sum为奇数时不能用(sum + 1) / 2,这样会爆。其他就没了,加油鸭

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main()
{
    int n,x,cnt = 0;
    ull sum = 0;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&x);
        if(x < 128)
        {
            sum += x * (1ll << 7 * cnt);
            if(sum % 2) printf("-%llu\n",sum / 2 + 1);
            else printf("%llu\n",sum / 2);
            sum = cnt = 0;
        }
        else
        {
            sum += (x - 128) * (1ll << 7 * cnt);
            cnt++;
        }
    }
    return 0;
}

 

Output options: -1 List in one column -verbose Verbose listing -select val Select a single algorithm -commands List of standard commands -standard-commands List of standard commands -digest-commands List of message digest commands (deprecated) -digest-algorithms List of message digest algorithms -kdf-algorithms List of key derivation and pseudo random function algorithms -random-instances List the primary, public and private random number generator details -random-generators List of random number generators -mac-algorithms List of message authentication code algorithms -cipher-commands List of cipher commands (deprecated) -cipher-algorithms List of symmetric cipher algorithms -encoders List of encoding methods -decoders List of decoding methods -key-managers List of key managers -key-exchange-algorithms List of key exchange algorithms -kem-algorithms List of key encapsulation mechanism algorithms -signature-algorithms List of signature algorithms -asymcipher-algorithms List of asymmetric cipher algorithms -public-key-algorithms List of public key algorithms -public-key-methods List of public key methods -store-loaders List of store loaders -providers List of provider information -engines List of loaded engines -disabled List of disabled features -options val List options for specified command -objects List built in objects (OID<->name mappings) Provider options: -provider-path val Provider load path (must be before 'provider' argument if required) -provider val Provider to load (can be specified multiple times) -propquery val Property query used when fetching algorithms
最新发布
06-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值