結果

問題 No.6 使いものにならないハッシュ
ユーザー yukirinyukirin
提出日時 2016-03-14 18:36:56
言語 Go
(1.22.1)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,473 bytes
コンパイル時間 12,949 ms
コンパイル使用メモリ 209,020 KB
実行使用メモリ 5,536 KB
最終ジャッジ日時 2023-10-14 22:57:51
合計ジャッジ時間 14,534 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)
var rdr = bufio.NewReaderSize(os.Stdin, 1000000)

func main() {
	sc.Split(bufio.ScanWords)
	k, n := nextInt(), nextInt()
	ps := eratosthenes(n)
	for i, v := range ps {
		if v >= k {
			ps = ps[i:]
			break
		}
	}

	h := make([]int, len(ps))
	for i, v := range ps {
		h[i] = hash(v)
	}

	m := make([]int, 10)
	l, tmp := 0, 0
	c := make([]int, 2)

	for i, v := range h {
		if m[v] != 0 {
			if tmp >= c[0] {
				c = []int{tmp, l}
			}
			tmp -= m[v] - l
			tmp++
			tmpL := l
			l = m[v]
			for _, v := range h[tmpL:m[v]] {
				m[v] = 0
			}

			m[v] = i + 1
			continue
		}
		m[v] = i + 1
		tmp++
	}

	if tmp >= c[0] {
		c = []int{tmp, l}
	}

	fmt.Println(ps[c[1]])
}

func hash(i int) int {
	n := 0
	for ; i > 0; i /= 10 {
		n += i % 10
	}
	if n >= 10 {
		return hash(n)
	}
	return n
}

func nextLine() string {
	sc.Scan()
	return sc.Text()
}

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

func eratosthenes(n int) []int {
	if n < 2 {
		return []int{}
	}

	r := int(math.Floor(math.Sqrt(float64(n))))
	list := make([]bool, n+1)
	list[0], list[1] = true, true

	for i := 2; i <= r; i++ {
		if !list[i] {
			for j := i * i; j <= n; j += i {
				list[j] = true
			}
		}
	}

	l := n / int(math.Ceil(math.Log(float64(n))))
	primes := make([]int, 0, l)
	for i, v := range list {
		if v {
			continue
		}
		primes = append(primes, i)
	}

	return primes
}
0