hdu 6085(bitset)

来源:互联网 发布:淘宝宽屏海报代码 编辑:程序博客网 时间:2024/05/23 18:29

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 nn children and mm kinds of candies. The iith child has AiAi dollars and the unit price of the iith kind of candy is BiBi. 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 1010 dollars and the unit price of his favorite candy is 44 dollars, then he will buy two candies and go home with 22 dollars left.

Now Yuta has qq queries, each of them gives a number kk. For each query, Yuta wants to know the number of the pairs (i,j)(1≤i≤n,1≤j≤m)(i,j)(1≤i≤n,1≤j≤m) which satisfies if the iith child’s favorite candy is the jjth kind, he will take kk dollars home.

To reduce the difficulty, Rikka just need to calculate the answer modulo 22.

But It is still too difficult for Rikka. Can you help her?
Input
The first line contains a number t(1≤t≤5)t(1≤t≤5), the number of the testcases.

For each testcase, the first line contains three numbers n,m,q(1≤n,m,q≤50000)n,m,q(1≤n,m,q≤50000).

The second line contains nn numbers Ai(1 ≤ Ai ≤ 50000)Ai(1≤ Ai ≤50000) and the third line contains mm numbers Bi(1≤ Bi ≤ 50000)Bi(1≤ Bi ≤50000).

Then the fourth line contains qq numbers ki(0 ≤ ki < maxBi)ki(0≤ ki < maxBi) , which describes the queries.

It is guaranteed that Ai≠Aj,Bi≠BjAi≠Aj,Bi≠Bj for all i≠ji≠j.
Output
For each query, print a single line with a single 0101 digit – the answer.
Sample Input
1
5 5 5
1 2 3 4 5
1 2 3 4 5
0 1 2 3 4
Sample Output
0
0
0
0
1

就是给你A集合 n个数,B集合 m个数 q个询问
问有多少对 (i,j) 使得a[i]%b[j]==k
转化得 a[i]-k=tb[j].

因为集合数量已经到达 5e4,所以暴力是不行的,利用bitset 对一个集合进行某项操作 例如 -k

从大到小枚举最大的k,因为k一定不能比b集合里面最大的数大,所以从最大的数开始 从大到小枚举,然后更新,如果有i存在,那么相当于 在0~N中 存在着 i,2i,3i这样的模数,更新就好,由于小于等于当前k的数对k是没有贡献的,所以从大到小一直更新就好,由于只要求奇偶,所以&1 就好

#include <bits/stdc++.h>using namespace std;const int N = 50000+5;bitset<N> a,b,ans,bx;void solve(int x){    bx.reset();    for(int i=x;i>=0;i--)    {        ans[i]=(bx&(a>>i)).count()&1;        if(b[i])        {            for(int j=0;j<N;j+=i)                bx.flip(j);        }    }}int main(){    int t;    scanf("%d",&t);    while(t--)    {        a.reset(),b.reset();        int n,m,q,x,maxt=0;        scanf("%d%d%d",&n,&m,&q);        for(int i=1;i<=n;i++)        {            scanf("%d",&x);            a.set(x);        }        for(int i=1;i<=m;i++)        {            scanf("%d",&x);            b.set(x);            maxt=max(x,maxt);        }        ans.reset();        solve(maxt);        while(q--)        {            int x;            scanf("%d",&x);            puts(ans[x]?"1":"0");        }    }}
原创粉丝点击