結果

問題 No.7 プライムナンバーゲーム
ユーザー yukirinyukirin
提出日時 2016-02-24 01:44:51
言語 Go
(1.22.1)
結果
AC  
実行時間 5 ms / 5,000 ms
コード長 1,059 bytes
コンパイル時間 14,624 ms
コンパイル使用メモリ 245,248 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-09 03:55:55
合計ジャッジ時間 15,551 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 4 ms
6,944 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 3 ms
6,948 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,948 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 2 ms
6,948 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 4 ms
6,944 KB
testcase_13 AC 4 ms
6,948 KB
testcase_14 AC 5 ms
6,944 KB
testcase_15 AC 5 ms
6,948 KB
testcase_16 AC 4 ms
6,948 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)
	n := nextInt()
	fs := eratosthenes(n)
	dp := make([]bool, n+1)

	for i := 2; i <= n; i++ {
		for _, f := range fs {
			if f > i {
				break
			}
			if !dp[i-f] && i-f > 1 {
				dp[i] = true
				break
			}
		}
	}
	if dp[n] {
		fmt.Println("Win")
		return
	}
	fmt.Println("Lose")
}

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