A.Xenia and Divisors

题意:给定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;
}
View Code

相关文章:

  • 2021-04-24
  • 2021-12-04
  • 2021-11-19
  • 2021-04-27
  • 2021-08-14
  • 2021-08-20
  • 2021-06-21
猜你喜欢
  • 2022-12-23
  • 2021-08-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-08-12
相关资源
相似解决方案