Binary Indexed Tree HDU-5921 (贡献,计数,数位DP)
Binary Indexed Tree HDU-5921 (贡献,计数)
题目链接:
题意:
给定你一个整数\(n\in[1,10^{18}]\),问你\(ans=\sum_{i=1}^{n}\sum_{j=1}^{i-1}cost(j,i)\)
其中\(cost(j,i)\)是 树状数组区间修改时\(add(l-1,-val),add(r,val)\)。实际被更改的位置个数。
思路:
通过分析可以发现\(cost(j,i)\)等于\(g(i)+g(j-1)-2\times g(lcp(i,j-1))\)
其中:
\(g(x)\)为\(\mathit x\) 在二进制表示法中\(\text 1\)的个数。
\(lcp(i,j)\)是\(i,j\)在二进制表示法中补上前导零使其等长度后的最长公共前缀。
那么答案
\[ans=\frac{\sum_{i=0}^{n}\sum_{j=0}^{n}(g(i)+g(j)-2\times g(lcp(i,j)))}{2}
\\
=\frac{(n+1)\sum_{i=0}^{n}(2\times g(i))-2\times \sum_{i=0}^{n}\sum_{j=0}^{n}g(lcp(i,j))}{2}
\\
=(n+1)\sum_{i=0}^{n}\times g(i)-\sum_{i=0}^{n}\sum_{j=0}^{n}g(lcp(i,j))
\]
然后来思考如何计算\(g(x),g(lcp(i,j))\),
我们需要两个额外的数组来辅助计算:
\(r[i]\)代表将n二进制拆位后第\(\mathit i\)为右边的数位组成的数最大值。
\(l[i]\)代表将n二进制拆位后第\(\mathit i\)为左边的数位组成的数最大值。
计算\(g(x)\)分为两类贡献:
- 到第\(\mathit i\)位,左边的数位不改变,那么为了不大于原数,右边的数应取\([0,r[i-1]]\)。
即当\(w_i=1\)时,有\(r[i-1]+1\)的贡献,当\(w_i=0\)时,没有贡献。
- 到第\(\mathit i\)位,左边的数位改变了,左边的数应取\([0,l[i+1]-1]\).那么此时右边的数可以任意取值,有\(2^i\)种取值。因为高位已经小于上限了,该位置一定可以取1,所以有\(l[i+1]*2^i\)的贡献。
两两lcp的部分,则与之类似。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int len;
ll w[65];
ll base[65], l[65], r[65];
const ll mod = 1e9 + 7ll;
ll getf()
{
ll res = 0ll;
for (int i = len; i >= 0; --i) {
if (w[i]) {
res++;
res %= mod;
if (i > 0) {
res += r[i - 1];
res %= mod;
}
}
res += l[i + 1] * base[i] % mod;
res %= mod;
}
return res;
}
ll getlcp()
{
ll res = 0ll;
for (int i = len; i >= 0; --i) {
if (w[i]) {
if (i > 0) {
res += (r[i - 1] + 1) * (r[i - 1] + 1) % mod;
res %= mod;
} else {
res += 1ll;
res %= mod;
}
}
res += l[i + 1] * base[i] % mod * base[i] % mod;
res %= mod;
}
return res;
}
int main()
{
#if DEBUG_Switch
freopen("D:\\code\\input.txt", "r", stdin);
#endif
//freopen("D:\\code\\output.txt","w",stdout);
int t;
t = readint();
int icase = 1;
base[0] = 1ll;
repd(i, 1, 64) {
base[i] = base[i - 1] * 2ll % mod;
}
while (t--) {
ll n = readll();
ll temp = n % mod;
len = 0;
while (n) {
w[len++] = n & 1;
n >>= 1;
}
--len;
l[len + 1] = 0ll;
r[0] = w[0];
for (int i = 1; i <= len; ++i) {
r[i] = (r[i - 1] + w[i] * (1ll << i) % mod) % mod;
}
for (int i = len; i >= 0; --i) {
l[i] = ((l[i + 1] << 1) % mod + w[i]) % mod;
}
ll ans = getf() * (temp + 1) % mod;
ans = (ans - getlcp() + mod) % mod;
printf("Case #%d: %lld\n", icase++, ans);
}
return 0;
}
本博客为本人原创,如需转载,请必须声明博客的源地址。
本人博客地址为:www.cnblogs.com/qieqiemin/
希望所写的文章对您有帮助。