HDU 6085 Rikka with Candies(bitset)

256 篇文章 0 订阅

Description

给出两个序列 A1,...,n B1,...,m ,给出 q 次查询,每次查询给出一整数k,查询满足 Aik mod Bj (i,j) 对数

Input

第一行一整数 T 表示用例组数,每组用例首先输入三个整数n,m,q分别表示两个序列的长度和查询数,之后输入 A1,...,n B1,...,m ,最后 q 行每行一个整数k表示一个查询

(1T5,1n,m,q50000,1Ai,Bi50000,0k<max Bi,AiAj,BiBj,ij)

Output

对于每组查询,输出满足 Aik mod Bj (i,j) 对数,结果模 2

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

Solution

Aik mod Bj等价于 Aik0 mod Bj ,且注意到 k<Bj ,用bitset存 A B,从大到小枚举 k ,每次把大于k Bj 插入到B中,那么 ans[k]=((A>>k)&B).count()&1 ,时间复杂度 O(M232),M=max Bj

Code

#include<cstdio>
#include<bitset> 
#include<cstring>
using namespace std;
namespace fastIO 
{
    #define BUF_SIZE 100000
    //fread -> read
    bool IOerror=0;
    inline char nc() 
    {
        static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
        if(p1==pend) 
        {
            p1=buf;
            pend=buf+fread(buf,1,BUF_SIZE,stdin);
            if(pend==p1) 
            {
                IOerror=1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) 
    {
        return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
    }
    inline void read(int &x) 
    {
        char ch;
        while(blank(ch=nc()));
        if(IOerror)return;
        for(x=ch-'0';(ch=nc())>='0'&&ch<='9';x=x*10+ch-'0');
    }
    inline void readc(char &x)
    {
        char ch;
        while(blank(ch=nc()));
        if(IOerror)return;
        x=ch;
    }
    #undef BUF_SIZE
};
using namespace fastIO;
#define maxn 50005
bitset<maxn>A,B;
int T,n,m,q,b[maxn],ans[maxn];
int main()
{
    read(T);
    //scanf("%d",&T);
    while(T--)
    {
        read(n),read(m),read(q);
        //scanf("%d%d%d",&n,&m,&q);
        A.reset(),B.reset();
        for(int i=1;i<=n;i++)
        {
            int a;
            read(a);
            //scanf("%d",&a);
            A.set(a);
        }
        memset(b,0,sizeof(b));
        int M=0;
        for(int i=1;i<=m;i++)
        {
            int a;
            read(a);
            //scanf("%d",&a);
            b[a]=1;M=max(M,a);
        }
        for(int i=0;i<=M;i++)ans[i]=0;
        for(int i=M;i>=0;i--)
        {
            ans[i]=((B<<i)&A).count()&1;
            if(b[i])
                for(int k=0;k<=M;k+=i)B.flip(k);
        }
        while(q--)
        {
            int k;
            read(k);
            //scanf("%d",&k);
            printf("%d\n",ans[k]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值