当前位置:网站首页>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;
}
边栏推荐
- 手摸手实现Canal如何接入MySQL实现数据写操作监听
- 21、 TF coordinate transformation (I): coordinate MSG message
- charles如何安装并使用
- Image steganography method
- 5、 C language judgment statement
- Tensorflow GPU installation process record
- Enterprise wechat customer service link, enterprise wechat customer service chat
- Mysql使用left join连表查询时,因连接条件未加索引导致查询很慢
- Qt development tips
- 3564. 日期类
猜你喜欢

PS modify the length and width pixels and file size of photos

Chapter I Introduction

Feeling about software development work in the second anniversary

Application of edge technology and applet container in smart home

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

苹果iPhone手机APP应用图标隐藏怎么找回恢复显示在iPhone苹果手机桌面显示被隐藏的应用APP图标到iPhone苹果手机桌面?

2021-09-02

SQL labs detailed problem solving process (less1-less10)

iPhone苹果手机上一些不想让他人看到的APP应用图标怎么设置手机桌面上的APP应用设置隐藏不让显示在手机桌面隐藏后自己可以正常使用的方法?

Find papers and their open source code
随机推荐
iPhone苹果手机上一些不想让他人看到的APP应用图标怎么设置手机桌面上的APP应用设置隐藏不让显示在手机桌面隐藏后自己可以正常使用的方法?
20、 ROS distributed communication
How to gracefully encapsulate anonymous inner classes (DSL, high-order functions) in kotlin
企业微信客服链接,企业微信客服聊天
C callback function, interface function pointer as function parameter, function pointer as structure member
Install scikit learn journey
3438. Number system conversion
Hard disk partition method
Mysql易错知识点整理(待更新)
idea调试burpsuit插件
Mysql使用left join连表查询时,因连接条件未加索引导致查询很慢
Enterprise wechat customer service link, enterprise wechat customer service chat
Feeling about software development work in the second anniversary
pyppeteer 遇到的问题
Chapter 3 stack, queue and array
苹果iPhone手机APP应用图标隐藏怎么找回恢复显示在iPhone苹果手机桌面显示被隐藏的应用APP图标到iPhone苹果手机桌面?
Application of edge technology and applet container in smart home
RepVGG论文详解以及使用Pytorch进行模型复现
5、 C language judgment statement
Pytorch GPU installation