Cerinta completa

You are given an integer . A set, , of triples is beautiful if and only if:

  • Let be the set of different ‘s in , be the set of different ‘s in , and be the set of different in . Then .

The third condition means that all ‘s are pairwise distinct. The same goes for and .

Given , find any beautiful set having a maximum number of elements. Then print the cardinality of (i.e., ) on a new line, followed by lines where each line contains space-separated integers describing the respective values of , , and .

Input Format

A single integer, .

Constraints

Output Format

On the first line, print the cardinality of (i.e., ).
For each of the subsequent lines, print three space-separated numbers per line describing the respective values of , , and for triple in .

Sample Input

3

Sample Output

3
0 1 2
2 0 1
1 2 0

Explanation

In this case, . We need to construct a set, , of non-negative integer triples () where . has the following triples:

We then print the cardinality of this set, , on a new line, followed by lines where each line contains three space-separated values describing a triple in .


Limbajul de programare folosit: java8

Cod:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int max = n/3*2;
        if (n%3==2)
            max++;
        int sn = n;
        if (n%3==1)
            sn--;
        System.out.println(max+1);
        int first = max;
        for (int i = (max+1)/2; i >= 0; i--) {
            System.out.println(first+" "+i+" "+(n-i-first));
            first--;
        }
        for (int i = sn-first-1; first >= 0; i--) {
            System.out.println(first+" "+i+" "+(n-i-first));
            first--;
        }
    }
}

Scor obtinut: 1.0

Submission ID: 464612629

Link challenge: https://www.hackerrank.com/challenges/beautiful-3-set/problem

Beautiful 3 Set