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> # ...
随机推荐
- HITICS || 2018大作业 程序人生 Hello's P2P
摘 要 本文通过分析一个hello.c的完整的生命周期,从它开始被编译,到被汇编.链接.在进程中运行,讲解了Linux计算机系统执行一个程序的完整过程. 关键词:操作系统,进程,程序的生命周期 目 ...
- _IO_FILE
hctf2017的babyprintf解法是house of orange,深入学习了一下,牵扯出许多知识,这里先进行第一步:_IO_FILE结构 0x00 _IO_FILE glibc-2.2.1\ ...
- vue父组件获取子组件页面的数组 以城市三级联动为例
父组件调用子组件 <Cselect ref="registerAddress"></Cselect> import Cselect from '../../ ...
- jenkins学习笔记(一)
windows下安装jenkins步骤 1.下载 官网路径:https://jenkins.io/ 2.安装 直接双击安装程序即可 centos7下安装命令: wget -O /etc/yum.rep ...
- 【php】类型转换
$a = 9; print_r((array) $a) ; 输出: [0=>9] print_r((array) null); 输出: []
- Many-to-one
创建模型 from django.db import models class Reporter(models.Model): first_name = models.CharField(max_le ...
- 基础训练 Sine之舞
Sine之舞 #include<iostream> #include<vector> #include<string.h> using namespace std; ...
- cf 1029 C
C. Maximal Intersection time limit per test 3 seconds memory limit per test 256 megabytes input stan ...
- Java中TreeMap集合讲解
1.TreeSet介绍 TreeSet是一个有序集合,可以以任意顺序将元素插入到集合中,在对集合进行遍历的时候,每个元素将自动按照排序后的顺序呈现.底层使用的是二叉树(更具体点是红黑树)实现,对于元素 ...
- ZOJ 3469 区间DP Food Delivery
题解 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm ...