結果

問題 No.1058 素敵な数
ユーザー ccppjsrbccppjsrb
提出日時 2020-07-09 12:04:47
言語 Go
(1.22.1)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 2,323 bytes
コンパイル時間 14,200 ms
コンパイル使用メモリ 235,524 KB
実行使用メモリ 10,768 KB
最終ジャッジ日時 2024-04-16 04:04:03
合計ジャッジ時間 14,152 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,816 KB
testcase_01 AC 53 ms
10,764 KB
testcase_02 AC 54 ms
10,768 KB
testcase_03 AC 54 ms
10,764 KB
testcase_04 AC 54 ms
10,768 KB
testcase_05 AC 54 ms
10,764 KB
testcase_06 AC 54 ms
10,764 KB
testcase_07 AC 54 ms
10,764 KB
testcase_08 AC 54 ms
10,764 KB
testcase_09 AC 54 ms
10,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

func configure(scanner *bufio.Scanner) {
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 1000005), 1000005)
}
func getNextString(scanner *bufio.Scanner) string {
	scanned := scanner.Scan()
	if !scanned {
		panic("scan failed")
	}
	return scanner.Text()
}
func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}
func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}
func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}
func main() {
	fp := os.Stdin
	wfp := os.Stdout
	extra := 0
	if os.Getenv("I") == "IronMan" {
		fp, _ = os.Open(os.Getenv("END_GAME"))
		extra = 100
	}
	scanner := bufio.NewScanner(fp)
	configure(scanner)
	writer := bufio.NewWriter(wfp)
	defer func() {
		r := recover()
		if r != nil {
			fmt.Fprintln(writer, r)
		}
		writer.Flush()
	}()
	solve(scanner, writer)
	for i := 0; i < extra; i++ {
		fmt.Fprintln(writer, "-----------------------------------")
		solve(scanner, writer)
	}
}
func solve(scanner *bufio.Scanner, writer *bufio.Writer) {
	n := getNextInt(scanner)
	if n == 1 {
		fmt.Fprintln(writer, 1)
		return
	}
	n--
	mx := int(1e6)
	era := make([]int, mx)
	for i := 2; i < mx; i++ {
		for j := i; j < mx; j += i {
			if era[j] == 0 {
				era[j] = i
			}
		}
	}
	pp := make([]int64, 0)
	for i := 100001; i < mx && len(pp) < 10; i++ {
		if era[i] == i {
			pp = append(pp, int64(i))
		}
	}

	l := make(ll, 0)
	for i := 0; i < 10; i++ {
		for j := 0; j < 10; j++ {
			l = append(l, pp[i]*pp[j])
		}
	}
	sort.SliceStable(l, l.less)
	c := newCompress(l)
	for i := 0; i < len(l); i++ {
		if c.get(l[i]) == n-1 {
			fmt.Fprintln(writer, l[i])
			return
		}
	}
}

type ll []int64

func (l ll) less(i, j int) bool {
	return l[i] < l[j]
}

type compress map[int64]int

func newCompress(aa []int64) compress {
	n := len(aa)
	bb := make([]int64, n)
	copy(bb, aa)
	sort.SliceStable(bb, func(i, j int) bool {
		return bb[i] < bb[j]
	})
	c := compress{}
	l := 0
	i := 0
	for r := 0; r < n; r++ {
		for r > l && bb[r] != bb[l] {
			l++
		}
		if r == l {
			c[bb[l]] = i
			i++
		}
	}
	return c
}
func (c compress) get(x int64) int {
	return c[x]
}
0