結果

問題 No.1006 Share an Integer
ユーザー 小野寺健小野寺健
提出日時 2021-12-18 12:47:44
言語 Go
(1.22.1)
結果
AC  
実行時間 270 ms / 2,000 ms
コード長 1,081 bytes
コンパイル時間 15,199 ms
コンパイル使用メモリ 213,604 KB
実行使用メモリ 22,532 KB
最終ジャッジ日時 2023-10-13 17:02:17
合計ジャッジ時間 18,904 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 1 ms
4,352 KB
testcase_04 AC 2 ms
4,356 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 134 ms
14,184 KB
testcase_12 AC 251 ms
20,464 KB
testcase_13 AC 259 ms
22,532 KB
testcase_14 AC 270 ms
22,532 KB
testcase_15 AC 208 ms
20,452 KB
testcase_16 AC 83 ms
12,096 KB
testcase_17 AC 129 ms
14,212 KB
testcase_18 AC 217 ms
20,444 KB
testcase_19 AC 203 ms
18,372 KB
testcase_20 AC 229 ms
20,448 KB
testcase_21 AC 268 ms
22,532 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func main() {
	r := bufio.NewReader(os.Stdin)
	w := bufio.NewWriter(os.Stdout)
	defer w.Flush()
	var X int
	fmt.Fscan(r, &X)
	d := make([]int, X+1)
	d[0] = 0
	for i := 1; i <= X; i++ {
		d[i] = 1
	}
	for i := 2; i <= X; i++ {
		if d[i] == 1 {
			d[i] = 2
			for j := 2 * i; j <= X; j += i {
				n := 1
				k := j
				for k % i == 0 {
					n++;
					k /= i
				}
				d[j] *= n
			}
		}
	}
	min := X
	var minpair [][2]int
	for a := 1; a <= X/2; a++ {
		b := X - a
		v := Abs(a - d[a] - b + d[b])
		if min > v {
			min = v
			minpair = make([][2]int, 0, 2)
			minpair = append(minpair, [2]int{a, b})
			if a != b {
				minpair = append(minpair, [2]int{b, a})
			}
		} else if min == v {
			minpair = append(minpair, [2]int{a, b})
			if a != b {
				minpair = append(minpair, [2]int{b, a})
			}		
		}
	}
	sort.Slice(minpair, func(i,j int) bool {return minpair[i][0] < minpair[j][0]})
	for _, pair:= range minpair {
		fmt.Fprintln(w, pair[0], pair[1])
	}
}

func Abs(x int) int {
	if x >= 0 {
		return x
	} else {
		return -x
	}
}
0