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 b, b 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.
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.
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.
6 1 1 1 2 2 2
-1
6 2 2 1 1 4 6
1 2 4 1 2 6
题意及其解题思路:输入一个n(n是3的倍数),接着输入n个数(每一个数a都是a>0&&a<=7),把这n个数按照3个一组分成n/3组,每一组都满足a<b<c && b%a=0 && c%b=0。然而1-7中只有124,126,136这三组满足,所以把(不能有5和7等)不满足条件的情况去掉,再把124,126,136分别有几组就输出几组。找规律看出1个数应该为n/3,2个数>=4个数,6个数>=3个数,2个数+3个数=4个数+6个数。
#include"stdio.h"
#include"string.h"
#include"stdlib.h"
#include"math.h"
#include"algorithm"
using namespace std;
int main()
{
int a,i,j,sum,n;
int xx[10];
while(~scanf("%d",&n))
{
memset(xx,0,sizeof(xx));
for(i=0;i<n;i++)
{
scanf("%d",&a);
xx[a]++;
}
if(xx[1]!=n/3 || xx[5]!=0 || xx[7]!=0 || xx[2]<xx[4] || xx[6]<xx[3] || xx[2]+xx[3]!=xx[4]+xx[6])
{
printf("-1\n");continue;
}
else
{
for(j=0;j<xx[4];j++)
{
printf("1 2 4\n");
}
for(j=0;j<xx[2]-xx[4];j++)
{
printf("1 2 6\n");
}
for(j=0;j<xx[3];j++)
{
printf("1 3 6\n");
}
}
}
return 0;
}