Description
Input
第一行一个正整数,表示数据组数据 ,接下来T行 每行一个正整数N
Output
2T行 第2i-1行表示第i个数据中问题一的解,
第2*i行表示第i个数据中问题二的解,
Sample Input
1
1
Sample Output
1
2
HINT
x=1与x=2都是原方程的根,注意第一个问题的解
不要mod 10^9+7
1<=N<=10^18
1<=T<=1000
题解
可以转为 x x o r 2 x = 3 x xxor2x=3x xxor2x=3x
由于 x + 2 x = 3 x x+2x=3x x+2x=3x
异或是不进位的加法
所以 x a n d 2 x = 0 xand2x=0 xand2x=0
也就相当于x中没有相邻的两个1
大力数位DP
第二问的话…
打表发现是斐波那契的第n+2项 以1开头
大力矩乘
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define mod 1000000007
using namespace std;
inline LL read()
{
LL f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void write(int x)
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10+'0');
}
inline void print(int x){write(x);printf(" ");}
struct matrix
{
LL m[4][4];
matrix(){memset(m,0,sizeof(m));}
friend matrix operator *(matrix u,matrix v)
{
matrix ret;
for(int i=1;i<=3;i++)
for(int j=1;j<=3;j++)
for(int k=1;k<=3;k++)
(ret.m[i][k]+=u.m[i][j]*v.m[j][k])%=mod;
return ret;
}
}st,tmp;
matrix pow_mod(matrix u,LL b)
{
matrix ret;
for(int i=1;i<=3;i++)ret.m[i][i]=1;
while(b)
{
if(b&1)ret=ret*u;
u=u*u;b>>=1;
}
return ret;
}
LL n;
LL f[85][2][2],g[85][2];
int po[85],ln;
void get(LL p)
{
ln=0;
while(p)po[++ln]=p&1,p>>=1;
}
LL dp(int p,int op1,int op2)//位置 是否顶格子 上一位填的是什么
{
if(p<=0)return 1;
if(f[p][op1][op2]!=-1)return f[p][op1][op2];
if(!op1)
{
if(op1)return g[p][0];
return g[p][0]+g[p][1];
}
int lim=op1?po[p]:1;
LL ret=0;
for(int i=0;i<lim;i++)if(!(op2&i))ret+=dp(p-1,0,i);
if(!(op2&lim))ret+=dp(p-1,op1,lim);
return f[p][op1][op2]=ret;
}
int main()
{
int T;scanf("%d",&T);
g[1][0]=g[1][1]=1;
for(int i=2;i<=65;i++)
g[i][0]=g[i-1][0]+g[i-1][1],g[i][1]=g[i-1][0];
memset(f,-1,sizeof(f));
while(T--)
{
n=read();memset(f,-1,sizeof(f));
get(n);
printf("%lld\n",dp(ln,1,0)-1);
st.m[1][1]=2;st.m[1][2]=st.m[1][3]=1;
memset(tmp.m,0,sizeof(tmp.m));
tmp.m[1][1]=tmp.m[1][2]=tmp.m[2][1]=tmp.m[2][3]=1;
tmp=pow_mod(tmp,n-1);
st=st*tmp;
printf("%lld\n",st.m[1][1]);
}
return 0;
}