520 字
3 分钟
DBMB-and-the-Array

题面#

DBMB had a birthday yesterday. He was gifted an array aa of nn elements and a number xx. But there is one problem: he only likes arrays where the sum of the elements equals ss. To make the array appealing to him, you can perform the following operation any number of times:

  • Choose an index ii (1in1 \le i \le n) and add xx to the number aia_i.

For example, if he was given the array [1,2,3,5][1, 2, 3, 5] and x=2x = 2, you can choose index 33 and get the array [1,2,5,5][1, 2, 5, 5]. Your task is to determine whether the array can appeal to DBMB after any number of operations.

Input

Each test consists of several test cases. The first line contains a single integer tt (1t10001 \le t \le 1000) — the number of test cases. The following describes the test cases.

The first line of each test case contains three integers nn, ss, xx (1n,x101 \le n, x \le 10, 1s1001 \le s \le 100).

The second line of each test case contains nn integers a1,a2,ana_1, a_2, \dots a_n (1ai101 \le a_i \le 10) — the elements of the array gifted to DBMB.

Output

For each test case, output “YES” if the array can appeal to DBMB. Otherwise, output “NO”.

You can output each letter in any case (lowercase or uppercase). For example, the strings “yEs”, “yes”, “Yes”, and “YES” will be accepted as a positive answer.

思路#

题目的意思是对一个数组任意一个元素加上给定的 xx 若干次,检查得到的最终数组的各元素之和是否为想要的目标数 ss

也就是说,当这个数组的初始和为 S0S_0 时,经过若干次操作之后,一定满足:

S=S0+kx,k0S = S_0 + kx, k \geq 0

为了求出这个符合条件的 kk,进行移项,得到:

k=SS0xk = \frac{S - S_0}{x}

kk 合法,则一定满足下面的条件:

  • SS0S \geq S_0
  • (SS0) mod x=0(S - S_0)\space mod \space x = 0SSS0S_0的差必须能被 xx 整除) 如果 kk 能达到上述条件,那么就能够出现这样一个方式使得最终数组的和为期望的 ss

代码#

#include <bits/stdc++.h>
using namespace std;
int sum(vector<int>& a) {
int result = 0;
for (auto& n: a) {
result += n;
}
return result;
}
int main() {
int t;
cin >> t;
while (t --) {
int n, s, x;
cin >> n >> s >> x;
vector<int> a(n + 1);
for (int i = 1; i <= n; i ++) cin >> a[i];
int cur = sum(a);
if (cur <= s && (s - cur) % x == 0) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
}
DBMB-and-the-Array
https://github.com/posts/dbmb-and-the-array/
作者
FZSGBall
发布于
2026-04-02
许可协议
CC BY-NC-SA 4.0