题目大意:
给定方程x xor 3x=2xx\ xor\ 3x=2xx xor 3x=2x。
询问当x∈[1,n]x\in[1,n]x∈[1,n]和x∈[1,2n]x\in[1,2^n]x∈[1,2n]时解的个数。
分析:
原方程等价于x xor 2x=3xx\ xor\ 2x=3xx xor 2x=3x,也就是这个数向左移动一位后所有的111都错开了,也就是满足任意两个111不能相邻。
第一问考虑数位dp,设f[i][0/1][0/1]f[i][0/1][0/1]f[i][0/1][0/1]表示做到第iii位,最高位是0/10/10/1,当前表示的数字比原数大(小)的方案数,转移显然。
第二问满足f[i]=f[i−1]+f[i−2]f[i]=f[i-1]+f[i-2]f[i]=f[i−1]+f[i−2],矩阵快速幂即可。
代码:
/**************************************************************
Problem: 3329
User: ypxrain
Language: C++
Result: Accepted
Time:40 ms
Memory:1300 kb
****************************************************************/
#include <iostream>
#include <cstdio>
#include <cmath>
#define LL long long
const LL mod=1e9+7;
const int maxn=307;
using namespace std;
int T;
LL n;
LL f[maxn][2][2];
struct rec{
LL a[3][3];
}A,B;
rec operator *(rec a,rec b)
{
rec c;
for (int i=1;i<=2;i++)
{
for (int j=1;j<=2;j++) c.a[i][j]=0;
}
for (int k=1;k<=2;k++)
{
for (int i=1;i<=2;i++)
{
for (int j=1;j<=2;j++)
{
c.a[i][j]=(c.a[i][j]+a.a[i][k]*b.a[k][j]%mod)%mod;
}
}
}
return c;
}
void power(LL k)
{
if (k==1)
{
A=B;
return;
}
power(k/2);
A=A*A;
if (k%2) A=A*B;
}
int main()
{
B.a[1][1]=0,B.a[1][2]=1,B.a[2][1]=1,B.a[2][2]=1;
scanf("%d",&T);
while (T--)
{
scanf("%lld",&n);
f[0][0][0]=1;
int j=1;
for (LL i=n;i>0;i>>=1,j++)
{
if (i&1)
{
f[j][0][0]=f[j-1][0][0]+f[j-1][1][0]+f[j-1][0][1]+f[j-1][1][1];
f[j][0][1]=0;
f[j][1][0]=f[j-1][0][0];
f[j][1][1]=f[j-1][0][1];
}
else
{
f[j][0][0]=f[j-1][0][0]+f[j-1][1][0];
f[j][0][1]=f[j-1][0][1]+f[j-1][1][1];
f[j][1][0]=0;
f[j][1][1]=f[j-1][0][0]+f[j-1][0][1];
}
}
j--;
printf("%lld\n",f[j][0][0]+f[j][1][0]-1);
power(n);
printf("%lld\n",(A.a[1][2]+A.a[2][2])%mod);
}
}