Hdu5921 Binary Indexed Tree
思路
计数问题,题目重点在于二进制下1的次数的统计,很多题解用了数位DP来辅助计算,定义g(i)表示i的二进制中1的个数, $ans = \sum_{i=1}^n \sum_{j=0}^{i-1} g(i,j) = 0.5\sum_{i=0}n\sum_{j=0}n[g(i)+g(j)-2g(lcp(i,j))] $
即先计算每个位的贡献,再减去重复的地方。
先计算前者,每个数会出现n+1 次,所以结果乘以n+1 即可,对第i位,统计这一位为1的数,考虑这一位的右边,如果当前数位为1,那么从这一位往后的后缀的数都满足,用r[i-1]表示,即从0~r[i-1]。这一位的左边的前缀的数也都满足,用l[i+1]表示,要乘\(2^i\) 。
同理,计算lcp的计数只需考虑两个数同时满足的情况即可。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 65;
typedef long long ll;
const ll mod = 1e9+7LL;
int a[N];
ll l[N],r[N],e[N];
ll get(int x){
ll cnt = 0LL;
for(int i=0;i<x;i++) cnt += a[i];
for(int i=x-1;i>=0;i--){
if(a[i]){
if(i) cnt += r[i-1];
}
{
cnt += l[i+1]*e[i]%mod;
cnt %= mod;
}
}
return cnt;
}
ll getlcp(int x){
ll cnt = 0LL;
for(int i=x-1;i>=0;i--){
if(a[i]){
if(i) (cnt += (r[i-1]+1)*(r[i-1]+1)%mod)%=mod;
else{
(cnt += 1LL)%=mod;
}
//printf("cnt %lld\n",cnt);
}
{
cnt += l[i+1]*e[i]%mod*e[i]%mod;
cnt %= mod;
//printf("cnt %lld\n",cnt);
}
}
//printf("cnt %lld\n",cnt);
return cnt;
}
int main(){
int i,T,len,t;
ll n,m,ans;
scanf("%d",&t);
T=0;
for(int i=0;i<63;i++) e[i]=(1LL<<i)%mod;
while(t--){
scanf("%lld",&n);
m=n;
len=0;
for(;m;m>>=1) a[len++]=m%2;
l[len]=0;
r[0]=a[0];
for(i=1;i<len;i++) r[i]=(r[i-1]+a[i]*(1LL<<i)%mod)%mod;
for(i=len-1;i>=0;i--) l[i] = ((l[i+1]<<1LL) + a[i])%mod;
ans = ((n+1)%mod*get(len))%mod;
//printf("%lld\n",ans);
ans -= getlcp(len);
ans = (ans%mod+mod)%mod;
printf("Case #%d: %lld\n",++T,ans);
}
}
Hdu5921 Binary Indexed Tree的更多相关文章
- Leetcode: Range Sum Query 2D - Mutable && Summary: Binary Indexed Tree
Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...
- SRM 627 D1L2GraphInversionsDFS查找指定长度的所有路径 Binary indexed tree (BIT)
题目:http://community.topcoder.com/stat?c=problem_statement&pm=13275&rd=16008 由于图中边数不多,选择DFS遍历 ...
- 树状数组(Binary Indexed Tree,BIT)
树状数组(Binary Indexed Tree) 前面几篇文章我们分享的都是关于区间求和问题的几种解决方案,同时也介绍了线段树这样的数据结构,我们从中可以体会到合理解决方案带来的便利,对于大部分区间 ...
- Binary Indexed Tree (Fenwick Tree)
Binary Indexed Tree 主要是为了存储数组前缀或或后缀和,以便计算任意一段的和.其优势在于可以常数时间处理更新(如果不需要更新直接用一个数组存储所有前缀/后缀和即可).空间复杂度O(n ...
- Binary Indexed Tree 总结
特点 1. 针对 数组连续子序列累加和 问题(需要进行频繁的 update.sum 操作): 2. 并非是树型结构,只是逻辑上层次分明: 3. 可以通过 填坑法 来理解: 4. 中心思想:每一个整数都 ...
- Binary Indexed Tree
我借鉴了这个视频中的讲解的填坑法,我认为非常易于理解.有FQ能力和基本英语听力能力请直接去看视频,并不需要继续阅读. naive 算法 考虑一个这样的场景: 给定一个int数组, 我们想知道它的连续子 ...
- 树状数组(Binary Indexed Tree)
树状数组(Binary Indexed Tree,BIT) 是能够完成下述操作的数据结构. 给一个初始值全为 0 的数列 a1, a2, ..., an (1)给定 i,计算 a1+a2+...+ai ...
- Fenwick Tree / Binary Indexed Tree
Motivation: Given a 1D array of n elements. [2, 5, -1, 3, 6] range sum query: what's the sum from 2n ...
- Binary Indexed Tree 2D 分类: ACM TYPE 2014-09-01 08:40 95人阅读 评论(0) 收藏
#include <cstdio> #include <cstdlib> #include <climits> #include <cstring> # ...
随机推荐
- cmake 指定输出目录
$ mkdir ~/cpp-netlib-build $ cd ~/cpp-netlib-build $ cmake -DCMAKE_BUILD_TYPE=Debug \ > -DCMAKE_C ...
- oracle调用子存储过程+游标循环实例
一,有子节点的部门的子节点的排序,调用子存储过程 CREATE OR REPLACE PROCEDURE "PRO_INIT_SORT" AS CURSOR cur_departm ...
- ios之UIScrollView
UIScrollView 类负责所有基于 UIKit 的滚动操作. 一.创建 [java] view plaincopy CGRect bounds = [ [ UIScreen mainSc ...
- RN原生方法setNativeProps
https://facebook.github.io/react-native/docs/direct-manipulation.html setNativeProps可以直接修改底层native组件 ...
- java 比较String StringBuffer StringBuilder
String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilder 字符串变量(非线程安全) 简要的说, String 类型和 StringBuffer 类型的主要性能 ...
- python基础学习笔记——开发规范
> 编码 1 2 3 4 5 所有的 Python 脚本文件都应在文件头标上 # -*- coding:utf-8 -*- 用于设置编辑器,默认保存为 utf-8 格式. > 注释 ...
- mysql无法启动,报错 Can't start server: can't create PID file: No space left on device
然后看mysql日志文件 出现Can't start server: can't create PID file: No space left on device 这个错误. 提示磁盘空间不足 后用d ...
- 学习iis工作原理
文章:IIs工作原理 文章:Asp.Net 构架(Http Handler 介绍) - Part.2
- LINQ-内部联接
一.简单键联接 下面的示例创建两个集合,其中包含两种用户定义类型 Person 和 Pet 的对象. 查询使用 C# 中的 join 子句将 Person 对象与 Owner 是该 Person 的 ...
- [Kubernetes]容器健康检查和恢复机制
在Kubernetes中,可以为Pod里的容器定义一个健康检查探针(Probe),这样Kubernetes会根据这个Probe的返回值决定这个容器的状态,而不是直接以容器是否允许(来自Docker返回 ...