题解:P8766 [蓝桥杯 2021 国 AB] 异或三角

· · 题解

题目传送门

一道有难度的数位 DP,感觉比一些紫题还难。

思路

Code

#include<bits/stdc++.h>
#define int long long
#define f(i,j,k) for(int i=j;i<=k;i++)
#define F(i,j,k) for(int i=j;i>=k;i--)
using namespace std;
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
inline void write(int x){
    if(x<0) {x=~(x-1); putchar('-');}
    if(x>9) write(x/10);
    putchar(x%10+'0');
}
int T=read(),dp[2][40][8],loc[40],cnt;
inline int dfs(int pos,int vis,int st){
    if(!pos) return st==7;
    if(dp[vis][pos][st]!=-1) return dp[vis][pos][st];
    int nex=vis?loc[pos]:1,res=0;
    res+=dfs(pos-1,vis&&nex==0,st);
    if((st&1)&&(st>>1&1)) res+=dfs(pos-1,vis&&nex==0,st|4);
    if(nex==1) res+=dfs(pos-1,vis,st|1)+dfs(pos-1,vis,st|2);
    return dp[vis][pos][st]=res;
}
inline void get(){
    int x=read(); cnt=0; memset(dp,-1,sizeof(dp));
    while(x) loc[++cnt]=x&1,x>>=1;
    write(dfs(cnt,1,0)*3); puts("");
}
inline void work(){while(T--) get();}
signed main(){work();return !!!!!("YZren");}