Problem
As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:
There are n children and m kinds of candies. The ith child has Ai dollars and the unit price of the ith kind of candy is Bi. The amount of each kind is infinity.
Each child has his favorite candy, so he will buy this kind of candies as much as possible and will not buy any candies of other kinds. For example, if this child has 10 dollars and the unit price of his favorite candy is 4 dollars, then he will buy two candies and go home with 2 dollars left.
Now Yuta has q queries, each of them gives a number k. For each query, Yuta wants to know the number of the pairs (i,j)(1≤i≤n,1≤j≤m) which satisfies if the ith child’s favorite candy is the jth kind, he will take k dollars home.
To reduce the difficulty, Rikka just need to calculate the answer modulo 2.
But It is still too difficult for Rikka. Can you help her?
Limit
1≤n,m,q≤50000
1≤Ai,Bi≤50000
0≤ki<maxBi
Ai≠Aj,Bi≠Bj for all i≠j
Idea
对任意一个 Bi , 对余数 k 的贡献可以认为是, [0,Bi) , [Bi,2×Bi) , [2×Bi,3×Bi) … 这些区间中对应 A 的个数相应的映射到 [0,Bi) 的余数区间。
枚举遍历 Bi 并对每个区间到余数区间进行映射并做异或,总体的复杂度将达到 O(N2) 。由于题意指明仅需通过 01 考虑,故通过位操作优化复杂度为 O(N232)
Code
#include<bits/stdc++.h>
using namespace std;
const unsigned MAXU = 0xFFFFFFFF;
const int N = 50000 + 10;
unsigned u[32] = {1};
int t, n, m, q, k, b[N];
struct BITSET {
unsigned b[3200];
int size;
void reset() { memset(b, 0, sizeof(b)); }
void reset(int pos) {
b[pos>>5] &= MAXU ^ (1u<<(pos&31));
}
void set() { memset(b, 0xff, sizeof(b)); }
void set(int pos) {
b[pos>>5] |= (1u<<(pos&31));
}
void flip() {
int seg = size / 32;
for(int i=0;i<seg;i++)
b[i] ^= MAXU;
for(int pos=seg*32;pos<size;pos++)
b[pos>>5] ^= (1u<<(pos&31));
}
void flip(int pos) {
b[pos>>5] ^= (1u<<(pos&31));
}
bool test(int pos) {
return b[pos>>5] & (1u<<(pos&31));
}
} ans, A;
void XOR(unsigned *dst, unsigned *src, int srcl, int srcr) {
int len = srcr - srcl + 1, sseg = len / 32, sft = srcl&31;
unsigned long long tmp = 0;
int slseg = srcl/32, dlseg = 0;
for(;dlseg < sseg;dlseg++, slseg++)
{
((tmp=src[slseg+1])<<=32) |= src[slseg];
dst[dlseg] ^= ((tmp >> sft) & MAXU);
}
if(len % 32) {
((tmp=src[slseg+1])<<=32) |= src[slseg];
dst[dlseg] ^= ((tmp >> sft) & u[(len-1)%32]);
}
}
int main()
{
for(int i=1;i<32;i++) u[i] = u[i-1] + (1<<i);
scanf("%d", &t);
while(t-- && scanf("%d %d %d", &n, &m, &q))
{
A.reset();
ans.reset();
for(int i=1, a;i<=n;i++)
{
scanf("%d", &a);
A.set(a);
}
int mx = 0;
for(int i=1;i<=m;i++)
scanf("%d", &b[i]), mx = max(mx, b[i]);
sort(b+1, b+m+1);
for(int i=1;i<=m;i++)
for(int j=0;j<=mx;j+=b[i])
XOR(ans.b, A.b, j, j+b[i]-1);
while(q--)
{
scanf("%d", &k);
printf("%d\n", ans.test(k)?1:0);
}
}
}