hiho1259 数位

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yqdjl6/article/details/83587754

这题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;
}

猜你喜欢

转载自blog.csdn.net/yqdjl6/article/details/83587754
今日推荐