您的位置:首页 > 其它

[找规律 递归子问题 || 数位DP] BZOJ 4513 [Sdoi2016]储能表

2016-04-23 09:51 597 查看
看不懂数位DP,蒟蒻就是蒟蒻:http://fancypei.github.io/2016/04/16/SDOI2016%20Round1/

然后ZZY Google到了一个神奇的做法,很happy:https://blog.menci.moe/sdoi2016-table/

强力膜拜

考虑异或的性质:

性质一:对于任意 x<2Nx<2N,y<2Ny<2N,必有 (x+2N) xor (y+2N)=x xor y(x+2N) xor (y+2N)=x xor y

性质二:对于任意 x≠yx≠y,必有 x xor z≠y xor zx xor z≠y xor z

性质三:对于任意 x<2Nx<2N,[0,2N−1][0,2N−1] 的所有数与 xx 的异或所得结果取遍 [0,2N−1][0,2N−1] 的所有数。

考虑递归子问题







#include<cstdio>
#include<cstdlib>
#include<algorithm>
#define lowbit(x) ((x)&-(x))
using namespace std;
typedef long long ll;

inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}

inline void read(ll &x){
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

ll P;

inline ll log2(ll x) {
ll ret=0;
while (x>>=1) ret++;
return ret;
}

inline ll mul(ll x,ll y,ll z=1) {
// (x * y) / z, z is 1 or 2;
if (z==2) { if (x&1) y>>=1; else if (y&1) x>>=1; }
return (x%P)*(y%P)%P;
}

inline ll Calc(ll first, ll n,ll k,ll t) {
first-=k;
if (first<1) n-=(1-first),first=1;
if (n<=0) return 0;
return mul(mul(first+(first+n-1),n,2LL),t);
}

ll Solve(ll n,ll m,ll K)
{
if (n==0 || m==0) return 0;
if (K<0) K=0;
if (n<m) swap(n,m);
if (n==m && n==lowbit(n)) return Calc(1,n-1,K,m);
ll N=log2(n),M=log2(m);
ll W=(1LL<<N),H=(1LL<<M);
ll lW,rW,rH,lH,bH,bW,rS,bS,sS,cS,lS;
if (N==M)
{
rW=n-W,rH=H;
bW=W,bH=m-H;
rS=Calc(W,rH,K,rW);
bS=Calc(H,bW,K,bH);
sS=Solve(rW,bH,K);
cS=Solve(bW,rH,K);
return (rS+bS+sS+cS)%P;
}
else
{
lW=(1LL<<N),lH=m;
rW=n-lW,rH=lH;
lS=Calc(0,lW,K,lH);
rS=Solve(rW,rH,K-lW);
if (lW>K)
(rS+=mul(mul(lW-K,m),rW))%=P;
return (lS+rS)%P;
}
}

int main()
{
ll Q,n,m,k;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(Q);
while (Q--)
{
read(n); read(m); read(k); read(P);
printf("%lld\n",Solve(n,m,k));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: