發現若是枚舉路徑兩邊的長度的話,若是根節點的值是\(x\),左邊走了\(l\),右邊走了\(r\)c++
確定答案會是\((2^{l + 1} + 2^{r + 1} - 3)x + t\),能夠發現\(t < (2^{l + 1} + 2^{r + 1} - 3)\),因而考慮計算對於\(t\),左邊走了\(l\),右邊走了深度\(r\),幾種走法使得總和爲\(t\)spa
容易發現右邊最小必定是走了\(2^{r} - 1\)因而能夠扣掉code
再發現咱們實際上是對於左邊和右邊串選擇長度爲\([1,l - 1]\)和\([1,r - 1]\)的11111111......get
可是這種\(i\)個1的形式不太好dp,因而就轉化成\(2^{i + 1} - 1\)的形式,每次強制選擇的個數,只要考慮\(2^{i+ 1}\)去dp就行了,\(t\)加上強制選的個數再進行數位dp就能夠了it
複雜度是\(d^{5}\)的,有點慢,可是能夠過,由於上界很遠。。。class
#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感受題面很是有趣)map