[HDU5694] BD String [分治][递归][结论]

[ L i n k \frak{Link} ]


打表
题意明显有二分倾向,又好像可以直接出公式。到底要怎么做呢?
题目要求的是L~RB的个数。显然Si的话,确定了i一边,另一边也可以确定。
同时Si里面i的左边又是Si-1

那么实际上可以把L~R位这一串字符拆成多个完整的BD String
可以考虑像线段树那样分治下去,不断拆出来左、中、右区间?
推一下就有公式了。


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<ctime>
#include<cmath>
using namespace std;
long long pos[100];
long long pow (long long base, long long cnt) {
    long long ret = 1;
    while (cnt) {
        if (cnt & 1) {
            ret *= base;
        }
        cnt >>= 1;
        base *= base;
    }
    return ret;
}
long long calc(const long long &x) {
    
    if (!x) return 0;
    int s = upper_bound(pos + 1, pos + 63, x) - pos - 1;
    if (x <= pos[s] + 1) return pos[s - 1] + 1 + (x > pos[s]);
    return pos[s-1]+2+ (x-pos[s]-1- (pos[s-1]+1- calc(pos[s]-x+pos[s]+1) ) );
    
}
int main() {
    
    for (int i = 1; i <= 63; ++i) {
        pos[i] = (pos[i-1] << 1) + 1;
    }
    
    int T;
    scanf("%d", &T);
    
    while (T--) {
        
        long long L,R;
        
        scanf("%lld%lld", &L, &R);
        printf("%lld\n",calc(R)-calc(L-1));
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Estia_/article/details/83545194
BD