c/c++语言开发共享BZOJ3329: Xorequ(二进制数位dp 矩阵快速幂)

题意 “题目链接” Sol 挺套路的一道题 首先把式子移一下项 $x oplus 2x = 3x$ 有一件显然的事情:$a oplus b leqslant c$ 又因为$a oplus b + 2(a & b) = c$ 那么$x & 2x = 0$ 也就是说,$x$的二进制表示下不能 …


题意

题目链接

sol

挺套路的一道题

首先把式子移一下项

(x oplus 2x = 3x)

有一件显然的事情:(a oplus b leqslant c)

又因为(a oplus b + 2(a & b) = c)

那么(x & 2x = 0)

也就是说,(x)的二进制表示下不能有相邻位

第一问直接数位dp即可

第二问比较interesting,设(f[i])表示二进制为(i)的方案数,转移时考虑上一位选不选

如果能选,方案数为(f[i – 2])

不选的方案数为(f[i – 1])

#include<bits/stdc++.h> #define ll long long  //#define int long long  #define file {freopen("a.in", "r", stdin); freopen("a.out", "w", stdout);} using namespace std; const int maxn = 233, mod = 1e9 + 7; inline ll read() {     char c = getchar(); ll x = 0, f = 1;     while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}     while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();     return x * f; } ll n; struct matrix {     int m[3][3];     matrix() {         memset(m, 0, sizeof(m));     }     matrix operator * (const matrix &rhs) const {         matrix ans;         for(int k = 1; k <= 2; k++)             for(int i = 1; i <= 2; i++)                 for(int j = 1; j <= 2; j++)                     (ans.m[i][j] += 1ll * m[i][k] * rhs.m[k][j] % mod) %= mod;         return ans;     } }; matrix matrixpow(matrix a, ll p) {     matrix base;     for(int i = 1; i <= 2; i++) base.m[i][i] = 1;     while(p) {         if(p & 1) base = base * a;         a = a * a; p >>= 1;     }     return base; } ll num[maxn], tot; ll f[maxn][2]; ll dfs(int x, bool lim, bool pre) {     if(!lim && (~f[x][pre])) return f[x][pre];     if(x == 0) return 1;     ll ans = 0;     if(!pre && (num[x] == 1 || (!lim))) ans += dfs(x - 1, lim, 1);     ans += dfs(x - 1, lim && num[x] == 0, 0);      if(!lim) f[x][pre] = ans;     return ans; } ll dp(ll x) {     tot = 0;     while(x) num[++tot] = x & 1, x >>= 1;     return dfs(tot, 1, 0); }    main() { //  file;     memset(f, -1, sizeof(f));     int t = read();     while(t--) {         n = read();         printf("%lldn", dp(n) - 1);         matrix a;         a.m[1][1] = 1; a.m[1][2] = 1;         a.m[2][1] = 1; a.m[2][2] = 0;         a = matrixpow(a, n);         printf("%dn", (a.m[1][1] + a.m[1][2]) % mod);           }      return 0; } /* 1 5 */ 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/c-cdevelopment/607326.html

(0)
上一篇 2021年5月14日
下一篇 2021年5月14日

精彩推荐