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> # ...
随机推荐
- 利用python进行数据分析3_Pandas的数据结构
Series #通过list构建Series ser_obj=pd.Series(range(10,20)) print(type(ser_obj))#<class 'pandas.core.s ...
- 参考别人的代码写的aes加密,记录一下(AES,ECB模式,填充PKCS5Padding,数据块128位,偏移量无,以hex16进制输出)
package org.jimmy.autosearch2019.test; import java.security.SecureRandom; import javax.crypto.Cipher ...
- Hibernate 多表查询 - Criteria添加子字段查询条件 - 出错问题解决
Criteria 查询条件如果是子对象中的非主键字段会报 could not resolve property private Criteria getCriteria(Favorite favori ...
- vue 封装分页组件
分页 一般都是调接口, 接口为这种格式 {code: 0, msg: "success",…} code:0 data:{ content:[{content: "11& ...
- HTML5 Canvas奇幻色彩Loading加载动画
转自 https://www.html5tricks.com/tag/loading%E5%8A%A8%E7%94%BB/
- windows 使用git上传代码至github
1. 首先创建github账户 2. 创建github项目 3. windows安装git工具 ·下载地址:https://git-for-windows.github.io/ ,下载直接安装即可, ...
- 目录扫描工具DirBuster
DirBuster是用来探测web服务器上的目录和隐藏文件的.因为DirBuster是采用java编写的,所以运行前要安装上java的环境. 来看一下基本的使用: ①:TargetURL下输入要探测网 ...
- 如何创作用纯 CSS 绘制一支栩栩如生的铅笔
效果预览 在线演示 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/PaZYBw 可交互视频教 ...
- simulation clock gen unit (推荐)
//Normal Clock Block always begin:clk_blk clk <=; # clk<=; #; end //Improved Clock Block, impr ...
- Exchange 2010 打补丁的顺序
升级的顺序: 客户端访问服务器 ——> 集线器传输服务器 ——>统一消息服务器(如果有的话)——> 邮箱服务器 ——> 边缘服务器.