HDU 6085 Rikka with Candies (bitset, 2017 Multi-Univ Training Contest 5)

Problem

Problem Link

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)(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?

Limit

1n,m,q50000

1Ai,Bi50000

0ki<maxBi

AiAj,BiBj for all ij

Idea

对任意一个 Bi , 对余数 k 的贡献可以认为是, [0,Bi) , [Bi,2×Bi) , [2×Bi,3×Bi) … 这些区间中对应 A 的个数相应的映射到 [0,Bi) 的余数区间。

枚举遍历 Bi 并对每个区间到余数区间进行映射并做异或,总体的复杂度将达到 O(N2) 。由于题意指明仅需通过 01 考虑,故通过位操作优化复杂度为 O(N232)

Code

#include<bits/stdc++.h>
using namespace std;
const unsigned MAXU = 0xFFFFFFFF;
const int N = 50000 + 10;
unsigned u[32] = {1};
int t, n, m, q, k, b[N];
struct BITSET {
    unsigned b[3200];
    int size;
    void reset() {  memset(b, 0, sizeof(b));    }
    void reset(int pos) {
        b[pos>>5] &= MAXU ^ (1u<<(pos&31));
    }

    void set() {    memset(b, 0xff, sizeof(b)); }
    void set(int pos) {
        b[pos>>5] |= (1u<<(pos&31));
    }

    void flip() {
        int seg = size / 32;
        for(int i=0;i<seg;i++)
            b[i] ^= MAXU;
        for(int pos=seg*32;pos<size;pos++)
            b[pos>>5] ^= (1u<<(pos&31));
    }
    void flip(int pos) {
        b[pos>>5] ^= (1u<<(pos&31));
    }

    bool test(int pos) {
        return b[pos>>5] & (1u<<(pos&31));
    }
} ans, A;
void XOR(unsigned *dst, unsigned *src, int srcl, int srcr) {
    int len = srcr - srcl + 1, sseg = len / 32, sft = srcl&31;
    unsigned long long tmp = 0;
    int slseg = srcl/32,    dlseg = 0;
    for(;dlseg < sseg;dlseg++, slseg++)
    {
        ((tmp=src[slseg+1])<<=32) |= src[slseg];
        dst[dlseg] ^= ((tmp >> sft) & MAXU);
    }
    if(len % 32) {
        ((tmp=src[slseg+1])<<=32) |= src[slseg];
        dst[dlseg] ^= ((tmp >> sft) & u[(len-1)%32]); 
    }
}
int main()
{
    for(int i=1;i<32;i++)   u[i] = u[i-1] + (1<<i);
    scanf("%d", &t);
    while(t-- && scanf("%d %d %d", &n, &m, &q))
    {
        A.reset();
        ans.reset();
        for(int i=1, a;i<=n;i++)
        {
            scanf("%d", &a);
            A.set(a);
        }
        int mx = 0;
        for(int i=1;i<=m;i++)
            scanf("%d", &b[i]), mx = max(mx, b[i]);
        sort(b+1, b+m+1);
        for(int i=1;i<=m;i++) 
        for(int j=0;j<=mx;j+=b[i])
            XOR(ans.b, A.b, j, j+b[i]-1);
        while(q--)
        {
            scanf("%d", &k);
            printf("%d\n", ans.test(k)?1:0);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值