http://www.lydsy.com/JudgeOnline/problem.php?id=4513

bzoj千题计划277:bzoj4513: [Sdoi2016]储能表-LMLPHP

f[i][0/1][0/1][0/1] 从高到低第i位,是否卡n的上限,是否卡m的上限,是否卡k的下限 的方案数

g[i][0/1][0/1][0/1] 对应 f 的和

#include<cstdio>
#include<cstring>
#include<iostream> using namespace std; int f[][][][],g[][][][]; template<typename T>
void read(T &x)
{
x=; char c=getchar();
while(!isdigit(c)) c=getchar();
while(isdigit(c)) { x=x*+c-''; c=getchar(); }
} void solve()
{
long long n,m,k; int p;
read(n); read(m); read(k); read(p);
memset(f,,sizeof(f));
memset(g,,sizeof(g));
f[][][][]=;
int A,B,C,z;
int ni,mi,ki;
for(int i=;i>=;--i)
for(int a=;a<=;++a)
for(int b=;b<=;++b)
for(int c=;c<=;++c)
if(f[i+][a][b][c])
{
ni=n>>i&; mi=m>>i&; ki=k>>i&;
for(int x=;x<=;++x)
if(!a || x<=ni)
for(int y=;y<=;++y)
if(!b || y<=mi)
{
z=x^y;
if(c && z<ki) continue;
A=a&x==ni; B=b&&y==mi; C=c&&z==ki;
f[i][A][B][C]=(f[i][A][B][C]+f[i+][a][b][c])%p;
g[i][A][B][C]=(g[i][A][B][C]+g[i+][a][b][c])%p;
if(z) g[i][A][B][C]=(g[i][A][B][C]+(1LL<<i)%p*f[i+][a][b][c]%p)%p;
}
}
printf("%d\n",(g[][][][]-k%p*f[][][][]%p+p)%p);
} int main()
{
int T;
read(T);
while(T--) solve();
return ;
}
05-04 06:22