题目链接:题目
大意:
按某种方式排列n*n方阵,问给出的方阵重新排序后有没有可能成为这种方阵。
思路:
一个这种方阵仅由n,c,d以及a[0][0]决定,所以它唯一可能对应的方阵我们可以找到,然后就是比较两个方阵里的数能不能对应,就像比较两个字符串是不是错排了,只要分别排序再比较就行了。
另一种方法就是,因为可以看做是找配对的问题,我们可以把给出的数字都放到一个集合里,然后看我们所需的数字是否都有,由于数字可能重复,所以不能使用unordered_set,只能使用set所以复杂度还是一样。
代码:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define MOD 1000000007
#define fi first
#define se second
#define pii pair<int,int>
#define vec vector
int solve(int n,int c,int d,vec<int> a){
sort(a.begin(),a.end());
vec<int> q;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
q.push_back(a[0]+c*i+d*j);
}
}
sort(q.begin(),q.end());
for(int i=0;i<n*n;i++){
if(a[i]!=q[i])return false;
}
return true;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--){
int n,c,d;
cin>>n>>c>>d;
vec<int> a(n*n);
for(int i=0;i<n*n;i++)cin>>a[i];
if(solve(n,c,d,a))cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}