LOJ#3109. 「TJOI2019」甲苯先生的线段树
发现如果枚举路径两边的长度的话,如果根节点的值是\(x\),左边走了\(l\),右边走了\(r\)
肯定答案会是\((2^{l + 1} + 2^{r + 1} - 3)x + t\),可以发现\(t < (2^{l + 1} + 2^{r + 1} - 3)\),于是考虑计算对于\(t\),左边走了\(l\),右边走了深度\(r\),几种走法使得总和为\(t\)
容易发现右边最小一定是走了\(2^{r} - 1\)于是可以扣掉
再发现我们其实是对于左边和右边串选择长度为\([1,l - 1]\)和\([1,r - 1]\)的11111111......
但是这种\(i\)个1的形式不太好dp,于是就转化成\(2^{i + 1} - 1\)的形式,每次强制选择的个数,只要考虑\(2^{i+ 1}\)去dp就好了,\(t\)加上强制选的个数再进行数位dp就可以了
复杂度是\(d^{5}\)的,有点慢,但是可以过,因为上界很远。。。
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define MAXN 200005
#define ba 47
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
res = 0;T f = 1;char c = getchar();
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 +c - '0';
c = getchar();
}
res *= f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) {
out(x / 10);
}
putchar('0' + x % 10);
}
int d,c;int64 a,b,s;
int getpos(int64 x) {
int res = 0;
while(x) {
++res;
x >>= 1;
}
return res;
}
int64 GetSum(int64 a,int64 b) {
int ta = getpos(a),tb = getpos(b);
if(ta > tb) {swap(a,b);swap(ta,tb);}
int64 res = 0;
for(int i = 1 ; i <= tb - ta ; ++i) {res += b;b >>= 1;}
while(a != b) {
res += a;res += b;
a >>= 1;b >>= 1;
}
res += a;
return res;
}
map<int64,int64> zz;
int La,Lb;
int64 dp[55][105][2];
int64 Process(int64 s,int num) {
memset(dp,0,sizeof(dp));
dp[0][0][0] = 1;
if(s & 1) return 0;
for(int i = 1 ; i <= 50 ; ++i) {
int to = (s >> i) & 1;
for(int j = 0 ; j <= num ; ++j) {
for(int t = 0 ; t <= 1 ; ++t) {
int up = (i <= La) + (i <= Lb);
for(int h = 0 ; h <= up ; ++h) {
if(((t + h) & 1) == to) {
int64 f = 1;
if(h == 1 && up == 2) f = 2;
dp[i][j + h][(t + h - to) / 2] += f * dp[i - 1][j][t];
}
}
}
}
}
return dp[50][num][0];
}
int64 Calc() {
int64 res = 0;
for(int i = 0 ; i <= d ; ++i) {
for(int j = 0 ; j <= d ; ++j) {
int64 k = (1LL << i + 1) + (1LL << j + 1) - 3;
int64 tmp = s;
if(tmp < k) continue;
if(getpos(tmp / k) + max(i,j) > d) continue;
tmp = tmp % k;
if(tmp < (1LL << j) - 1) continue;
tmp -= (1LL << j) - 1;
if(tmp == 0) {++res;continue;}
La = i - 1,Lb = j - 1;
for(int h = 0 ; h <= max(i - 1,0) + max(j - 1,0) ; ++h) {
res += Process(tmp + h,h);
}
}
}
return res;
}
void Solve() {
read(d);read(a);read(b);read(c);
s = GetSum(a,b);
if(c == 1) {out(s);enter;}
else {out(Calc() - 1);enter;}
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
int T;
read(T);
for(int i = 1 ; i <= T ; ++i) {
Solve();
}
return 0;
}
(权游文化还行。。。最近刚开始追权游23333感觉题面非常有趣)