A. Xenia and Divisors

来源:互联网 发布:双色球算法公式 编辑:程序博客网 时间:2024/04/25 01:59

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Xenia the mathematician has a sequence consisting of n (n is divisible by 3) positive integers, each of them is at most 7. She wants to split the sequence into groups of three so that for each group of three a, b, c the following conditions held:

  • a < b < c;
  • a divides bb divides c.

Naturally, Xenia wants each element of the sequence to belong to exactly one group of three. Thus, if the required partition exists, then it has  groups of three.

Help Xenia, find the required partition or else say that it doesn't exist.

Input

The first line contains integer n (3 ≤ n ≤ 99999) — the number of elements in the sequence. The next line contains n positive integers, each of them is at most 7.

It is guaranteed that n is divisible by 3.

Output

If the required partition exists, print  groups of three. Print each group as values of the elements it contains. You should print values in increasing order. Separate the groups and integers in groups by whitespaces. If there are multiple solutions, you can print any of them.

If there is no solution, print -1.

Sample test(s)
input
61 1 1 2 2 2
output
-1
input
62 2 1 1 4 6
output
1 2 41 2 6

解题说明:此题输入n个数,求出n/3组数确保第二个数被第一个数整除,第三个数被第二个数整除。虽然n的范围很大,但是由于输入的数字最大为7,题目变得简单很多。在1到7内来分析,满足条件的只有 1 2 4, 1 2 6, 1 3 6这三种情况。于是我们只需要统计1-7中每个数出现的次数,用一个数组来存放次数。在不出现5和7,且1 2 4 6满足一定条件下才可找到题目要求的输出。


#include <iostream>#include<algorithm>#include<cstdio>#include<cmath>#include<cstring>#include<cstdlib>using namespace std;int main(){int n,a[7]={0},i,t,s=1;    scanf("%d",&n);    for(i=0;i<n;i++) {scanf("%d",&t);if(t==5||t==7) {s=0;}else{a[t]++;}}    if(s==1&&a[1]==a[2]+a[3]&&a[1]==a[4]+a[6]&&a[2]>=a[4])    {        for(i=0;i<a[4];i++) {printf("1 2 4\n");}for(i=0;i<a[6]-a[3];i++){printf("1 2 6\n");}for(i=0;i<a[3];i++){printf("1 3 6\n");}    }    else{printf("-1\n");}return 0;}


原创粉丝点击