# 思路

$$a\bigoplus b=c\rightarrow a=c\bigoplus b$$得原式可化为$$x\bigoplus 2x=3x$$

• 如果第$$n$$位是$$1$$，那么第$$n-1$$位就必须是$$0$$，此时就相当于忽略第$$n,n-1$$位，转变成最高位是$$n-2$$的个数，因此$$f(n)$$可以从第$$n-2$$位转移过来；
• 如果第$$n$$位是$$0$$，那么就相当于忽略第$$n$$位，转变成最高位是$$n-1$$的个数，因此$$f(n)$$可以从第$$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("*********\n")
#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);
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);
}
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\n", 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\n", f[0]);
}
return 0;
}

posted @ 2019-08-09 16:08  Dillonh  阅读(251)  评论(0编辑  收藏  举报