首页 > 其他分享 >[ABC283Ex] Popcount Sum

[ABC283Ex] Popcount Sum

时间:2023-01-04 16:36:53浏览次数:51  
标签:int Sum popcount should leq Popcount test ABC283Ex LL

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);
	}
}

标签:int,Sum,popcount,should,leq,Popcount,test,ABC283Ex,LL
From: https://www.cnblogs.com/mekoszc/p/17025263.html

相关文章

  • [ABC283Ex] Min + Sum
    ProblemStatementYouaregiventwosequencesofintegersoflength$N$:$A=(A_1,A_2,\ldots,A_N)$and$B=(B_1,B_2,\ldots,B_N)$.Printthenumberofp......
  • java8中常用函数式接口Supplier<T>、Consumer<T>、Function<T,R>、Predicate<T>使用示
    场景函数式接口(FunctionalInterface)就是一个有且仅有一个抽象方法,但是可以有多个非抽象方法的接口。而java中的函数式编程体现就是Lambda,所以函数式接口就是可以适用......
  • Codeforces 1373 D. Maximum Sum on Even Positions 做题记录(单调队列)
    因为只能转一个子数组,很显然转长度为奇数的子数组,对最大化答案是没有意义的(偶数位的数字之和不会变化)。因此只考虑转偶长度的子数组。转动偶数长度的子数组,相当于......
  • UVa10827 Maximum sum on a torus
    思路一道比较经典的题目。首先我们可以把方阵扩展一下,变成\(4n^2\)大小的方阵。问题就变为在长度为\(2\timesn\)的方阵中求一个长和宽均小于等于\(n\)的子矩阵的......
  • CodeForces 1726E Tree Sum
    洛谷传送门CF传送门不错的一道Combinatorics。结论1:\(n\)为奇数时答案为\(0\)。设\(d_i\)为与点\(i\)相连的边边权乘积。每加入一条边对两端的\(d_i\)贡献......
  • P2398 GCD SUM——欧拉函数
    此题可以拓展为\(\sum\limits^n_{i=1}\sum\limits^m_{j=1}\gcd(i,j)\)结论是\(\sum\limits^{\min(n,m)}_{d=1}\varphi(d)\lfloor\dfrac{n}{d}\rfloor\lfloor\dfrac{m}{......
  • Sumitomo Mitsui Trust Bank Programming Contest 2019 —— B
    也不知道这比赛为啥要取这么长的名称(传送门:https://atcoder.jp/contests/sumitrust2019/tasks/sumitb2019_e哈哈,你被骗了!但网址是真的!题意有红绿蓝三种帽子RedandBl......
  • 模拟spring-kafka实现kafka的consumer监听
    背景:因为某些原因,无法直接使用springboot提供的@KafkaListener,改为模拟springboot注解的方式搬过来实现首先创建一个业务处理的service,这个service主要用于消费下来的消息......
  • cesaro sum和tauber型定理
    缓更Definition.1:Givenasequence\(\{a_n\}(n\ge1)\),thecesarosumofadenote$\lim_{n\rightarrow\infty}\frac{\sum_{i=1}^na_i}{n}$,ifthislimit......
  • 404. Sum of Left Leaves
    Giventherootofabinarytree,returnthesumofallleftleaves.Aleafisanodewithnochildren.Aleftleafisaleafthatistheleftchildofanother......