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> # ...
随机推荐
- C程序(2)
- w3 parse a url
最新链接:https://www.w3.org/TR/html53/ 2.6 URLs — HTML5 li, dd li { margin: 1em 0; } dt, dfn { font-wei ...
- 给 MSYS2 添加国内源
https://wiki.qt.io/MSYS2pacman -S base-devel git mercurial svn wget p7zip软件包 开发包 http://mirrors.ustc ...
- css3中animation属性animation-timing-function知识点以及其属性值steps()
在animation中最重要的其实就是时间函数(animation-timing-function)这个属性,他决定了你的动画将以什么样的速度执行,所以最关键的属性值也就是cubic-bezier(n ...
- MySQL插入SQL语句后在phpmyadmin中注释显示乱码
自己写一个建一个简单的数据表,中间加了个注释,但是用PHPmyadmin打开后发现注释不对. 就先查询了一下sql 语句 发现SQL 语句并没有问题,感觉像是显示编码的问题,就先用set names ...
- 经典的7种排序算法 原理C++实现
排序是编程过程中经常遇到的操作,它在很大程度上影响了程序的执行效率. 7种常见的排序算法大致可以分为两类:第一类是低级排序算法,有选择排序.冒泡排序.插入排序:第二类是高级排序算法,有堆排序.排序树. ...
- 【js】【ios】【safari】【兼容问题】【转发】JS IOS/iPhone的Safari不兼容Javascript中的Date()问题
引用地址:http://www.cnblogs.com/yiven/p/6053872.html 1 var date = new Date('2016-11-11 11:11:11'); 2 d ...
- python爬虫基础03-requests库
优雅到骨子里的Requests 本文地址:https://www.jianshu.com/p/678489e022c8 简介 上一篇文章介绍了Python的网络请求库urllib和urllib3的使用 ...
- shell-code-1
#!/bin/bash # online test tool: http://www.shucunwang.com/RunCode/shell/ name="pxy"#Attent ...
- int (*a)[10] 和 int *a[10] 的区别
int *a[10] :指针数组.数组a里存放的是10个int型指针 int (*a)[10] :数组指针.a是指针,指向一个数组.此数组有10个int型元素 int *a[10] 先找到声明符a,然 ...