結果

問題 No.12 限定された素数
ユーザー warashiwarashi
提出日時 2015-10-27 20:18:27
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 1,544 bytes
コンパイル時間 11,849 ms
コンパイル使用メモリ 222,124 KB
実行使用メモリ 35,584 KB
最終ジャッジ日時 2024-04-19 03:27:46
合計ジャッジ時間 14,138 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 65 ms
35,584 KB
testcase_01 AC 58 ms
28,928 KB
testcase_02 AC 55 ms
27,648 KB
testcase_03 AC 45 ms
26,624 KB
testcase_04 AC 63 ms
33,024 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 60 ms
28,672 KB
testcase_08 AC 56 ms
26,880 KB
testcase_09 AC 63 ms
32,640 KB
testcase_10 WA -
testcase_11 AC 59 ms
28,160 KB
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 65 ms
35,584 KB
testcase_18 AC 61 ms
34,816 KB
testcase_19 AC 62 ms
34,816 KB
testcase_20 AC 56 ms
27,776 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"math"
)

var PRIME = SieveOfEratosthenes(5000000)
var count = make([]int, 10)
var A [10]bool

func SieveOfEratosthenes(max int) []int {
	isNotPrime := make([]bool, max+1)
	isNotPrime[0] = true
	isNotPrime[1] = true
	for i := 2; i <= int(math.Sqrt(float64(max))); i++ {
		if isNotPrime[i] {
			continue
		}
		for j := i * i; j <= max; j += i {
			isNotPrime[j] = true
		}
	}
	prime := make([]int, 0, max/2)
	for i, b := range isNotPrime {
		if !b {
			prime = append(prime, i)
		}
	}
	return prime
}

func addNum(n int) {
	for n > 0 {
		count[n%10]++
		n /= 10
	}
}
func check() int {
	for i := 0; i < 10; i++ {
		if !A[i] && count[i] > 0 {
			return 1
		} else if A[i] && count[i] == 0 {
			return -1
		} else if count[i] < 0 {
			panic("count に負の数が入っています")
		}
	}
	return 0
}

func main() {
	var N, tmp int
	ans := make([]int, 0, 10000000)
	fmt.Scan(&N)
	if N == 10 {
		fmt.Println(4999999)
		return
	}
	for i := 0; i < N; i++ {
		fmt.Scan(&tmp)
		A[tmp] = true
	}

	for i := 0; i < len(PRIME); i++ {
		for j := i; j < len(PRIME); j++ {
			addNum(PRIME[j])
			cond := check()
			if cond == 0 {
				var max, min int
				if i == 0 {
					min = 1
				} else {
					min = PRIME[i-1] + 1
				}
				if j+1 >= len(PRIME) {
					max = 5000000
				} else {
					max = PRIME[j+1] - 1
				}
				ans = append(ans, max-min)
			} else if cond > 0 {
				count = make([]int, 10)
				i = j + 1
				break
			}
		}
	}

	max := -1
	for _, m := range ans {
		if max < m {
			max = m
		}
	}
	fmt.Println(max)
}
0