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> # ...
随机推荐
- Catalan 数
概要 在一些面试的智力题中会遇到此数的变形,如果完全不了解,直接想结果是很困难的,故在此简单介绍一下. 基本定义 Catalan 数的定义根据不同的应用环境有很多不同的定义方式,下面给出一个. ...
- 如何在Mac OS X中开启或关闭显示隐藏文件命令
打开终端,输入:defaults write com.apple.finder AppleShowAllFiles -bool true 此命令显示隐藏文件defaults write com.app ...
- css--背景和列表
背景 背景样式: background-color 设置元素的背景颜色 background-image 把图像设置为背景 background-repeat 设置背景图像是否重复及如 ...
- POI写入word docx 07 的两种方法
下载最新jar包:http://poi.apache.org/download.html 以及API 1.写入word 1.1 直接通过XWPFDocument生成 在使用XWPFDocument写d ...
- js常用技巧汇总
将彻底屏蔽鼠标右键 oncontextmenu="window.event.returnvalue=false" <table border oncontextmenu=re ...
- ios之键盘的自定义
一.键盘通知 当文本View(如UITextField,UITextView,UIWebView内的输入框)进入编辑模式成为first responder时,系统会自动显示键盘.成为firstresp ...
- Comet OJ 热身赛-principal
这题的话,我们分析一下,入栈的操作是: 栈空 栈顶元素和当前操作元素不属于同一类括号 栈顶元素和当前操作元素属于同一类括号,但是并不是左括号在前,右括号在后 上面三个条件有任意一个满足都应该入栈,如果 ...
- 【cpu】CPU版本认识
此图应该是摘选自<鸟哥的linux私房菜>
- Dajngo admin
Dajngo admin admin app Django 提供了基于 web 的管理工具. Django 自动管理工具是 django.contrib 的一部分.你可以在项目的 settings.p ...
- Codeforces Round #877 (Div. 2) B. - Nikita and string
题目链接:http://codeforces.com/contest/877/problem/B Nikita and string time limit per test2 seconds memo ...