思路:
- 建图: 以所有四位上述六种多变形数为点建图, 存在x到y的一条边当前仅当x的后两位等于y的前两位, 因为任意一个非1的数最多只能属于一个类型即最多是一种多边形数(联立任意两个多边形数的公式的解为0和1), 所以可以更细化地建图: 保存每类多边形的列表, 同时把邻接表的节点按类型分组.
- 题目要求相当于要找一个大小为6的环, 同时其中每个元素的类型各不相同. 可以枚举环中节点类型排列顺序, 再在起始类型中枚举起点, 再通过回溯判断是否存在当前种类排列顺序且包含当前起点的解.
- 计算量:枚举类型排列顺序为 6 ! 6! 6! , 任意一种四位多边形不超过100个所以枚举起点 ≤ 100 \le 100 ≤100, 任意一个节点的出度不超过2,所以回溯找6元环的复杂度 ≤ 2 6 \le 2^6 ≤26, 所以总计算量不超过 6 ! × 100 × 2 6 ≈ 4 e 6 6! \times 100\times 2^6 \approx 4e6 6!×100×26≈4e6.
ll cmp(ll x, int type) {//计算第x项type类型多边形
if (type == 0)
return x * (x + 1) / 2;
if (type == 1)
return x * x;
if (type == 2)
return x * (3 * x - 1) / 2;
if (type == 3)
return x * (2 * x - 1);
if (type == 4)
return x * (5 * x - 3) / 2;
if (type == 5)
return x * (3 * x - 2);
}
int fun() {
vector<int> e[100][6];//e[x][y]:{v1,v2,...} 后两位为x的节点与y类型的前两位为x的vi有一条有向边
vector<int> li[6];//li[i]: 类型为i的节点列表
int res = 0;
for (int i = 1; i <= 10000; i++) {
for (int j = 0; j < 6; j++) {
auto t = cmp(i, j);
if (t >= 1000 && t < 10000) {//建图
e[t / 100][j].push_back(t);
li[j].push_back(t);
}
}
}
vector<int> st;//回溯过程中维护当前访问栈中的节点
vector<int> o(6);//枚举类型顺序
iota(o.begin(), o.end(), 0);
function<void(int, int)> dfs = [&](int v, int ind) {//v: 当前节点 ind: 当前类型
if (res)//已找到答案提前退出
return;
st.push_back(v);
if (ind == 5) {
if (st.front() / 100 == st.back() % 100)// 已经找到解
res = accumulate(st.begin(), st.end(), 0);
st.pop_back();
return;
}
for (auto j : e[v % 100][o[ind + 1]])
dfs(j, ind + 1);
st.pop_back();
};
do {
for (auto x : li[o[0]]) {//枚举起始类型的起点
dfs(x, 0);
if (res)
return res;
}
} while (next_permutation(o.begin(), o.end()));//枚举类型顺序
}