103351: [Atcoder]ABC335 B - Tetrahedral Number

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:6 Solved:0

Description

Score : $150$ points

Problem Statement

You are given an integer $N$.

Print all triples of non-negative integers $(x,y,z)$ such that $x+y+z\leq N$ in ascending lexicographical order.

What is lexicographical order for non-negative integer triples?

A triple of non-negative integers $(x,y,z)$ is said to be lexicographically smaller than $(x',y',z')$ if and only if one of the following holds:

  • $x < x'$;
  • $x=x'$ and $y< y'$;
  • $x=x'$ and $y=y'$ and $z< z'$.

Constraints

  • $0 \leq N \leq 21$
  • $N$ is an integer.

Input

The input is given from Standard Input in the following format:

$N$

Output

Print all triples of non-negative integers $(x,y,z)$ such that $x+y+z\leq N$ in ascending lexicographical order, with $x,y,z$ separated by spaces, one triple per line.


Sample Input 1

3

Sample Output 1

0 0 0
0 0 1
0 0 2
0 0 3
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 3 0
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
1 2 0
2 0 0
2 0 1
2 1 0
3 0 0

Sample Input 2

4

Sample Output 2

0 0 0
0 0 1
0 0 2
0 0 3
0 0 4
0 1 0
0 1 1
0 1 2
0 1 3
0 2 0
0 2 1
0 2 2
0 3 0
0 3 1
0 4 0
1 0 0
1 0 1
1 0 2
1 0 3
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 3 0
2 0 0
2 0 1
2 0 2
2 1 0
2 1 1
2 2 0
3 0 0
3 0 1
3 1 0
4 0 0

Output

分数:$150$分

问题描述

你将得到一个整数$N$。

按字典序递增顺序打印出所有满足$x+y+z\leq N$的非负整数三元组$(x,y,z)$。

什么是非负整数三元组的字典序?

一个非负整数三元组$(x,y,z)$被认为是 字典序较小 的,当且仅当以下之一成立:

  • $x < x'$;
  • $x=x'$且$y< y'$;
  • $x=x'$且$y=y'$且$z< z'$。

限制条件

  • $0 \leq N \leq 21$
  • $N$是一个整数。

输入

输入以标准输入的形式给出,如下所示:

$N$

输出

按字典序递增顺序打印出所有满足$x+y+z\leq N$的非负整数三元组$(x,y,z)$,用空格分隔$x,y,z$,每行一个三元组。


样例输入1

3

样例输出1

0 0 0
0 0 1
0 0 2
0 0 3
0 1 0
0 1 1
0 1 2
0 2 0
0 2 1
0 3 0
1 0 0
1 0 1
1 0 2
1 1 0
1 1 1
1 2 0
2 0 0
2 0 1
2 1 0
3 0 0

样例输入2

4

样例输出2

0 0 0
0 0 1
0 0 2
0 0 3
0 0 4
0 1 0
0 1 1
0 1 2
0 1 3
0 2 0
0 2 1
0 2 2
0 3 0
0 3 1
0 4 0
1 0 0
1 0 1
1 0 2
1 0 3
1 1 0
1 1 1
1 1 2
1 2 0
1 2 1
1 3 0
2 0 0
2 0 1
2 0 2
2 1 0
2 1 1
2 2 0
3 0 0
3 0 1
3 1 0
4 0 0

HINT

从小到大输出符合要求的x、y、z

加入题单

上一题 下一题 算法标签: