loj#6436. 「PKUSC2018」神仙的游戏(生成函数)

题意

链接

Sol

生成函数题都好神仙啊qwq

我们考虑枚举一个长度\(len\)。有一个结论是如果我们按\(N - len\)的余数分类,若同一组内的全为\(0\)或全为\(1\)(?不算),那么存在一个长度为\(len\)的border。

有了这个结论后我们考虑这样一种做法:把序列看成两个串\(a, b\),若\(a_i = 0, b_j = 1\),那么对于所有的\(k | (|i - j|)\), \(N-k\)都不会成为答案。

考虑怎么快速算不合法的\((i, j)\)。对于多项式乘法得到的多项式的第\(k\)项,实际上是由所有的\(a_i * a_j(i+j=k)\)相乘得到的。我们把序列\(b\)翻转一下,这时候得到的第\(k\)项实际上就是由\(a_i * a_{N - j}\)得到的。

然后枚举一个数看一下他的倍数是否\(>0\)就行了

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 8e6 + 10, INF = 1e9 + 1;
const double eps = 1e-9, pi = acos(-1);
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int N, M, a[MAXN], b[MAXN], vis[MAXN];
char s[MAXN];
namespace Poly {
    int rev[MAXN], GPow[MAXN], A[MAXN], B[MAXN], C[MAXN], lim, INV2;
    const int G = 3, mod = 1004535809, mod2 = 1004535808;
    template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
    template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
    template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
    template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
    int fp(int a, int p, int P = mod) {
        int base = 1;
        for(; p > 0; p >>= 1, a = 1ll * a * a % P) if(p & 1) base = 1ll * base *  a % P;
        return base;
    }
    int inv(int x) {
        return fp(x, mod - 2);
    }
    int GetLen(int x) {
        int lim = 1;
        while(lim <= x) lim <<= 1;
        return lim;
    }
    int GetOrigin(int x) {//¼ÆËãÔ­¸ù 
        static int q[MAXN]; int tot = 0, tp = x - 1;
        for(int i = 2; i * i <= tp; i++) if(!(tp % i)) {q[++tot] = i;while(!(tp % i)) tp /= i;}
        if(tp > 1) q[++tot] = tp;
        for(int i = 2, j; i <= x - 1; i++) {
            for(j = 1; j <= tot; j++) if(fp(i, (x - 1) / q[j], x) == 1) break;
            if(j == tot + 1) return i;
        }
        return -1;
    }
    void Init(/*int P,*/ int Lim) {
        INV2 = fp(2, mod - 2);
        for(int i = 1; i <= Lim; i++) GPow[i] = fp(G, (mod - 1) / i);
    }
    void NTT(int *A, int lim, int opt) {
        int len = 0; for(int N = 1; N < lim; N <<= 1) ++len; 
        for(int i = 1; i <= lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1));
        for(int i = 0; i <= lim; i++) if(i < rev[i]) swap(A[i], A[rev[i]]);
        for(int mid = 1; mid < lim; mid <<= 1) {
            int Wn = GPow[mid << 1];
            for(int i = 0; i < lim; i += (mid << 1)) {
                for(int j = 0, w = 1; j < mid; j++, w = mul(w, Wn)) {
                    int x = A[i + j], y = mul(w, A[i + j + mid]);
                    A[i + j] = add(x, y), A[i + j + mid] = add(x, -y);
                }
            }
        }
        if(opt == -1) {
            reverse(A + 1, A + lim);
            int Inv = fp(lim, mod - 2);
            for(int i = 0; i <= lim; i++) mul2(A[i], Inv);
        }
    }
    void Mul(int *a, int *b, int N, int M) {
        memset(A, 0, sizeof(A)); memset(B, 0, sizeof(B));
        int lim = 1, len = 0; 
        while(lim <= N + M) len++, lim <<= 1;
        for(int i = 0; i <= N; i++) A[i] = a[i]; 
        for(int i = 0; i <= M; i++) B[i] = b[i];
        NTT(A, lim, 1); NTT(B, lim, 1);
        for(int i = 0; i <= lim; i++) B[i] = mul(B[i], A[i]);
        NTT(B, lim, -1);
        for(int i = 0; i <= N + M; i++) b[i] = B[i];
        memset(A, 0, sizeof(A)); memset(B, 0, sizeof(B));
    }
    void Inv(int *a, int *b, int len) {//B1 = 2B - A1 * B^2 
        if(len == 1) {b[0] = fp(a[0], mod - 2); return ;}
        Inv(a, b, len >> 1);
        for(int i = 0; i < len; i++) A[i] = a[i], B[i] = b[i];
        NTT(A, len << 1, 1); NTT(B, len << 1, 1);
        for(int i = 0; i < (len << 1); i++) mul2(A[i], mul(B[i], B[i]));
        NTT(A, len << 1, -1);
        for(int i = 0; i < len; i++) add2(b[i], add(b[i], -A[i]));
        for(int i = 0; i < (len << 1); i++) A[i] = B[i] = 0;
    }
    void Dao(int *a, int *b, int len) {
        for(int i = 1; i < len; i++) b[i - 1] = mul(i, a[i]); b[len - 1] = 0;
    }
    void Ji(int *a, int *b, int len) {
        for(int i = 1; i < len; i++) b[i] = mul(a[i - 1], fp(i, mod - 2)); b[0] = 0;
    }
    void Ln(int *a, int *b, int len) {//G(A) = \frac{A}{A'} qiudao zhihou jifen 
        static int A[MAXN], B[MAXN];
        Dao(a, A, len); 
        Inv(a, B, len);
        NTT(A, len << 1, 1); NTT(B, len << 1, 1);
        for(int i = 0; i < (len << 1); i++) B[i] = mul(A[i], B[i]);
        NTT(B, len << 1, -1); 
        Ji(B, b, len << 1);
        memset(A, 0, sizeof(A)); memset(B, 0, sizeof(B));
    }
    void Exp(int *a, int *b, int len) {//F(x) = F_0 (1 - lnF_0 + A) but code ..why....
        if(len == 1) return (void) (b[0] = 1);
        Exp(a, b, len >> 1); Ln(b, C, len);
        C[0] = add(a[0] + 1, -C[0]);
        for(int i = 1; i < len; i++) C[i] = add(a[i], -C[i]);
        NTT(C, len << 1, 1); NTT(b, len << 1, 1);
        for(int i = 0; i < (len << 1); i++) mul2(b[i], C[i]);
        NTT(b, len << 1, -1);
        for(int i = len; i < (len << 1); i++) C[i] = b[i] = 0;
    }
    void Sqrt(int *a, int *b, int len) {
        static int B[MAXN];
        Ln(a, B, len);
        for(int i = 0; i < len; i++) B[i] = mul(B[i], INV2);
        Exp(B, b, len); 
    }
};
using namespace Poly; 
bool flag[MAXN];
signed main() {
	scanf("%s", s);
	N = strlen(s); int Lim = GetLen(N); Init(4 * Lim);
	for(int i = 0; i < N; i++) a[i] = (s[i] == '0'), b[i] = (s[N - i - 1] == '1');
	Mul(a, b, Lim, Lim);
	LL ans = 1ll * N * N;
	for(int i = 1; i <= N; i++) {
		ans ^= 1ll * (N - i) * (N - i);
		for(int j = i; j < N; j += i)
			if(b[N - j - 1] || b[N + j - 1]) 
				{ans ^= 1ll * (N - i) * (N - i); break;}
	}	
	cout << ans;
    return 0;
}
posted @ 2019-03-13 21:45  自为风月马前卒  阅读(357)  评论(0编辑  收藏  举报

Contact with me