题目描述
解题思路
循环遍历数组,判断因数找出所有的有序二元组对,并用集合进行存储,遍历集合,以i,j,k,l互不相等为条件,生成有序四元组,计算四元组个数
附代码
#include<bits/stdc++.h>
#include<unordered_map>
#include<unordered_set>
#define ll long long
using namespace std;
set<pair<int, int>> s;
int main() {
int N;
cin >> N;
vector<int> nums;
while (N--) {
int temp;
cin >> temp;
nums.push_back(temp);
}
for (int i = 0; i < nums.size(); i++) {
for (int j = i + 1; j < nums.size(); j++) {
if (nums[i] % nums[j] == 0)
s.insert({ j + 1,i + 1 });
if(nums[j] % nums[i] == 0)
s.insert({ i + 1,j + 1 }); //判断ai和aj是否为对方的因数
}
}
int sum = 0;
for (auto t1 : s) {
for (auto t2 : s) {
if (t1.first != t2.first && t1.first != t2.second && t1.second != t2.first && t1.second != t2.second)
sum++;
}
}
cout << sum;
return 0;
}