HDU6085-Rikka with Candies

此题涉及数学和编程竞赛中的查询问题,任务是计算特定条件下糖果购买情况的组合数量,并对结果进行模运算。通过使用bitset优化数据结构来提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Rikka with Candies

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


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  i th child has  Ai  dollars and the unit price of the  i th 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  i th child’s favorite candy is the  j th 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
 

Source
 

题意:给你数组a和数组b,数组a大小为n,数组b大小为m,有q次询问,每次询问有多少a[i]%b[j]==k,答案对2取模

解题思路:先用一个bitset记录下数组a包括哪些,再枚举b,然后用bitset记录下b的倍数,然后每次看有几个a等于b的倍数加k


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <unordered_map>
#include <functional>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

int a[50004], b[50004], ans[50004];
bitset<50004>x, aa;
int n, m, q, ma, k, vis[50004];

int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        x.reset();
        aa.reset();
        memset(vis, 0, sizeof vis);
        ma = 0;
        scanf("%d%d%d", &n, &m, &q);
        for (int i = 1; i <= n; i++) scanf("%d", &a[i]), aa.set(a[i]);
        for (int i = 1; i <= m; i++) scanf("%d", &b[i]), ma = max(ma, b[i]), vis[b[i]] = 1;
        for (int i = ma; i >= 0; i--)
        {
            ans[i] = ((x << i)&aa).count() & 1;
            if (vis[i])
            {
                for (int j = 0; j <= ma; j += i)
                    x.flip(j);
            }
        }
        for (int i = 1; i <= q; i++)
        {
            scanf("%d", &k);
            printf("%d\n", ans[k]);
        }
    }
    return 0;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值