拼多多笔试 拼多多笔试题 0817

笔试时间:2025年8月17日

往年笔试合集:

2023春招秋招笔试合集

2024春招秋招笔试合集

第一题

多多正在参加一个特殊的满减活动,有n个各不相同的商品,每个商品的价格是a_i。活动规则是——若挑选的两个商品的价格总和是m的倍数的话,可以免费带走这两个商品。由于多多最多只能带两个商品,请问多多有多少种组合方式免费带两个商品。

输入描述

第一行输入两个数字n和m。

接下来输入n个商品的价格a_i,a_i为整数。

1 ≤ n ≤ 200000,1 ≤ m < 200000,1 ≤ a_i ≤ 1000000000

输出描述

输出一个数字,表示多多能免费带走两个商品的方式数量。最终结果对998244353取模

样例输入

2 4

1 3

样例输出

1

解释: 多多只有1种方式免费拿走两件商品。第1件商品和第2件商品

参考题解

先把所有价格按给定数取模做频次统计;把余数为零的内部两两配对计入;对每个正余数只与它的补余配对;若给定数为偶数,再额外计算一半位置的内部配对;结果取指定模数。

C++:

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    const long long MOD = 998244353;
    int itemCount, modulus;
    if (!(cin >> itemCount >> modulus)) return 0;
    vector<long long> remainderFreq(modulus, 0);
    for (int i = 0; i < itemCount; ++i) {
        long long price; 
        cin >> price;
        remainderFreq[price % modulus] += 1;
    }
    long long pairCount = 0;
    pairCount += remainderFreq[0] * (remainderFreq[0] - 1) / 2;
    for (int rem = 1; rem < (modulus + 1) / 2; ++rem) {
        pairCount += remainderFreq[rem] * remainderFreq[modulus - rem];
    }
    if (modulus % 2 == 0) {
        int half = modulus / 2;
        pairCount += remainderFreq[half] * (remainderFreq[half] - 1) / 2;
    }
    cout << (pairCount % MOD) << "\n";
    return 0;
}

Java:

import java.io.*;
import java.util.*;

public class Main {
    static final long MOD = 998244353L;

    // 快速输入
    static final class FastScanner {
        private final InputStream in;
        private final byte[] buffer = new byte[1 << 16];
        private int ptr = 0, len = 0;
        FastScanner(InputStream is) { this.in = is; }
        private int read() throws IOException {
            if (ptr >= len) {
                len = in.read(buffer);
                ptr = 0;
                if (len <= 0) return -1;
            }
            return buffer[ptr++];
        }
        int nextInt() throws IOException {
            int c, sgn = 1, x = 0;
            do { c = read(); } while (c <= 32 && c != -1);
            if (c == '-') { sgn = -1; c = read(); }
            while (c > 32 && c != -1) {
                x = x * 10 + (c - '0');
                c = read();
            }
            return x * sgn;
        }
        long nextLong() throws IOException {
            int c; long x = 0; int sgn = 1;
            do { c = read(); } while (c <= 32 && c != -1);
            if (c == '-') { sgn = -1; c = read(); }
            while (c > 32 && c != -1) {
                x = x * 10 + (c - '0');
                c = read();
            }
            return x * sgn;
        }
    }

    public static void main(String[] args) throws Exception {
        FastScanner fs = new FastScanner(System.in);

        int itemCount = fs.nextInt();
        int modulus = fs.nextInt();

        long[] remainderFreq = new long[modulus];
        for (int i = 0; i < itemCount; i++) {
            long price = fs.nextLong();
            int r = (int) ((price % modulus + modulus) % modulus);
            remainderFreq[r] += 1;
        }

        long pairCount = 0L;

        // r = 0
        pairCount += remainderFreq[0] * (remainderFreq[0] - 1) / 2;

        // 1..(modulus-1)/2
        for (int rem = 1; rem < (modulus + 1) / 2; rem++) {
            pairCount += remainderFreq[rem] * remainderFreq[modulus - rem];
        }

        // rem = modulus/2 when modulus even
        if ((modulus & 1) == 0) {
            int half = modulus / 2;
            pairCount += remainderFreq[half] * (remainderFreq[half] - 1) / 2;
        }

        System.out.println(pairCount % MOD);
    }
}

Python:

import sys

