B - Xenia and Divisors

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.

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.

Example
Input
6
1 1 1 2 2 2
Output
-1
Input
6
2 2 1 1 4 6
Output
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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值