hdu 6085 Rikka with Candies(bitset 计数原理)

来源:互联网 发布:js去隐藏域传值 编辑:程序博客网 时间:2024/06/07 18:00

Rikka with Candies

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1237    Accepted Submission(s): 542


Problem Description
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 10dollars 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)(1in,1jm) 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?
 

Input
The first line contains a number t(1t5), the number of the testcases. 

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

The second line contains n numbers Ai(1Ai50000) and the third line contains m numbers Bi(1Bi50000).

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

It is guaranteed that AiAj,BiBj for all ij.
 

Output
For each query, print a single line with a single 01 digit -- the answer.
 

Sample Input
15 5 51 2 3 4 51 2 3 4 50 1 2 3 4
 

Sample Output
00001

http://blog.csdn.net/luricheng/article/details/77006185

解:求出B数组中最大的数x作为开始枚举的答案,因为不可能出现超过x的答案,从大到小遍历,(a-i)%b=0,求出bx数组中有多少是b的倍数,就能算出有多少(a-i),

即答案为i的数量

#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<cmath>#include<string>#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]=((a>>i)&bx).count()&1;        if(!b[i]) continue;        for(int j=0;j<N;j+=i)            bx.flip(j);    }    return ;}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=0;i<n;i++)        {            scanf("%d", &x);            a.set(x);        }        for(int i=0;i<m;i++)        {            scanf("%d", &x);            b.set(x);            maxt=max(x,maxt);        }        ans.reset();        solve(maxt);        while(q--)        {            scanf("%d", &x);            if(ans[x])puts("1");            else puts("0");        }    }    return 0;}







原创粉丝点击