def main():
    data = list(map(int, sys.stdin.buffer.read().split()))
    it = iter(data)
    item_count = next(it)
    modulus = next(it)

    remainder_freq = [0] * modulus
    for _ in range(item_count):
        price = next(it)
        r = price % modulus
        remainder_freq[r] += 1

    pair_count = 0

    # r = 0
    c0 = remainder_freq[0]
    pair_count += c0 * (c0 - 1) // 2

    # 1..(modulus-1)//2
    for rem in range(1, (modulus + 1) // 2):
        pair_count += remainder_freq[rem] * remainder_freq[modulus - rem]

    # rem = modulus//2 when modulus even
    if modulus % 2 == 0:
        half = modulus // 2
        ch = remainder_freq[half]
        pair_count += ch * (ch - 1) // 2

    print(pair_count % 998244353)

if __name__ == "__main__":
    main()

第二题

多多先生经营着一个生机勃勃的多多果园。果园里不仅种满了各种果树,还吸引了许许多多可爱的小动物前来定居。最近,果园的收获季到了,多多先生收获了许多筐香甜的果实。在果园的一角,有一个存放备用果实的大仓库,仓库里有X筐果实。神奇的是,从第1天开始,每天早晨都会有一只新的小动物来到果园,希望能在这里定居。每只小动物的食量都不同。如果多多先生允许第i天来的小动物留下,那么这只小动物就会从“当天(第i天)”开始,一直到第n天结束,每天都需要吃掉c_i筐果实才能感到幸福。多多先生非常善良,他希望果园里的小动物越多越好。但是,他也必须保证每一只留在果园的小动物,从它来到的那天起直到第n天,每一天都能吃到足够的食物,绝不能有任何一只小动物挨饿。多多先生可以选择拒绝任何一只前来定居的小动物。被拒绝的小动物会默默地离开,不会消耗果园的任何果实。请问,在第n天结束时,多多先生的果园里最多能有多少只幸福的小动物?

输入描述

输入的第一行包含两个整数n和X(1≤n≤200000,1≤X≤10^18),分别表示果园收获季的总天数,以及初始时仓库里备用的果实筐数。

输入的第二行包含n个整数c_1, c_2, …, c_n(1≤c_i≤300),其中c_i表示第i天前来定居的小动物的每日食量。

数字之间用空格隔开。

输出描述

输出一个整数,表示在第n天结束时,在保证所有小动物每天都能吃饱、一直幸福的前提下,果园里所能拥有的小动物的最大数量。

样例输入

3 8

2 2 2

样例输出

2

参考题解

把每只对象的总消耗量等于到季末的天数乘以日需求,全部排序后从小到大贪心累加,直到超过总预算为止,累加数量即为答案。

C++:

#include <bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int dayCount; 
    long long fruitStock;
    if (!(cin >> dayCount >> fruitStock)) return 0;
    vector<long long> dailyIntake(dayCount);
    for (int i = 0; i < dayCount; ++i) cin >> dailyIntake[i];
    vector<long long> totalCost(dayCount);
    for (int i = 0; i < dayCount; ++i) totalCost[i] = dailyIntake[i] * 1LL * (dayCount - i);
    sort(totalCost.begin(), totalCost.end());
    __int128 usedCost = 0;
    int maxCount = 0;
    for (long long cost : totalCost) {
        if (usedCost + cost <= (__int128)fruitStock) {
            usedCost += cost;
            ++maxCount;
        } else break;
    }
    cout << maxCount << "\n";
    return 0;
}

Java:

import java.io.*;
import java.util.*;
import java.math.BigInteger;

public class Main {
    // 简单快速输入
    static final class FastScanner {
        private final InputStream in;
        private final byte[] buf = new byte[1 << 16];
        private int ptr = 0, len = 0;
        FastScanner(InputStream is) { this.in = is; }
        private int read() throws IOException {
            if (ptr >= len) {
                len = in.read(buf);
                ptr = 0;
                if (len <= 0) return -1;
            }
            return buf[ptr++];
        }
        int nextInt() throws IOException {
            int c; do { c = read(); } while (c <= 32 && c != -1);
            int sgn = 1; if (c == '-') { sgn = -1; c = read(); }
            int x = 0; while (c > 32 && c != -1) { x = x * 10 + (c - '0'); c = read(); }
            return x * sgn;
        }
        long nextLong() throws IOException {
            int c; do { c = read(); } while (c <= 32 && c != -1);
            int sgn = 1; if (c == '-') { sgn = -1; c = read(); }
            long x = 0; while (c > 32 && c != -1) { x = x * 10 + (c - '0'); c = read(); }
            return x * sgn;
        }
    }

    public static void main(String[] args) throws Exception {
        FastScanner fs = new FastScanner(System.in);

        int dayCount = fs.nextInt();
        long fruitStock = fs.nextLong();

        long[] dailyIntake = new long[dayCount];
       

剩余60%内容,订阅专栏后可继续查看/也可单篇购买

2025 春招笔试合集 文章被收录于专栏

2025打怪升级记录,大厂笔试合集 C++, Java, Python等多种语言做法集合指南

全部评论
我连题都没看懂
点赞 回复 分享
发布于 08-19 15:24 山东

相关推荐

08-16 10:51
已编辑
门头沟学院 Java
点赞 评论 收藏
分享
15道选择题+3道sql题,没想象中的难
投递拼多多集团-PDD等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务