题目大意:
因为异或是不进位的二进制加法,那么因为结果正好和加法相同,那么说明x在二进制上没有相邻的1。那么简单的数位DP就可以求出满足这个的答案了。
再看subtask2,根据打表找规律可得,这就是斐波那契数列的第n+2项(以首项是0来说)。那么只需要
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long unsigned
const LL MOD = 1e9+7;
LL dp[100][2], L, R, cnt;
int n, a[100];
LL DP(int i, int j, int f) {
if(!i) return 1;
if(!f && ~dp[i][j]) return dp[i][j];
LL ans = 0;
int ed = f ? a[i] : 1;
for(int k = 0; k <= ed; ++ k) if(!k||!j) ans += DP(i-1, k, f && k == ed);
if(!f) dp[i][j] = ans;
return ans;
}
LL solve(LL s, int len = 0) {
for(; s; s >>= 1) a[++ len] = s & 1;
return DP(len, 0, 1);
}
struct Mat { LL a[3][3]; } A, B;
Mat Mul(Mat A, Mat B) {
Mat C;
for(int i = 0; i < 2; ++ i)
for(int j = 0; j < 2; ++ j)
C.a[i][j] = 0;
for(int i = 0; i < 2; ++ i)
for(int j = 0; j < 2; ++ j)
for(int k = 0; k < 2; ++ k)
C.a[i][j] = (C.a[i][j] + A.a[i][k] * B.a[k][j]) % MOD;
return C;
}
Mat ksm(Mat A, LL k) {
Mat C;
for(int i = 0; i < 2; ++ i)
for(int j = 0; j < 2; ++ j)
C.a[i][j] = (i == j);
for(; k; k >>= 1) {
if(k & 1) C = Mul(C, A);
A = Mul(A, A);
}
return C;
}
int main() {
memset(dp, -1, sizeof dp);
int T; scanf("%d", &T);
while(T --) {
scanf("%llu", &R);
A.a[0][0] = A.a[0][1] = A.a[1][0] = 1;
A.a[1][1] = 0;
B.a[0][1] = 0; B.a[0][0] = 1;
A = ksm(A, R+1); A = Mul(A, B);
printf("%llu\n%llu\n", solve(R)-1, A.a[0][0]);
}
}