原题:
ac代码:(状态压缩dp)
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const ll N = 1 << 21;
const int M = 22;
int maps[M][M];
ll dp[N][M];
int gcd(int a, int b) {
if (b == 0)
return a;
else return gcd(b, a%b);
}
bool judge(int x, int y) {
if (gcd(x, y) == 1)
return true;
else
return false;
}
int main() {
for (int i = 1; i <= 21; i++)
for (int j = 1; j <= 21; j++) {
if (judge(i, j))
maps[i][j] = 1;
}
dp[1][0] = 1;
for (ll i = 0; i < 1 << 21; i++) {
for (ll j = 0; j < 21; j++) {
if (i >> j & 1) {
for (int k = 0; k < 21; k++)
{
if (maps[k + 1][j + 1] == 1 && i >> k & 1) {
dp[i][j] += dp[i - (1 << j)][k];
}
}
}
}
}
ll ans = 0;
for (int i = 0; i < 21; i++)
{
ans += dp[(1 << 21) - 1][i];
}
cout << ans << endl;
return 0;
}