题解:CF2168C Intercepting Butterflies

· · 题解

神秘通信题,直接上结论,没啥好说的。

显然可以把原本传入的数当作一个值域为 [1,15] 的正整数集合。注意 2^{15} 要对应空集。

同理,第一次传出的集合也可以看作是一个 [0,2^{20}) 中的二进制数。显然,这个二进制数中途最多只会有一位改变。

考虑传出的二进制数中,前 15 位表示传入的集合,之后第 16 至 19 位表示这个集合中所有数的异或和,最后一位表示第 16 至 19 位 1 的个数的奇偶性。

那么,解码时:

代码实现还是比较容易的。 :::success[code]

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define ms(x) memset(x,0,sizeof(x))
#ifdef ONLINE_JUDGE
#define gc getchar_unlocked
#else
#define gc _getchar_nolock
#endif
const int N=1e6+10,mod=998244353;
void encode()
{
    int x;
    cin>>x;
    if(x==(1<<15))
    {
        cout<<0<<'\n'<<'\n';
        return;
    }
    int y=0;
    for(int i=1;i<=15;i++) if(x&(1<<(i-1))) y^=i;
    int z=__builtin_popcount(y)&1;
    int ans=x+(y<<15)+(z<<19);
    cout<<__builtin_popcount(ans)<<'\n';
    for(int i=0;i<=19;i++) if(ans&(1<<i)) cout<<i+1<<' ';
    cout<<'\n';
}
void decode()
{
    int n;
    cin>>n;
    int s=0;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        s+=(1<<(x-1));
    }
    int x=(s&0x7fff),y=((s>>15)&15),z=((s>>19)&1);
    if(z!=(__builtin_popcount(y)&1))
    {
        if(x==0) cout<<(1<<15)<<'\n';
        else cout<<x<<'\n';
        return;
    }
    s=0;
    for(int i=1;i<=15;i++) if(x&(1<<(i-1))) s^=i;
    s^=y;
    if(s) x^=(1<<(s-1));
    if(!x) cout<<(1<<15)<<'\n';
    else cout<<x<<'\n';
}
signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0),cout.tie(0);
    string op;
    int t;
    cin>>op>>t;
    if(op=="first") while(t--) encode();
    else while(t--) decode();
    return 0;
}

:::