累加器(河南萌新2024)

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef double db;
typedef long double ldb;
typedef pair<int, int> pii;
typedef pair<ll, ll> PII;
#define pb emplace_back
//#define int ll
#define all(a) a.begin(),a.end()
#define x first
#define y second
#define ps push_back
#define endl '\n'
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)

void solve();

const int N = 2e6 + 10;

signed main() {
    IOS;
    solve();
    return 0;
}

ll sb(ll a)
{
    ll ans = 0;
    ll a1 = a-1,a2 = a;
    for(int i = 0; i <= 30; ++ i)
    {
        if((a1&(1<<i)) != (a2&(1<<i)))
            ans ++;
    }
    return ans;
}

void solve() {
    vector<ll> a(N);
    for(int i = 1; i < N; ++ i)
    {
        a[i] = sb(i);
        a[i] += a[i-1];
    }
    ll t;
    cin >> t;
    while(t--)
    {
        ll x,y;
        cin >> x >> y;
        y = x + y;
        cout << a[y] - a[x] << endl;
    }
}
相关推荐
哎嗨人生公众号9 小时前
手写求导公式,让轨迹优化性能飞升,150ms变成9ms
开发语言·c++·算法·机器人·自动驾驶
foundbug9999 小时前
STM32 内部温度传感器测量程序(标准库函数版)
stm32·单片机·嵌入式硬件·算法
Hello.Reader9 小时前
为什么学线性代数(一)
线性代数·算法·机器学习
_深海凉_10 小时前
LeetCode热题100-找到字符串中所有字母异位词
算法·leetcode·职场和发展
木井巳10 小时前
【递归算法】目标和
java·算法·leetcode·决策树·深度优先
旖-旎10 小时前
哈希表(字母异位次分组)(5)
数据结构·c++·算法·leetcode·哈希算法·散列表
别或许10 小时前
4、高数----一元函数微分学的计算
人工智能·算法·机器学习
_深海凉_10 小时前
LeetCode热题100-最长连续序列
算法·leetcode·职场和发展
这里没有酒10 小时前
[信息安全] AES128 加密/解密 --> state 矩阵
算法
无限进步_10 小时前
【C++】多重继承中的虚表布局分析:D类对象为何有两个虚表?
开发语言·c++·ide·windows·git·算法·visual studio