标签:problem mat bit putc turn putchar static 线段树 注意
#include <bits/stdc++.h>
#define ll long long
#define getchar nc
using namespace std;
inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline ll read()
{
register ll x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
inline void write(register ll x)
{
if(!x)putchar('0');if(x<0)x=-x,putchar('-');
static int sta[20];register int tot=0;
while(x)sta[tot++]=x%10,x/=10;
while(tot)putchar(sta[--tot]+48);
}
inline int Max(register int x,register int y)
{
return x>y?x:y;
}
inline int dep(register ll n)
{
int res=0;
while(n)
++res,n>>=1;
return res;
}
map<ll,ll> mp[55][55];
inline ll dfs(register int x,register int y,register ll z)
{
if(x<y)
x^=y^=x^=y;
if(z<0||z>(2ll<<x)+(2ll<<y)-x-y-4)
return 0;
if(!x&&!y)
return !z;
if(mp[x][y].count(z))
return mp[x][y][z];
return mp[x][y][z]=dfs(x-1,y,z)+dfs(x-1,y,z-(1ll<<x)+1);
}
int T,d,c;
ll a,b,n,ans;
int main()
{
T=read();
while(T--)
{
n=0;
d=read(),a=read(),b=read(),c=read();
while(a!=b)
{
if(a>b)
n+=a,a>>=1;
else
n+=b,b>>=1;
}
n+=a;
if(c==1)
{
write(n),puts("");
continue;
}
ans=0;
if(dep(n)<=d)
++ans;
for(register int l=1;l<=d;++l)
{
ll k=(2ll<<l)-1;
if(k<=n&&dep(n/k)+l<=d)
ans+=dfs(l,0,n%k);
}
for(register int l=1;l<=d;++l)
for(register int r=1;r<=d;++r)
{
ll k=(2ll<<l)+(2ll<<r)-3,b=(1ll<<r)-1;
if(k+b<=n&&dep((n-b)/k)+Max(l,r)<=d)
ans+=dfs(l-1,r-1,(n-b)%k);
}
write(ans-1),puts("");
}
return 0;
}
【题解】Luogu P5342 [TJOI2019]甲苯先生的线段树
标签:problem mat bit putc turn putchar static 线段树 注意
原文地址:https://www.cnblogs.com/yzhang-rp-inf/p/10933615.html