A. Xenia and Divisors
来源:互联网 发布:双色球算法公式 编辑:程序博客网 时间:2024/04/25 01:59
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.
61 1 1 2 2 2
-1
62 2 1 1 4 6
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;}
- A. Xenia and Divisors
- CodeForces 342A Xenia and Divisors
- Codeforces 342 A. Xenia and Divisors
- CodeForces 342A Xenia and Divisors
- Xenia and Divisors
- A. Xenia and Divisors codeforces-problem-342A
- CF 342A(Xenia and Divisors-考虑有限情况)
- CodeForces #199Div.2-A.Xenia and Divisors
- Codeforces Round #199 (Div. 2) A. Xenia and Divisors
- CF 342A Xenia and Divisors (数学规律)
- Code Forces 342 A. Xenia and Divisors 思维+暴力
- CF 342A Xenia and Divisors (数学规律)
- Codeforces Round #199 (Div. 2) -- A. Xenia and Divisors (思路)
- codeforces 339A.Helpful Maths B.Xenia and Ringroad 两水题
- B. Xenia and Ringroad
- E - Xenia and Dominoes
- Xenia and Ringroad
- Xenia and Spies
- 快速开发:功能限定与项目修复
- ARM寻址方式
- verilog分频器设计
- 【热门主题】魔兽世界游戏主题
- Objective-C的学习心得
- A. Xenia and Divisors
- glReadPixels glDrawPixels glCopyPixels
- Android理解:Activity状态和生命周期
- USB枚举
- CodeForces 148B - Escape
- Sql Server 2008完全卸载方法(其他版本类似)
- Linux应用程序开发相关技术学习心得
- vim 多窗口
- 扩展JQuery和JS的方法