当前位置:网站首页>4519. 正方形数组的数目
4519. 正方形数组的数目
2022-07-28 14:15:00 【追寻远方的人】
给定一个非负整数数组 A,如果该数组每对相邻元素之和是一个完全平方数,则称这一数组为正方形数组。
返回 A 的正方形排列的数目。
两个排列 A1 和 A2 不同的充要条件是存在某个索引 i,使得A1[i]≠A2[i]。
输入格式
第一行包含一个整数 n,表示数组 AA的长度。
第二行包含 n 个整数 A[i]。
输出格式
一个整数,表示 A 的正方形排列的数目。
数据范围
1≤n≤12,
0≤A[i]≤109。
输入样例:
3
1 17 8
输出样例:
2
样例解释
[1,8,17] 和[17,8,1] 都是有效的排列。
代码:
// 注意判重的技巧 值相等的特殊处理
#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]) // 若前一个没有访问过且与当前的值相等 说明前一个已经搜索完了取消标记 则当前的不必再搜索
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]) // 防止有相同结果的排列
{
st[i] = true;
dfs(1, w[i]);
st[i] = 0;
}
}
cout << ans << endl;
return 0;
}
边栏推荐
- Foundation of knowledge atlas (II) - knowledge expression system of knowledge atlas
- Vtkcellpicker picking triangular patches
- Idea2020.1.4 packages package collapse
- Crawler: from entry to imprisonment (II) -- Web collector
- Feeling about software development work in the second anniversary
- Is the expansion operator a deep copy or a shallow copy
- The second pre class exercise
- MLX90640 红外热成像仪传感器模块开发笔记(八)
- 使用cpolar发布树莓派网页(apache2的安装测试)
- Chapter II linear table
猜你喜欢

MySQL authorization method

Introduction to mqtt protocol

企业微信客服链接,企业微信客服聊天

Compilation language and interpretation language

JS学习笔记18-23

Examples of Pareto optimality and Nash equilibrium

RepVGG论文详解以及使用Pytorch进行模型复现
Gradle -- package multiple variants with gradle

Chapter 3 stack, queue and array

6、 C language circular statement
随机推荐
Is the expansion operator a deep copy or a shallow copy
Instructions for common symbols in kotlin
How to conduct risk assessment related to intellectual property rights
Multi merchant mall system function disassembly lecture 17 - platform side order list
Enumeration type
CCSP 云安全设计原则都有哪些
The second pre class exercise
Using keras to visualize the network model, failed to import pydot appears
Product Manager
Solution to the problem of high collapse caused by float (including all methods)
Solve blast database error: error pre fetching sequence data
Picture Trojan principle production prevention
URP下使用GL进行绘制方法
Introduction to mqtt protocol
即刻体验 | 借助 CTS-D 进一步提升应用设备兼容性
SQL error [1810] [22008]: ora-01810: format code occurs twice
Application of edge technology and applet container in smart home
2、 Declaration and definition of variables and constants
4、 C language operators
MITK create module