結果

問題 No.1988 Divisor Tiling
ユーザー HimaHima
提出日時 2022-06-26 18:23:12
言語 Go
(1.22.1)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 1,906 bytes
コンパイル時間 15,331 ms
コンパイル使用メモリ 223,276 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-28 13:21:22
合計ジャッジ時間 13,363 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 1 ms
6,812 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 1 ms
6,940 KB
testcase_06 AC 1 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 2 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,944 KB
testcase_12 AC 2 ms
6,944 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 2 ms
6,944 KB
testcase_15 AC 2 ms
6,944 KB
testcase_16 AC 2 ms
6,944 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 2 ms
6,944 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 3 ms
6,944 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 3 ms
6,940 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 4 ms
6,944 KB
testcase_28 AC 4 ms
6,944 KB
testcase_29 AC 6 ms
6,940 KB
testcase_30 AC 8 ms
6,944 KB
testcase_31 AC 15 ms
6,940 KB
testcase_32 AC 1 ms
6,940 KB
testcase_33 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"errors"
	"fmt"
	"os"
	"sort"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)
var out = bufio.NewWriter(os.Stdout)

func divide(x int) []int {
	m := make(map[int]struct{})
	for i := 1; i*i <= x; i++ {
		if x%i == 0 {
			m[i] = struct{}{}
			m[x/i] = struct{}{}
		}
	}
	var res []int
	for k := range m {
		res = append(res, k)
	}
	sort.Ints(res)
	return res
}

func solve(n, h int) ([][]int, error) {
	w := n / h

	var needRotate bool
	if w < h {
		h, w = w, h
		needRotate = true
	}
	div := divide(n)
	//fmt.Println(div)
	f := make([][]int, h)
	for i := 0; i < h; i++ {
		f[i] = make([]int, w)
	}
	cur := 0
	idx := len(div) - 2
	for div[idx] >= w && idx >= 0 {
		if div[idx]%w != 0 {
			return nil, errors.New("Can not construct.")
		}
		nc := cur + div[idx]/w
		for i := cur; i < nc; i++ {
			for j := 0; j < w; j++ {
				f[i][j] = div[idx]
			}
		}
		cur = nc
		idx--
	}
	//
	j := 0
	for idx >= 0 {
		v, cnt := div[idx], div[idx]
		for cnt > 0 {
			f[cur][j] = v
			cnt--
			j++
		}
		idx--
	}
	if !needRotate {
		return f, nil
	}
	ans := make([][]int, w)
	for i := 0; i < w; i++ {
		ans[i] = make([]int, h)
	}
	for i := 0; i < h; i++ {
		for j := 0; j < w; j++ {
			ans[j][i] = f[i][j]
		}
	}
	return ans, nil
}

func main() {
	buf := make([]byte, 1024*1024)
	sc.Buffer(buf, bufio.MaxScanTokenSize)
	sc.Split(bufio.ScanWords)

	n, h := nextInt(), nextInt()

	ans, err := solve(n, h)
	if err != nil {
		PrintInt(-1)
		return
	}
	PrintVertically(ans)
}

func nextInt() int {
	sc.Scan()
	i, _ := strconv.Atoi(sc.Text())
	return i
}

func PrintInt(x int) {
	defer out.Flush()
	fmt.Fprintln(out, x)
}

func PrintHorizonaly(x []int) {
	defer out.Flush()
	fmt.Fprintf(out, "%d", x[0])
	for i := 1; i < len(x); i++ {
		fmt.Fprintf(out, " %d", x[i])
	}
	fmt.Fprintln(out)
}

func PrintVertically(x [][]int) {
	defer out.Flush()
	for _, v := range x {
		PrintHorizonaly(v)
	}
}
0