結果

問題 No.12 限定された素数
ユーザー warashiwarashi
提出日時 2015-10-30 14:05:36
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 1,260 bytes
コンパイル時間 10,614 ms
コンパイル使用メモリ 220,800 KB
実行使用メモリ 28,384 KB
最終ジャッジ日時 2024-04-19 03:28:39
合計ジャッジ時間 12,807 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
26,368 KB
testcase_01 AC 40 ms
26,368 KB
testcase_02 WA -
testcase_03 AC 32 ms
26,240 KB
testcase_04 AC 39 ms
26,240 KB
testcase_05 AC 42 ms
26,368 KB
testcase_06 AC 49 ms
26,368 KB
testcase_07 RE -
testcase_08 WA -
testcase_09 AC 52 ms
26,240 KB
testcase_10 WA -
testcase_11 RE -
testcase_12 RE -
testcase_13 AC 45 ms
26,368 KB
testcase_14 WA -
testcase_15 AC 45 ms
26,368 KB
testcase_16 RE -
testcase_17 WA -
testcase_18 AC 40 ms
26,368 KB
testcase_19 AC 40 ms
26,368 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 39 ms
26,368 KB
testcase_23 AC 39 ms
26,240 KB
testcase_24 AC 39 ms
26,368 KB
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"math"
)

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

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 ok(A []bool, n int) bool {
	count := make([]int, 10)
	for n > 0 {
		count[n%10]++
		n /= 10
	}
	for i := 0; i < 10; i++ {
		if !A[i] && count[i] > 0 {
			return false
		}
	}
	return true
}

func main() {
	var N, tmp, ans int
	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++ {
		if !ok(A, PRIME[i]) {
			continue
		}
		var min, max int
		if i == 0 {
			min = 1
		} else {
			min = PRIME[i-1] + 1
		}
		for j := i; j < len(PRIME); j++ {
			if !ok(A, PRIME[j]) {
				break
			}
			if j+1 > len(PRIME) {
				max = 5000000
			} else {
				max = PRIME[j+1] - 1
			}
		}
		if ans < max-min {
			ans = max - min
		}
	}
	fmt.Println(ans)
}
0