aboutsummaryrefslogtreecommitdiff
path: root/works
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-10-15 15:48:13 +0800
committercrupest <crupest@outlook.com>2020-10-15 15:48:13 +0800
commit189313e0f901339bdeb80013772564e360e3013e (patch)
tree6a984fdac38f69dc85a0da5cbef695ae182ec4d5 /works
parent1478c6b582e194a330e1f1f76e2b6c423be5e29d (diff)
downloadcrupest-189313e0f901339bdeb80013772564e360e3013e.tar.gz
crupest-189313e0f901339bdeb80013772564e360e3013e.tar.bz2
crupest-189313e0f901339bdeb80013772564e360e3013e.zip
import(solutions): Add problem 526 .
Diffstat (limited to 'works')
-rw-r--r--works/solutions/cpp/526.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/works/solutions/cpp/526.cpp b/works/solutions/cpp/526.cpp
new file mode 100644
index 0000000..19f445f
--- /dev/null
+++ b/works/solutions/cpp/526.cpp
@@ -0,0 +1,29 @@
+#include <vector>
+
+using std::vector;
+
+class Solution {
+public:
+ void dfs(int N, int index, bool *num_used, int &result) {
+ if (index > N) {
+ result += 1;
+ return;
+ }
+
+ for (int num = 1; num <= N; num++) {
+ if (!num_used[num] && (num % index == 0 || index % num == 0)) {
+ num_used[num] = true;
+ dfs(N, index + 1, num_used, result);
+ num_used[num] = false;
+ }
+ }
+ }
+
+ int countArrangement(int N) {
+ bool *num_used = new bool[N + 1]{false};
+ int result = 0;
+ dfs(N, 1, num_used, result);
+ delete[] num_used;
+ return result;
+ }
+};