結果

問題 No.106 素数が嫌い!2
ユーザー fmhrfmhr
提出日時 2015-05-31 02:28:41
言語 Go
(1.22.1)
結果
AC  
実行時間 1,490 ms / 5,000 ms
コード長 742 bytes
コンパイル時間 10,498 ms
コンパイル使用メモリ 240,816 KB
実行使用メモリ 158,336 KB
最終ジャッジ日時 2024-04-19 02:16:56
合計ジャッジ時間 21,204 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 643 ms
80,768 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 640 ms
80,768 KB
testcase_05 AC 1,464 ms
158,336 KB
testcase_06 AC 1,480 ms
158,208 KB
testcase_07 AC 1,480 ms
158,336 KB
testcase_08 AC 51 ms
12,800 KB
testcase_09 AC 55 ms
12,800 KB
testcase_10 AC 1,490 ms
158,336 KB
testcase_11 AC 590 ms
80,256 KB
testcase_12 AC 1,442 ms
158,080 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"math"
)

func isPrime(n int, r []int) bool {
	x := int(math.Sqrt(float64(n)) + 1)
	for _, i := range r {
		if i > x {
			break
		}
		if n%i == 0 {
			return false
		}
	}
	return true
}

func getPrime(n int) ([]int, map[int]int) {
	p := make([]bool, n+1) // false: 素数の可能性有 true: notPrime
	r := make([]int, 0)
	m := map[int]int{}
	for i := 2; i < n+1; i++ {
		if p[i] {
			continue
		}
		if !isPrime(i, r) {
			continue
		}
		r = append(r, i)
		for j := i; j <= n; j += i {
			p[j] = true
			m[j]++
		}
	}
	return r, m
}

func main() {
	var N, K int
	fmt.Scan(&N, &K)
	_, m := getPrime(N)
	ans := 0
	//fmt.Println(m)
	for i := 2; i < N+1; i++ {
		if m[i] >= K {
			ans++
		}
	}
	fmt.Println(ans)
}
0