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. MySQL——后码锁(Next-Key Block)

    众所周知,Mysql的事务隔离级别分为4个,分别是READ-UNCOMMITED,READ-COMMITED,REPEATABLE-READ,SERIALIZABLE,在常规数据库概论中,前三种事务隔 ...

  2. Socket.io入门

    Socket.io入门 根据官方文档socket.io使用必须客户端根服务端一致,socket.io不兼容webSocket或者其他模块,因为socket.io在连接时做了自定义处理, 所以不同的长连 ...

  3. 2023-09-16:用go语言,给你一个整数 n 和一个在范围 [0, n - 1] 以内的整数 p , 它们表示一个长度为 n 且下标从 0 开始的数组 arr , 数组中除了下标为 p 处是 1

    2023-09-16:用go语言,给你一个整数 n 和一个在范围 [0, n - 1] 以内的整数 p , 它们表示一个长度为 n 且下标从 0 开始的数组 arr , 数组中除了下标为 p 处是 1 ...

  4. App性能指标(安装、冷启动、卸载、平均内存/cpu/fps/net)测试记录

    [需求背景] 需要针对产品以及竞品做出横向对比,输出对应的比对测试报告,供产研进行产品性能优化依据 [测试方案] 对于主流的厂商和系统版本进行多维度的横向对比 厂商:华为系.小米系.蓝绿系.三星系.苹 ...

  5. 织梦dede邮箱发信配置教程

    环境要求 主机465端口是开启和放行的 php扩展openssl是开启的 php扩展sockets是开启的 1.QQ邮箱或者163邮箱.126邮箱,开启SMTP服务,拿到授权码,根据自己的来 QQ邮箱 ...

  6. 「ABC 218」解集

    E 倒流一下,然后把负权边置零后跑 MST 即可. #include<cstdio> #include<vector> #include<algorithm> us ...

  7. 6.2 Sunday搜索内存特征

    Sunday 算法是一种字符串搜索算法,由Daniel M.Sunday于1990年开发,该算法用于在较长的字符串中查找子字符串的位置.算法通过将要搜索的模式的字符与要搜索的字符串的字符进行比较,从模 ...

  8. 传统的MVC开发模式和前后端分离开发模式

    1. 引言 在当今互联网时代,移动应用和网页应用的发展极大地推动了前后端分离开发模式的兴起.传统的后端渲染方式已经不能满足用户对高性能和优质用户体验的需求,于是前后端分离逐渐成为了一种主流的开发模式. ...

  9. 一些常见小程序的UI设计分享

    外卖优惠券小程序的UI设计 电子商城系统UI分享 A B C

  10. mysqli操作

    1.使用mysqli_connect()函数,语法如下: mysqli 对象名=mysqli_connect(数据库服务名,用户名,密码,数据库名) 例:$conn=mysqli_connect('l ...