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. Java中锁的简单使用体验

    锁是控制多个线程访问共享资源的一种同步机制. synchronized:可以将代码块或方法设置为同步. ReentrantLock:提供了比synchronized更广泛的锁操作函数. ReadWri ...

  2. c++算法之动态规划:01背包

    什么是动态规划? 动态规划算法(dynamic programing),是一种由递推为基础的比贪心更稳定的一种优化策略,为运筹学的一部分.就是通过以递推为基础的手段非暴力求出最值. 它的总体思想其实就 ...

  3. 《CTFshow-Web入门》02. Web 11~20

    @ 目录 web11 题解 原理 web12 题解 web13 题解 web14 题解 web15 题解 web16 题解 原理 web17 题解 web18 题解 原理 web19 题解 web20 ...

  4. Elasticsearch之索引简单应用

    本篇所有操作都在 Kibana 上执行 创建第一个索引 PUT product { // 索引设置 "settings": { // 分片数量 "number_of_sh ...

  5. Row Major

    Smiling & Weeping ----昨天, 别人在我身旁大声说出你的名字, 这对于我, 像从敞开的窗口扔进了一朵玫瑰花. 思路:不客气地说,这是一道令人费解的题目,要求构造一个字符串, ...

  6. C#结合OpenCVSharp4使用直方图算法比较图片相似度

    C#结合OpenCVSharp4使用直方图算法比较图片相似度 直方图有灰度直方图.颜色直方图,如果是灰度图像,那么就用灰度直方图,这里使用颜色直方图来计算两个图片的相似度. 这里只记录如何使用,至于算 ...

  7. 算法——AcWing算法提高课中代码和题解

    文章目录 第一章 动态规划 (完成情况:64/68) 数字三角形模型 最长上升子序列模型 背包模型 状态机模型 状态压缩DP 区间DP 树形DP 数位DP 单调队列优化DP 斜率优化DP 第二章 搜索 ...

  8. MySQL低配数据库被大量数据导入时KO

    在一个低配MySQL数据库(笔记本电脑虚机环境,虚机配置2CPU/3G内存),在3000万级别的大量数据LOAD DATA方式导入时,坚持一小时后,终于被KO了,甚至没写下任何有用的日志,只是在操作界 ...

  9. 【PHP反序列化】速览

    PHP反序列化 一.原理 序列化就是将对象转化成字符串,反序列化相反.数据的格式转换和对象的序列化有利于对象的保存 . 反序列化漏洞:就是php对数据进行反序列化时,没有进行过滤,导致用户可以控制反序 ...

  10. JS个人总结(1)

    1. html页面引入js文件优先使用引入外部js文件. 2. 如果在html页面里使用<script></script>,则把js内容放在html内容下面,也就是</b ...