题解:P10890 【烂题杯 Round 1】可持久化糖果树

· · 题解

不多说了直接上代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

namespace IO{
    const int sz=1<<22;
    char a[sz+5],b[sz+5],*p1=a,*p2=a,*t=b,p[105];
    inline char gc(){
        return p1==p2?(p2=(p1=a)+fread(a,1,sz,stdin),p1==p2?EOF:*p1++):*p1++;
    }
    template<class T> void read(T& x){
        x=0; char c=gc();
        for(;c<'0'||c>'9';c=gc());
        for(;c>='0'&&c<='9';c=gc())
            x=x*10+(c-'0');
    }
    inline void flush(){fwrite(b,1,t-b,stdout),t=b; }
    inline void pc(char x){*t++=x; if(t-b==sz) flush(); }
    template<class T> void write(T x,char c='\n'){
        if(x<0) pc('-'), x=-x;
        if(x==0) pc('0'); int t=0;
        for(;x;x/=10) p[++t]=x%10+'0';
        for(;t;--t) pc(p[t]); pc(c);
    }
    struct F{~F(){flush();}}f;
}
using IO::read;
using IO::write;
const ll P = 1e9 + 9;
const ll N = 1e5 + 10;
const ll M = 6;
const ll K = 21;
ll qpow(ll x, ll y) {
    if(y == 0) return 1;
    if(y % 2 == 1) return x * qpow(x, y - 1) % P;
    ll tmp = qpow(x, y / 2);
    return tmp * tmp % P;
}
const ll G = 13;
const ll w1 = qpow(G, (P - 1) / 3);
const ll w2 = qpow(G, (P - 1) / 3 * 2);
const ll inv3 = qpow(3, P - 2);
ll tmp1[3], tmp2[3], pw3[K];
ll num1[K], num2[K];
ll n, m, k, q, X;
ll f[1000010];
ll a[N][M];
ll ver[1000010];
ll root;
struct base3 {
    int num[K];
    base3(int x = 0) {
        memset(num, 0, sizeof num);
        int len = 0;
        while(x) {
            num[++ len] = x % 3;
            x /= 3;
        }
    }
    friend base3 operator ^(const base3 &x, const base3 &y) {
        base3 z;
        for(int i = 1; i < K; i ++) {
            z.num[i] = (x.num[i] + y.num[i]) % 3;
        }
        return z;
    }
    int to_int() {
        int x = 0;
        for(ll i = 1; i < K; i ++) {
            x += pw3[i - 1] * num[i];
        }
        return x;
    }
} b[M];
ll Change(ll x, ll y, ll z) {
    ll len = 0;
    while(x) {
        num1[++ len] = x % 3;
        x /= 3;
    }
    for(ll i = 1; i <= len; i ++) {
        if(i == y) num1[i] = (num1[i] * z) % 3;
        x += pw3[i - 1] * num1[i];
        num1[i] = 0;
    }
    return x;
}
void dfs(ll num, base3 x, ll mu) {
    if(num > 2 * m) {
        (f[x.to_int()] += mu) %= P;
        return;
    }
    if(num <= m) {
        dfs(num + 1, x, (mu * w1) % P);
        dfs(num + 1, x ^ b[num], (P - mu) % P);
    } else {
        dfs(num + 1, x, (mu * w2) % P);
        dfs(num + 1, x ^ b[num - m], (P - mu) % P);
    }
}
void FWT(ll *a, ll type, ll len) {
    for (ll x = 3; x <= len; x *= 3) {
        ll k = x / 3;
        for (ll i = 0; i < len; i += x) {
            for (ll j = 0; j < k; j ++) {
                for (ll l = 0; l < 3; l++) tmp1[l] = a[i + j + l * k];
                if (type == 1) {
                    tmp2[0] = (tmp1[0] + tmp1[1] + tmp1[2]) % P;
                    tmp2[1] = (tmp1[0] + tmp1[1] * w1 + tmp1[2] * w2) % P;
                    tmp2[2] = (tmp1[0] + tmp1[1] * w2 + tmp1[2] * w1) % P;
                } else {
                    tmp2[0] = (tmp1[0] + tmp1[1] + tmp1[2]) % P;
                    tmp2[1] = (tmp1[0] + tmp1[1] * w2 + tmp1[2] * w1) % P;
                    tmp2[2] = (tmp1[0] + tmp1[1] * w1 + tmp1[2] * w2) % P;
                    for (ll l = 0; l < 3; l++) (tmp2[l] *= inv3) %= P;
                }
                for (ll l = 0; l < 3; l++) a[i + j + l * k] = tmp2[l];
            }
        }
    }
}
int main() {
    read(n), read(m), read(k), read(X);
    pw3[0] = 1;
    for(int i = 1; i <= k; i ++) {
        root = root * 3 + 1;
        pw3[i] = pw3[i - 1] * 3;
    }
    for(int i = 1; i <= n; i ++) {
        for(int j = 1; j <= m; j ++) {
            for(int x = 1; x <= k; x ++) {
                ll tmp = (X + X * i + (X ^ (j * x))) % 1000000000;
                tmp %= 3;
                a[i][j] += pw3[x - 1] * tmp;
            }
            b[j] = base3(a[i][j]);
        }
        dfs(1, base3(), 1);
    }
    ll inv = qpow(qpow(w1 - 1, m), P - 2) * qpow(qpow(w2 - 1, m), P - 2) % P;
    FWT(f, 1, pw3[k]);
    ver[0] = root;
    ll ans = f[ver[0]] * inv % P;
    read(q);
    for(int i = 1; i <= q; i ++) {
        int x = (X ^ i) % i, y = (X ^ i) % k + 1, z = (X + (X ^ i)) % 3;
        ver[i] = Change(ver[x], y, z);
        ans ^= (f[ver[i]] * inv % P);
    }
    write(ans);
}