累加器(河南萌新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;
    }
}
相关推荐
电鱼智能的电小鱼1 小时前
基于电鱼 AI 工控机的智慧工地视频智能分析方案——边缘端AI检测,实现无人值守下的实时安全预警
网络·人工智能·嵌入式硬件·算法·安全·音视频
孫治AllenSun2 小时前
【算法】图相关算法和递归
windows·python·算法
格图素书3 小时前
数学建模算法案例精讲500篇-【数学建模】DBSCAN聚类算法
算法·数据挖掘·聚类
DashVector4 小时前
向量检索服务 DashVector产品计费
数据库·数据仓库·人工智能·算法·向量检索
AI纪元故事会4 小时前
【计算机视觉目标检测算法对比:R-CNN、YOLO与SSD全面解析】
人工智能·算法·目标检测·计算机视觉
夏鹏今天学习了吗4 小时前
【LeetCode热题100(59/100)】分割回文串
算法·leetcode·深度优先
卡提西亚4 小时前
C++笔记-10-循环语句
c++·笔记·算法
还是码字踏实4 小时前
基础数据结构之数组的双指针技巧之对撞指针(两端向中间):三数之和(LeetCode 15 中等题)
数据结构·算法·leetcode·双指针·对撞指针
Coovally AI模型快速验证6 小时前
当视觉语言模型接收到相互矛盾的信息时,它会相信哪个信号?
人工智能·深度学习·算法·机器学习·目标跟踪·语言模型
电院工程师6 小时前
SIMON64/128算法Verilog流水线实现(附Python实现)
python·嵌入式硬件·算法·密码学