2017多校訓練Contest5: 1001 Rikka with Candies hdu6085

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 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)(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
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壓進一個bitset x1

從大到小枚舉k,每次把大於k的b的所有倍數壓入bitset x2裏

然後直接(x1>>k)&x2求出當前k的答案

手寫會好一點,用系統的bitset需要卡一下常數

#include<map>
#include<cmath>
#include<queue>
#include<bitset>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<cassert>
#include<iostream>
#include<algorithm>
using namespace std;
inline int read()
{
    int x=0;char ch=getchar();
    while(ch<'0'||ch>'9'){ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x;
}
inline bool cmp(int x,int y)
{
    return x>y;
}
bitset<50051> x1,x2;
int a[50051],b[50051];
int ans[50051];
int main()
{

    int T;
    //scanf("%d",&T);
    T=read();
    while(T>0)
    {
        T--;
        int n,m,q;
        //scanf("%d%d%d",&n,&m,&q);
        n=read();
        m=read();
        q=read();
        int i,j;
        int maxx=0;
        for(i=1;i<=n;i++)
        {
            //scanf("%d",&a[i]);
            a[i]=read();
            maxx=max(maxx,a[i]);
        }
        for(i=1;i<=m;i++)
        {
            //scanf("%d",&b[i]);
            b[i]=read();
            maxx=max(maxx,b[i]);
        }
        sort(b+1,b+1+m,cmp);
        x1.reset();
        x2.reset();
        for(i=1;i<=n;i++)
            //x1[a[i]]=1;
            x1.flip(a[i]);
        int d=1;
        b[0]=0; 
        memset(ans,0,sizeof(ans));
        for(i=maxx;i>=0;i--)
        {
            while(d<=m&&b[d]>i)
            {
                for(j=0;j<=maxx;j+=b[d])
                    //x2[j]=(x2[j]^1);
                    x2.flip(j);
                d++;
                //x2.flip(0);
            }
            //x3=((x1>>i)&x2);
            ans[i]=((x1>>i)&x2).count();
        }
        int x;
        for(i=1;i<=q;i++)
        {
            //scanf("%d",&x);
            x=read();
            if(ans[x]&1)
                puts("1");
            else
                puts("0");
            //printf("%d\n",ans[x]%2);
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章