HDU 6085 Rikka with Candies (bitset)

Rikka with Candies

HDU 6085 2017ACM暑期多校联合训练 - Team 5 1001 Rikka with Candies

题目链接

题目描述
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)(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?
输入
The first line contains a number 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).

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

Then the fourth line contains q numbers ki(0≤ki

#include<iostream>
#include<cstring>
#include<cstdio>
#include<bitset>
using namespace std;
int vis[50040];
int output[50050];
bitset<50050>tmp,ans,temp;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(vis,0,sizeof(vis));
        tmp.reset();//初始化,将所有的二进制定位0
        ans.reset();
        int n,m,qq,maxx=0;
        scanf("%d%d%d",&n,&m,&qq);
        int x;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            tmp.set(x);//将tmp中x处的二进制改为1
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&x);
            maxx=max(maxx,x);
            vis[x]=1;//标记单价
        }
        for(int i=maxx;i>=0;i--)
        {
            temp=ans&(tmp>>i);//tmp中的二进制向右移动i位
            output[i]=temp.count()%2;//计算temp中二进制为1的个数
            if(vis[i]==1)
                for(int j=0;j<=maxx;j+=i)
                    ans.flip(j);//二进制取反
        }
        while(qq--)
        {
            scanf("%d",&x);
            printf("%d
",output[x]);
        }
    }
    return 0;
}



原文地址:https://www.cnblogs.com/nanfenggu/p/7900056.html