Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
魔方陣の解の数を一瞬で求めるプログラム 5x5の魔方陣の場合 http://blog.unfindable.net/archives/7466
#include <iostream>
const int s = 65;
constexpr int eq(int a, int b) { return a != b ? 0 : a; }
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22, int x24, int x14, int x23, int x8, int x18, int x6, int x10, int x11, int x15, int x16, int x20) {
return (x20 < 1 || x20 > 25) ? 0 :
(((used & (1 << x20)) ? 0 : 1));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22, int x24, int x14, int x23, int x8, int x18, int x6, int x10, int x11, int x15, int x16) {
return (x16 < 1 || x16 > 25) ? 0 :
(((used & (1 << x16)) ? 0 : check(used + (1 << x16), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, x23, x8, x18, x6, x10, x11, x15, x16, eq(s - x16 - x17 - x18 - x19, s - x5 - x10 - x15 - x25))));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22, int x24, int x14, int x23, int x8, int x18, int x6, int x10, int x11, int x15) {
return (x15 < 1 || x15 > 25) ? 0 :
(((used & (1 << x15)) ? 0 : check(used + (1 << x15), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, x23, x8, x18, x6, x10, x11, x15, s - x1 - x6 - x11 - x21)));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22, int x24, int x14, int x23, int x8, int x18, int x6, int x10, int x11) {
return (x11 > 25) ? 0 :
(((used & (1 << x11)) ? 0 : check(used + (1 << x11), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, x23, x8, x18, x6, x10, x11, s - x11 - x12 - x13 - x14))
+ check(used, x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, x23, x8, x18, x6, x10, x11 + 1));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22, int x24, int x14, int x23, int x8, int x18, int x6, int x10) {
return (x10 < 1 || x10 > 25) ? 0 :
(((used & (1 << x10)) ? 0 : check(used + (1 << x10), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, x23, x8, x18, x6, x10, 1)));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22, int x24, int x14, int x23, int x8, int x18, int x6) {
return (x6 > 25) ? 0 :
(((used & (1 << x6)) ? 0 : check(used + (1 << x6), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, x23, x8, x18, x6, s - x6 - x7 - x8 - x9))
+ check(used, x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, x23, x8, x18, x6 + 1));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22, int x24, int x14, int x23, int x8, int x18) {
return (x18 < 1 || x18 > 25) ? 0 :
(((used & (1 << x18)) ? 0 : check(used + (1 << x18), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, x23, x8, x18, 1)));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22, int x24, int x14, int x23, int x8) {
return (x8 > 25) ? 0 :
(((used & (1 << x8)) ? 0 : check(used + (1 << x8), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, x23, x8, s - x3 - x8 - x13 - x23))
+ check(used, x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, x23, x8 + 1));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22, int x24, int x14, int x23) {
return (x23 < 1 || x23 > 25) ? 0 :
(((used & (1 << x23)) ? 0 : check(used + (1 << x23), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, x23, 1)));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22, int x24, int x14) {
return (x14 < 1 || x14 > 25) ? 0 :
(((used & (1 << x14)) ? 0 : check(used + (1 << x14), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, x14, s - x21 - x22 - x24 - x25)));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22, int x24) {
return (x24 > 25) ? 0 :
(((used & (1 << x24)) ? 0 : check(used + (1 << x24), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24, s - x4 - x9 - x19 - x24))
+ check(used, x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, x24 + 1));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12, int x22) {
return (x22 < 1 || x22 > 25) ? 0 :
(((used & (1 << x22)) ? 0 : check(used + (1 << x22), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, x22, 1)));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4, int x12) {
return (x12 > 25) ? 0 :
(((used & (1 << x12)) ? 0 : check(used + (1 << x12), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12, s - x2 - x7 - x12 - x17))
+ check(used, x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, x12 + 1));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3, int x4) {
return (x4 < 1 || x4 > 25) ? 0 :
(((used & (1 << x4)) ? 0 : check(used + (1 << x4), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, x4, 1)));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2, int x3) {
return (x3 > 25) ? 0 :
(((used & (1 << x3)) ? 0 : check(used + (1 << x3), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3, s - x1 - x2 - x3 - x5))
+ check(used, x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, x3 + 1));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21, int x2) {
return (x2 > 25) ? 0 :
(((used & (1 << x2)) ? 0 : check(used + (1 << x2), x7, x9, x17, x19, x13, x1, x25, x5, x21, x2, 1))
+ check(used, x7, x9, x17, x19, x13, x1, x25, x5, x21, x2 + 1));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5, int x21) {
return (x21 < 1 || x21 > 25) ? 0 :
(((used & (1 << x21)) ? 0 : check(used + (1 << x21), x7, x9, x17, x19, x13, x1, x25, x5, x21, 1)));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25, int x5) {
return (x5 > 25) ? 0 :
(((used & (1 << x5)) ? 0 : check(used + (1 << x5), x7, x9, x17, x19, x13, x1, x25, x5, s - x5 - x9 - x13 - x17))
+ check(used, x7, x9, x17, x19, x13, x1, x25, x5 + 1));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1, int x25) {
return (x25 < 1 || x25 > 25) ? 0 :
(((used & (1 << x25)) ? 0 : check(used + (1 << x25), x7, x9, x17, x19, x13, x1, x25, 1)));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13, int x1) {
return (x1 > 25) ? 0 :
(((used & (1 << x1)) ? 0 : check(used + (1 << x1), x7, x9, x17, x19, x13, x1, s - x1 - x7 - x13 - x19))
+ check(used, x7, x9, x17, x19, x13, x1 + 1));
}
constexpr int check(int used, int x7, int x9, int x17, int x19, int x13) {
return (x13 > 25) ? 0 :
(((used & (1 << x13)) ? 0 : check(used + (1 << x13), x7, x9, x17, x19, x13, 1))
+ check(used, x7, x9, x17, x19, x13 + 1));
}
constexpr int check(int used, int x7, int x9, int x17, int x19) {
return (x19 > 25) ? 0 :
(((used & (1 << x19)) ? 0 : check(used + (1 << x19), x7, x9, x17, x19, 1))
+ check(used, x7, x9, x17, x19 + 1));
}
constexpr int check(int used, int x7, int x9, int x17) {
return (x17 > 25) ? 0 :
(((used & (1 << x17)) ? 0 : check(used + (1 << x17), x7, x9, x17, x7 + 1))
+ check(used, x7, x9, x17 + 1));
}
constexpr int check(int used, int x7, int x9) {
return (x9 > 25) ? 0 :
(((used & (1 << x9)) ? 0 : check(used + (1 << x9), x7, x9, x9 + 1))
+ check(used, x7, x9 + 1));
}
constexpr int check(int used, int x7) {
return (x7 > 22) ? 0 :
(((used & (1 << x7)) ? 0 : check(used + (1 << x7), x7, x7 + 1))
+ check(used, x7 + 1));
}
int main() {
constexpr int result = check(0,1);
std::cout << result << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment