题意:给定N个数,每个数的取值范围为1-7,N是3的倍数,判定是否能够恰好将N个数分成若干三元组,使得一个组中的元素a,b,c满足 a < b < c 并且 a|b && b|c(整除)。
分析:满足这种条件的三元组只有[1, 2, 4], [1, 2, 6], [1, 3, 6]因此如果有5或者是7肯定是不行的,然后是1的个数要等于4和6的个数之和,最后就是2的数量必须4的数量。
#include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int N = 100005; int n; int cnt[10]; int main() { int x; bool flag = false; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &x); if (x == 5 || x == 7) flag = true; cnt[x]++; } if (flag || cnt[1] != cnt[4]+cnt[6] || cnt[2] < cnt[4]) { puts("-1"); return 0; } for (int i = 0; i < cnt[4]; ++i) puts("1 2 4"); for (int i = 0; i < cnt[3]; ++i) puts("1 3 6"); for (int i = 0; i < cnt[6]-cnt[3]; ++i) puts("1 2 6"); return 0; }