当前位置:网站首页>4519. Number of square arrays
4519. Number of square arrays
2022-07-28 15:13:00 【Pursue people far away】
Given an array of nonnegative integers A, If the sum of each pair of adjacent elements in the array is a complete square , This array is called a square array .
return A The number of squares arranged by .
Two permutations A1 and A2 Different if and only if there is an index i, bring A1[i]≠A2[i].
Input format
The first line contains an integer n, Represents an array AA The length of .
The second line contains n It's an integer A[i].
Output format
An integer , Express A The number of squares arranged by .
Data range
1≤n≤12,
0≤A[i]≤109.
sample input :
3
1 17 8
sample output :
2
Sample explanation
[1,8,17] and [17,8,1] They're all effective permutations .
Code :
// Pay attention to the skills of weight judgment Special treatment of equal values
#include <bits/stdc++.h>
using namespace std;
const int N = 15;
int n;
int w[N];
bool st[N];
int ans;
bool check(int x)
{
int r = sqrt(x);
return x == r * r;
}
void dfs(int u, int last)
{
if (u == n)
{
ans++;
return;
}
for (int i = 0; i < n; i++)
{
if (st[i])
continue;
if (i && !st[i - 1] && w[i] == w[i - 1]) // If the previous one has not been accessed and is equal to the current value It indicates that the previous one has been searched and unmarked Then the current one does not need to be searched
continue;
if (!check(w[i] + last))
continue;
st[i] = 1;
dfs(u + 1, w[i]);
st[i] = 0;
}
}
int main()
{
cin >> n;
for (int i = 0; i < n; i++)
cin >> w[i];
sort(w, w + n);
for (int i = 0; i < n; i++)
{
if (!i || w[i] != w[i - 1]) // Prevent permutations with the same results
{
st[i] = true;
dfs(1, w[i]);
st[i] = 0;
}
}
cout << ans << endl;
return 0;
}
边栏推荐
- 3715. 最少交换次数
- View gnuradio version
- CCSP international registered cloud security experts set up examination rooms in China
- QT hex, decimal, qbytearray, qstring data conversion
- 3477. Simple sorting
- 3564. 日期类
- charles如何安装并使用
- What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]
- Gradle -- package multiple variants with gradle
- 4、 C language operators
猜你喜欢

Machine learning related concepts

Chapter I Introduction

RepVGG论文详解以及使用Pytorch进行模型复现

Deploy flask on Alibaba cloud server

滑块还原和验证(法律数据库)

Idea2020.1.4 packages package collapse

Solution to the problem of high collapse caused by float (including all methods)

Repvgg paper explanation and model reproduction using pytoch

6、 C language circular statement

Foundation of knowledge atlas (II) - knowledge expression system of knowledge atlas
随机推荐
云上安全主要面临的威胁有哪些
DataTables warning: table id=campaignTable - Cannot reinitialise DataTable.解决
Some considerations for installing Oracle11g
云计算需要考虑的安全技术列举
Examples of Pareto optimality and Nash equilibrium
3477. Simple sorting
Chapter I Introduction
Solve blast database error: error pre fetching sequence data
Basic operation implementation of sequence table
iframe 标签
Development status of security and privacy computing in China
Shell command
3438. Number system conversion
Modify the default path of Jupiter notebook
How to conduct risk assessment related to intellectual property rights
PS modify the length and width pixels and file size of photos
Chapter 3 stack, queue and array
Why do enterprises need user autonomous digital identity
安全与隐私计算在国内发展现状
Mlx90640 infrared thermal imager sensor module development notes (VIII)