hdu 6085(bitset)

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");
        }
    }
}
發佈了60 篇原創文章 · 獲贊 4 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章