题面
DBMB had a birthday yesterday. He was gifted an array of elements and a number . But there is one problem: he only likes arrays where the sum of the elements equals . To make the array appealing to him, you can perform the following operation any number of times:
- Choose an index () and add to the number .
For example, if he was given the array and , you can choose index and get the array . 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 () — the number of test cases. The following describes the test cases.
The first line of each test case contains three integers , , (, ).
The second line of each test case contains integers () — 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.
思路
题目的意思是对一个数组任意一个元素加上给定的 若干次,检查得到的最终数组的各元素之和是否为想要的目标数
也就是说,当这个数组的初始和为 时,经过若干次操作之后,一定满足:
为了求出这个符合条件的 ,进行移项,得到:
若 合法,则一定满足下面的条件:
- ( 与 的差必须能被 整除) 如果 能达到上述条件,那么就能够出现这样一个方式使得最终数组的和为期望的
代码
#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; } }}