hiho1259 数位

这题uva上不能交,题意最后综合一下就是给你一个序列,f[i]=f[i-1]*3,f[i+1]=f[i-1]*3+1,最后综合过来就是数位dp计算将f[i]%mod以后的每种值的数量然后异或起来,公式比较难推,推出来以后就是裸的计算取模的dp

#include<bits/stdc++.h>
using namespace std;
using LL = int64_t;
LL mod,now;
LL dp[5][70][65550],cnt[70],fac[70];
LL dfs(int len,LL pos,bool flag) {
    if(len<0) return pos==0;
    if(flag==false&&dp[now][len][pos]!=-1) return dp[now][len][pos];
    int ends=flag?cnt[len]:1;
    LL ans=0;
    for(int i=0;i<=ends;i++) {
        ans+=dfs(len-1,(pos-i*fac[len]+mod)%mod,flag&&i==ends);
    }
    if(flag==false) dp[now][len][pos]=ans;
    return ans;
}

LL solve(LL n) {
    fac[0]=1;
    for(int i=1;i<70;i++) fac[i]=fac[i-1]*3%mod;
    int len=0;
    while(n) {
        cnt[len++]=n%2;
        n/=2;
    }
    LL ans=0;
    for(int i=0;i<mod;i++) ans^=dfs(len-1,i,true)-(i==0);
    return ans;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    LL n;
    memset(dp,-1,sizeof(dp));
    int T;cin>>T;
    while(T--) {
        cin>>n>>mod;
        if(mod==3) now=0;
        else if(mod==5) now=1;
        else if(mod==17) now=2;
        else if(mod==257) now=3;
        else if(mod==65537) now=4;
        cout<<solve(n)<<"\n";
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值