B. Progressive Square
time limit per test: 2 seconds
memory limit per test: 256 megabytes
input: standard input
output: standard output
A progressive square of size n n n is an n × n n \times n n×n matrix. Maxim chooses three integers a 1 , 1 a_{1,1} a1,1, c c c, and d d d and constructs a progressive square according to the following rules:
a i + 1 , j = a i , j + c a_{i+1,j} = a_{i,j} + c ai+1,j=ai,j+c
a i , j + 1 = a i , j + d a_{i,j+1} = a_{i,j} + d ai,j+1=ai,j+d
For example, if n = 3 n = 3 n=3, a 1 , 1 = 1 a_{1,1} = 1 a1,1=1, c = 2 c=2 c=2, and d = 3 d=3 d=3, then the progressive square looks as follows:
( 1 4 7 3 6 9 5 8 11 ) \begin{pmatrix} 1 & 4 & 7 \\ 3 & 6 & 9 \\ 5 & 8 & 11 \end{pmatrix} 1354687911
Last month Maxim constructed a progressive square and remembered the values of n n n, c c c, and d d d. Recently, he found an array b b b of n 2 n^2 n2 integers in random order and wants to make sure that these elements are the elements of that specific square.
It can be shown that for any values of n n n, a 1 , 1 a_{1,1} a1,1, c c c, and d d d, there exists exactly one progressive square that satisfies all the rules.
Input
The first line contains an integer t t t ( 1 ≤ t ≤ 10 4 1 \le t \le {10} ^ 4 1≤t≤104) — the number of test cases.
The first line of each test case contains three integers n n n, c c c, and d d d ( 2 ≤ n ≤ 500 2 \le n \le 500 2≤n≤500, 1 ≤ c , d ≤ 1 0 6 1 \le c, d \le 10^6 1≤c,d≤106) — the size of the square and the values of c c c and d d d as described in the statement.
The second line of each test case contains n ⋅ n n \cdot n n⋅n integers b 1 , b 2 , … , b n ⋅ n b_1, b_2, \dots, b_{n \cdot n} b1,b2,…,bn⋅n ( 1 ≤ b i ≤ 1 0 9 1 \le b_i \le 10^9 1≤bi≤109) — the elements found by Maxim.
It is guaranteed that the sum of n 2 n ^ 2 n2 over all test cases does not exceed 25 ⋅ 10 4 25 \cdot {10} ^ 4 25⋅104.
Output
For each test case, output “YES” in a separate line if a progressive square for the given n n n, c c c, and d d d can be constructed from the array elements a a a, 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.
Example
inputCopy
5
3 2 3
3 9 6 5 7 1 10 4 8
3 2 3
3 9 6 5 7 1 11 4 8
2 100 100
400 300 400 500
3 2 3
3 9 6 6 5 1 11 4 8
4 4 4
15 27 7 19 23 23 11 15 7 3 19 23 11 15 11 15
outputCopy
NO
YES
YES
NO
NO
AC代码:
#include<map>
#include<set>
#include<stack>
#include<cmath>
#include<queue>
#include<string>
#include<bitset>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<numeric>
#include<iomanip>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
const int N=3e5+10;
const int MOD=1e9 + 7;
const int INF=0X3F3F3F3F;
const int dx[]={-1,1,0,0,-1,-1,+1,+1};
const int dy[]={0,0,-1,1,-1,+1,-1,+1};
const int M = 1e6 + 10;
int n, c, d;
int t;
int a[N], b[N];
int main()
{
cin >> t;
while(t --){
int flag = 0;
cin >> n >> c >> d;// c列d行
for(int i = 1; i <= n * n; i ++)
{
cin >> a[i];
}
sort(a + 1, a + 1 + n * n);
b[1] = a[1];
int f = b[1], k = 1;
for(int i = 2; i <= n; i ++)//
{
b[++ k] = f + c;
f = f + c;
}//共n列
for(int i = 1; i <= n; i ++)
{
f = b[i];
for(int j = 2; j <= n; j ++)
{
b[++ k] = f + d;
f = f + d;
}
}
sort(b + 1, b + 1 + n * n);
for(int i = 1; i <= n * n; i ++)
{
if(a[i] != b[i]) flag = 1;
}
if(flag) puts("NO");
else puts("YES");
}
return 0;
}