题目
原题链接
有这样一类数,x^(3x)=2x
求n之内有多少个x以及2^n之内有多少个x,第二问要mod
x可达1e18
分析
变形:
x^(2x)=3x
x^(2x)=x+2x
说明在这里^运算和+运算等价,有2x=x<<1,可以推出只有每位元素都和前一位元素不同时为1才满足这个条件。
第一问数位DP
第二问由当前选择0,则后一位随便选,当前选择1,则后两位随便选得出这是个斐波那契数列,矩阵优化即可。
代码
#include<cmath>
#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=65,mo=1e9+7;
int T;
LL f[maxn][2],n,ans;
int z[maxn],sz;
struct matrix
{
int n,m;
LL a[5][5];
matrix() {n=m=0;memset(a,0,sizeof(a));}
void init() {n=m=0;memset(a,0,sizeof(a));}
friend matrix operator *(matrix x,matrix y)
{
matrix o;
o.n=x.n;o.m=y.m;
for(int i=1;i<=x.n;i++)
for(int j=1;j<=x.m;j++)
for(int k=1;k<=y.m;k++)
o.a[i][k]=(o.a[i][k]+x.a[i][j]*y.a[j][k])%mo;
return o;
}
matrix qkpow(LL y)
{
matrix ret,tt=(*this);ret.n=tt.n;ret.m=tt.m;
for(int i=1;i<=ret.n;i++) ret.a[i][i]=1;
while(y)
{
if(y&1) ret=ret*tt;
tt=tt*tt;
y>>=1;
}
return ret;
}
}A,B;
LL dp(int pos,bool last,bool lim)
{
if(pos<1)return 1;
if(!lim && f[pos][last]!=-1)return f[pos][last];
LL ret=0;
ret+=dp(pos-1,0,lim && 0==z[pos]);
if(!last && (!lim || (lim && z[pos])))ret+=dp(pos-1,1,lim && 1==z[pos]);
if(!lim)return f[pos][last]=ret;
return ret;
}
LL calc(LL x)
{
sz=0;
do{
z[++sz]=x&1;
x>>=1;
}while(x);
return dp(sz,0,1)-1;
}
int main()
{
//freopen("in.txt","r",stdin);
memset(f,-1,sizeof(f));
scanf("%d",&T);
while(T--)
{
scanf("%lld",&n);
printf("%lld\n",calc(n));
A.init();B.init();A.n=A.m=B.n=2;B.m=1;
A.a[1][1]=A.a[1][2]=A.a[2][1]=1;A.a[2][2]=0;
B.a[1][1]=B.a[2][1]=1;
A=A.qkpow(n-1);B=A*B;
ans=(B.a[1][1]+B.a[2][1])%mo;
cout<<ans<<endl;
}
return 0;
}