Xorequ(BZOJ3329+数位DP+斐波那契数列)

题目链接

传送门

思路

(aigoplus b=c ightarrow a=cigoplus b)得原式可化为(xigoplus 2x=3x)

又异或是不进位加法,且(2x=1<<x,3x=(1<<x)+x),因此可知((x&2x)=0),也就是说(x)的二进制中没有相邻的(1)

第一问就可以用数位(DP)来写。

对于第二问我们可以考虑递推式,我们定义(f(x))表示(2^x)时满足等式的数的个数,则

  • 如果第(n)位是(1),那么第(n-1)位就必须是(0),此时就相当于忽略第(n,n-1)位,转变成最高位是(n-2)的个数,因此(f(n))可以从第(n-2)位转移过来;
  • 如果第(n)位是(0),那么就相当于忽略第(n)位,转变成最高位是(n-1)的个数,因此(f(n))可以从第(n-1)位转移过来。

最后得到递推式(f(n)=f(n-1)+f(n-2)),也就是斐波那契数列,注意该递推式中的(n)是指(2)进制中最高位是多少,也就是题目中的(n-1),因次本题答案是(f(n+1))

其实这两个规律可以通过打表找出来的~

代码

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********
")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)

const double eps = 1e-8;
const int mod = 1000000007;
const int maxn = 50000 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

int t;
LL n;
int a[62];
int f[3], base[3][3];
LL dp[62][2][2][2];

LL dfs(int pos, int pre, int flag, bool limits, bool lead) {
    if(pos == -1) return flag && (!lead);
    if(!limits && dp[pos][pre][flag][lead] != -1) return dp[pos][pre][flag][lead];
    int up = limits ? a[pos] : 1;
    LL ans = 0;
    for(int i = 0; i <= up; ++i) {
        if(i == 0) ans += dfs(pos - 1, 0, flag, limits && i == a[pos], lead);
        else ans += dfs(pos - 1, 1, flag && (pre != 1), limits && a[pos] == i, 0);
    }
    if(!limits) dp[pos][pre][flag][lead] = ans;
    return ans;
}

LL solve(LL x) {
    int len = 0;
    while(x) {
        a[len++] = x % 2;
        x >>= 1;
    }
    return dfs(len - 1, 0, 1, 1, 1);
}

void mul() {
    int c[3];
    memset(c, 0, sizeof(c));
    for(int i = 0; i < 2; ++i) {
        for(int j = 0; j < 2; ++j) {
            c[i] = (c[i] + 1LL * f[j] * base[j][i] % mod) % mod;
        }
    }
    memcpy(f, c, sizeof(c));
}

void mulself() {
    int c[3][3];
    memset(c, 0, sizeof(c));
    for(int i = 0; i < 2; ++i) {
        for(int j = 0; j < 2; ++j) {
            for(int k = 0; k < 2; ++k) {
                c[i][j] = (c[i][j] + 1LL * base[i][k] * base[k][j] % mod) % mod;
            }
        }
    }
    memcpy(base, c, sizeof(c));
}

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif
    memset(dp, -1, sizeof(dp));
    scanf("%d", &t);
    while(t--) {
        scanf("%lld", &n);
        printf("%lld
", solve(n));
        base[0][0] = 1, base[0][1] = 1;
        base[1][0] = 1, base[1][1] = 0;
        f[0] = f[1] = 1;
        while(n) {
            if(n & 1) mul();
            mulself();
            n >>= 1;
        }
        printf("%d
", f[0]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/Dillonh/p/11327901.html