BZOJ 3329: Xorequ

这篇博客详细介绍了BZOJ 3329题目的解决方案。通过分析题目条件x^(3x)=2x,得出x^(2x)等价于x+2x,转化为数位DP问题解决第一问。对于第二问,发现是斐波那契数列,利用矩阵快速幂优化计算2^n之内满足条件的x的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

原题链接
有这样一类数,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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值