P5539 【XR-3】Unknown Mother-Goose
P5539 【XR-3】Unknown Mother-Goose
手写bitset。
看了题解好久还是不是很懂,可能我太菜了吧。
要用分块的思想,当
然而当
因为手写bitset相当于把64个数压在一起。将一个状压后的数
设要更新的数为
就是说当高位看
对应到高位就是有
代码就是这样:
memset(tmp,0,sizeof(tmp));
for(int i=0;i<(l<<6);i+=l) //预处理64l个数
tmp[i>>6] |= 1llu<<(i&63);
for(int i=0;i<=m;i+=l) //周期是$2^{64l}$
for(int j=0;j<l;j++) //在周期内,对应的余数和对应的低位
bs[i+j]|=tmp[j];
还要注意边界的细节。
如
还要计算相邻
#include<bits/stdc++.h>
using namespace std;
const int N = 1e9+3;
typedef unsigned long long ull;
ull bs[(N>>6)+10],tmp[65];
int n,m,s,l,ans;
int cnt(ull x)
{
int ret = 0;
while(x)
{
ret += x&1;
x>>=1;
}
return ret;
}
int main()
{
scanf("%d%d",&n,&s);
m = (n>>6);
while(s--)
{
scanf("%d",&l);
if(l<64)
{
memset(tmp,0,sizeof(tmp));
for(int i=0;i<(l<<6);i+=l)
tmp[i>>6] |= 1llu<<(i&63);
for(int i=0;i<=m;i+=l)
for(int j=0;j<l;j++)
bs[i+j]|=tmp[j];
}
else
for(int i=0;i<=n;i+=l)
bs[i>>6] |= 1llu <<(i&63);
}
if((n&63)!=63) bs[m]&=(1llu<<(n+1-(m<<6)))-1;
bs[0] &= -2llu;
for(int i=0;i<=m;i++)ans += cnt(bs[i]&(bs[i]<<1)&(bs[i]<<2));
for(int i=1;i<=m;i++)ans += cnt(bs[i]&(bs[i-1]>>62)&((bs[i-1]>>63)|(bs[i]<<1)));
cout << ans;
return 0;
}