[ABC283Ex] Popcount Sum
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的更多相关文章
- 直观比较 popcount 的效率差异
问题 求 \(\sum\limits_{i=1}^{3\times 10^8} popcount(i)\) . 仅考虑在暴力做法下的效率. 枚举位 __builtin_popcount #includ ...
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
随机推荐
- Spring源码学习笔记13——总结篇, 从IOC到AOP
系列文章目录和关于我 零丶序言 在<Spring源码学习笔记12--总结篇,IOC,Bean的生命周期,三大扩展点>中,我们总结了Spring IOC部分的知识,为了更好的给群里的伙伴们分 ...
- 【路由器】Breed 介绍、刷入和使用
目录 介绍 文件说明 刷入 Breed 通过 Breed 刷机 其他功能 参考资料 介绍 Breed 是国内个人 hackpascal 开发的闭源 Bootloader,也被称为"不死鸟&q ...
- Redis系列20:LFU内存淘汰算法分析
Redis系列1:深刻理解高性能Redis的本质 Redis系列2:数据持久化提高可用性 Redis系列3:高可用之主从架构 Redis系列4:高可用之Sentinel(哨兵模式) Redis系列5: ...
- OGG-Postgres实时同步到Kafka
(一)数据同步信息 名称 源端 名称 目标端 数据库类型 Postgresql 12.4 组件类型 Kafka IP地址 20.2.127.23 Broker地址 20.2.125.52:9092, ...
- elmentui表单重置初始值问题与解决方法
背景 在做管理台项目时,我们会经常使用到表单+表格+弹窗表单的组合,以完成对数据的增.删.查.改. 在vue2+elementui项目中,使用弹窗dialog+表单form,实现对数据的添加和修改. ...
- 使用Vue3+elementPlus的Tree组件实现一个拖拽文件夹管理
目录 1.前言 2.分析 3. 实现 4.踩坑 4.1.拖拽辅助线的坑 4.2.数据的坑 4.3.限制拖拽 4.4.样式调整 1.前言 最近在做一个文件夹管理的功能,要实现一个树状的文件夹面板.里面包 ...
- 下载、安装CAN-EYE植被参数工具
本文介绍植被指数计算软件CAN-EYE的下载.安装方法. CAN-EYE软件是由法国国家农业研究院(French National Institute of Agricultural Rese ...
- Unexpected keys "@@dva", "user" found in preloadedState argument passed to createStore. Expected to find one of the known reducer keys instead: "router", "loading". Unexpected keys will be ignored.
Please use `require("history").DOMUtils` instead of `require("history/DOMUtils") ...
- 数据重整:用Java实现精准Excel数据排序的实用策略
摘要:本文由葡萄城技术团队原创并首发.转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 前言 在数据处理或者数据分析的场景中,需要对已有的数据进行排序,在Ex ...
- Spring扩展接口(2):BeanDefinitionRegistryPostProcessor
在此系列文章中,我总结了Spring几乎所有的扩展接口,以及各个扩展点的使用场景.并整理出一个bean在spring中从被加载到最终初始化的所有可扩展点的顺序调用图.这样,我们也可以看到bean是如何 ...