当前位置:网站首页>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;
}
边栏推荐
- 【游戏测试工程师】初识游戏测试—你了解它吗?
- Node.js+express realizes the operation of MySQL database
- 10、 C enum enumeration
- SSL socket cross platform solution libevent OpenSSL
- iPhone苹果手机上一些不想让他人看到的APP应用图标怎么设置手机桌面上的APP应用设置隐藏不让显示在手机桌面隐藏后自己可以正常使用的方法?
- idea调试burpsuit插件
- JS learning notes 24-28: end
- Mlx90640 infrared thermal imager sensor module development notes (VIII)
- 19、 ROS parameter name setting
- pyppeteer 遇到的问题
猜你喜欢

Hard disk partition method

PHP memory horse

The difference between @notnull, @notblank, @notempty of commonly used verification annotations

idea调试burpsuit插件

celery 相关

SQL labs detailed problem solving process (less1-less10)

Analysis vulnerability introduction
![What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]](/img/a9/336390db64d871fa1655800c1e0efc.png)
What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]

Mlx90640 infrared thermal imager sensor module development notes (VIII)

RepVGG论文详解以及使用Pytorch进行模型复现
随机推荐
SQL labs detailed problem solving process (less1-less10)
DataTables warning: table id=campaignTable - Cannot reinitialise DataTable.解决
使用cpolar发布树莓派网页(apache2的安装测试)
23、 TF coordinate transformation (III): dynamic coordinate transformation
MLX90640 红外热成像仪传感器模块开发笔记(八)
3、 C language storage class
CCSP international registered cloud security experts set up examination rooms in China
Enterprise wechat customer service link, enterprise wechat customer service chat
URP下使用GL进行绘制方法
即刻体验 | 借助 CTS-D 进一步提升应用设备兼容性
3438. Number system conversion
chrome插件调试
idea调试burpsuit插件
Shader顶点着色器修改顶点高度的一个思路
List of security technologies to be considered in cloud computing
8、 C scope rules
No files or folders found to process
Pytorch GPU installation
Application of edge technology and applet container in smart home
Compilation language and interpretation language