HDU 6085 Rikka with Candies

来源:互联网 发布:python ftplib 编辑:程序博客网 时间:2024/06/05 10:38

因为题目要求答案mod 2,所以类似CS Academy #32的Light Count,将5e4的空间压缩成大概800份,每份空间用64位ULL状压。

对于每个qk,求得多少对ai % bj = qk, 即ai = bj*x+qk && qk<=bj-1.
将q数组和b数组从大到小排序后,枚举qk,如果存在还未使用过的bj>qk,那么类似于素数筛,将0, bj, 2*bj, 3*bj…异或进d数组,如果大于qk的bj都取完了,那么查询qk就相当于将整个d数组右移qk位,并将这个右移过的d数组与a数组进行&操作。

代码:

#include<bits/stdc++.h>using namespace std;#define CLR(A, X) memset(A, X, sizeof(A))typedef unsigned long long LL;const int N = 5e4+5, M = 8e2+5;int b[N], c[N], g[N], ans[N];LL d[M], a[M];void add(LL *f, int pos) { f[pos>>6] ^= (LL)1<<(pos&63); }int query(int t, int m) {    int x = t>>6, y = t&63, cnt = 0;    for(int i = 0; i < m; i++) {        if(i+x < m) cnt ^= __builtin_popcountll(a[i+x]&(d[i]<<y)); else break;        if(i+x+1<m && y) cnt ^= __builtin_popcountll(a[i+x+1]&(d[i]>>(64-y)));    }    return cnt&1;}int main() {    int T;    scanf("%d", &T);    while(T--) {        CLR(d, 0); CLR(a, 0);        int n, m, q, x, mx = 0;        scanf("%d%d%d", &n, &m, &q);        for(int i = 0; i < n; i++) { scanf("%d", &x); add(a, x); mx = max(mx, x); }        for(int i = 1; i <= m; i++) scanf("%d", &b[i]);        for(int i = 1; i <= q; i++) scanf("%d", &c[i]), g[i] = c[i];        sort(b+1, b+m+1); sort(c+1, c+q+1);        int i = m, j = unique(c+1, c+q+1)-c-1, mn = (mx>>6)+1;        while(i>=1 || j>=1) {            while(i>=1 && b[i]>c[j]) {                for(int x = 0; x <= mx; x += b[i]) add(d, x);                i--;            }            while(j>=1 && c[j]>=b[i]) ans[c[j]] = query(c[j], mn), j--;        }        for(int i = 1; i <= q; i++) printf("%d\n", ans[g[i]]);    }    return 0;}