Problem Statement

Find the sum of popcounts of all integers between $1$ and $N$, inclusive, such that the remainder when divided by $M$ equals $R$.

Here, the popcount of a positive integer $X$ is the number of $1$s in the binary notation of $X$, that is, the number of non-negative integers $k$ such that the $2^k$s place is $1$.

For each input, process $T$ test cases.

Constraints

  • $1 \leq T \leq 10^5$
  • $1 \leq M \leq N \leq 10^9$
  • $0 \leq R < M$
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format. The first line is as follows:

$T$

$T$ test cases follow. Each of them is given in the following format:

$N$ $M$ $R$

Output

Print $T$ lines. The $i$-th line should contain the answer to the $i$-th test case.


Sample Input 1

2
12 5 1
6 1 0

Sample Output 1

6
9

In the $1$-st test case, the popcount of $1$ is $1$, the popcount of $6$ is $2$, and the popcount of $11$ is $3$, so $1+2+3=6$ should be printed.

In the $2$-nd test case, the popcount of $1$ is $1$, $2$ is $1$, $3$ is $2$, $4$ is $1$, $5$ is $2$, and $6$ is $2$, so $1+1+2+1+2+2=9$ should be printed.

看到这种形式,就能想到类欧。

考虑每一位分别计算,那么第 \(x\) 位大概是一个 \(\sum\limits_{i=0}^{\lfloor\frac {n-r}m\rfloor}\lfloor\frac {im+r}{2^x}\rfloor\) 之类的东西。但这样弄只会弄出来所有模 \(m\) 余 \(r\) 的数二进制下前 \(x\) 位所表示的二进制数之和,而不能得到刚好第 \(x\) 位为1的数的数量。

但是可以容斥一下。设 \(f_x\) 为用类欧求出的前 \(x\) 位之和,那么 \(f_x-2f_{x+1}\) 就是刚好第 \(x\) 位为1的数的数量了。

#include<cstdio>
typedef long long LL;
int t,n,m,r;
LL f[35],ans;
LL calc(int a,int b,int c,int n)
{
if(!a)
return (b/c)*(n+1);
if(a>=c||b>=c)
return 1LL*n*(n+1)/2*(a/c)+1LL*(n+1)*(b/c)+calc(a%c,b%c,c,n);
LL m=(1LL*a*n+b)/c;
return n*m-calc(c,c-b-1,a,m-1);
}
int main()
{
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&m,&r);
for(int i=0;i<=30;i++)
f[i]=calc(m,r,1<<i,(n-r)/m);
for(int i=ans=0;i<=30;i++)
f[i]-=f[i+1]*2,ans+=f[i];
// putchar('\n');
printf("%lld\n",ans);
}
}

[ABC283Ex] Popcount Sum的更多相关文章

  1. 直观比较 popcount 的效率差异

    问题 求 \(\sum\limits_{i=1}^{3\times 10^8} popcount(i)\) . 仅考虑在暴力做法下的效率. 枚举位 __builtin_popcount #includ ...

  2. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  3. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  4. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  5. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  6. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  9. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  10. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

随机推荐

  1. Mybatis-Plus+Nacos+Dubbo进行远程RPC调用保姆级教程

    默认你已经看过我之前的教程了,并且拥有上个教程完成的项目, 之前的教程 https://www.cnblogs.com/leafstar/p/17638782.html 1.在bank1的pom文件中 ...

  2. 【pytorch】目标检测:新手也能彻底搞懂的YOLOv5详解

    YOLOv5是Glenn Jocher等人研发,它是Ultralytics公司的开源项目.YOLOv5根据参数量分为了n.s.m.l.x五种类型,其参数量依次上升,当然了其效果也是越来越好.从2020 ...

  3. VS2015项目.net-framework-4.5.2升级或新建项目无法选择framework 4.6.2(解决办法)

    VS2015里面没有.NET Framework 4.6.2 VS2015默认安装的目标框架最高是.NET Framework 4.6.1,但是我的项目里面某些NuGet软件包更新需要依赖.NET F ...

  4. Kioptrix: Level 1 (#1) 古老的Apache Samba VULN

    0×01 Vulnhub靶机渗透总结之 Kioptrix: Level 1 (#1) 系列专栏:Vulnhub靶机渗透系列 欢迎大佬:点赞️收藏关注 首发时间: 2023年8月20日 如有错误 还望告 ...

  5. 当小白遇到FullGC

    起初没有人在意这场GC,直到它影响到了每一天! 前言 本文记录了一次排查FullGC导致的TP99过高过程,介绍了一些排查时思路,线索以及工具的使用,希望能够帮助一些新手在排查问题没有很好的思路时,提 ...

  6. 如何通过API接口获取1688的商品详情

    1688是中国最大的B2B电商平台之一,吸引了大量的国内外买家和卖家,提供了丰富的商品资源.许多开发者和企业想要通过API接口获取1688商品的详细信息,以便于进行商品数据分析.价格监控等工作.在本文 ...

  7. C++20起支持的一个小特性

    注释掉的为传统的写法,从C++20起支持default关键字修饰的写法,即使是成员变量有多个的时候也支持,减轻了程序员的心智负担.

  8. MySQL系列之备份恢复——运维在备份恢复方面、备份类型、备份方式及工具、逻辑备份和物理备份、备份策略、备份工具使用-mysqldump、企业故障恢复案例、备份时优化参数、MySQL物理备份工具

    文章目录 1. 运维在数据库备份恢复方面的职责 1.1 设计备份策略 1.2 日常备份检查 1.3 定期恢复演练(测试库) 1.4 故障恢复 1.5 迁移 2. 备份类型 2.1 热备 2.2 温备 ...

  9. Go 语言的前生今世与介绍

    Go 语言的前生今世与介绍 目录 Go 语言的前生今世与介绍 一. Go 语言的发展 1.1 Go 语言是如何诞生的? 1.2 Go语言的早期团队和演进历程 1.3 Go语言正式发布并开源 1.4 G ...

  10. 钉钉OA自定义审批流的创建和使用

    前言 大家好!我是sum墨,一个一线的底层码农,平时喜欢研究和思考一些技术相关的问题并整理成文,限于本人水平,如果文章和代码有表述不当之处,还请不吝赐教. 钉钉作为一款办公软件,审批功能是它的核心功能 ...