題意
題目鏈接
有一個商店,商店裏每天會增加一個石子集合,集合裏有兩堆石子數相同的石子,且每個集合有一個價格。Alice每天會在商店裏選價格和儘量大的一些集合,滿足把這些集合裏的石子堆放在一起,然後Bob任意拿掉一些石子堆,無論Bob怎麼拿,玩Nim-K遊戲先手總有必勝策略。
,強制在線
分析
Nim-K的結論就是,如果二進制的每一位都滿足石子數這一位的個數是的倍數,則先手必敗,否則先手必勝。
那麼很顯然要維護的就是模意義下的最大權線性無關組,具體做法就是從枚舉新加入的數的每一位非零位,如果線性基表示這一位的位置爲空,則直接放上去,否則取兩者之中權值較小的繼續往下做。
表算還要用bitset優化三進制加法,儘管如此仍然跑的極慢,反正我用了一晚上卡常都沒卡過去。
代碼
#include<bits/stdc++.h>
typedef unsigned long long LL;
const int N=65;
int n,stack[N];
LL ans,val[N],bin[N];
struct bi
{
LL b1,b2;
void reset() {b1=b2=0;}
void set(int x) {if (x<60) b1|=bin[x]; else b2|=bin[x-60];}
int get(int x) {return x<30?(b1>>(x*2))&3:(b2>>((x-30)*2))&3;}
bi operator & (const bi &a) const {return (bi){b1&a.b1,b2&a.b2};}
bi operator | (const bi &a) const {return (bi){b1|a.b1,b2|a.b2};}
bi operator ^ (const bi &a) const {return (bi){b1^a.b1,b2^a.b2};}
bi operator << (const int &x) const {return (bi){b1<<x,(b2<<x)&(bin[60]-1)};}
bi operator >> (const int &x) const {return (bi){b1>>x,b2>>x};}
}a,vec[N],c1,c2;
LL read()
{
LL x=0;char ch=getchar();
while (ch<'0'||ch>'9'){ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x;
}
void pri(LL x)
{
int top=0;
if (!x) {puts("0");return;}
while (x) stack[++top]=x%10,x/=10;
while (top) putchar(stack[top--]+'0');
puts("");
}
void add(LL x,LL y)
{
a.reset();
int top=0;
for (int i=0;i<60&&x;i++)
{
if (x&1) a.set(i*2);
x>>=1;
}
for (int i=59;i>=0;i--)
if (a.get(i))
{
if (!val[i])
{
vec[i]=a;ans+=(val[i]=y);
break;
}
if (val[i]<y)
{
ans+=y-val[i];std::swap(val[i],y);
std::swap(a,vec[i]);
}
bi b=vec[i];
if (a.get(i)==b.get(i)) b=((b&c1)<<1)^((b&c2)>>1);
a=a^b^((a&b&c2)>>1)^((a&b&c1)<<1);
a=a^((((a>>1)&c1)^((a<<1)&c2))&a);
}
}
int main()
{
bin[0]=1;
for (int i=1;i<=60;i++) bin[i]=bin[i-1]*2;
for (int i=0;i<60;i++) c1.set(i*2),c2.set(i*2+1);
scanf("%d",&n);
while (n--)
{
LL x=read(),y=read();
x^=ans;y^=ans;
add(x,y);
pri(ans);
}
return 0;
}