結果

問題 No.308 素数は通れません
ユーザー yosupotyosupot
提出日時 2015-12-01 22:59:52
言語 Go
(1.22.1)
結果
WA  
実行時間 -
コード長 2,330 bytes
コンパイル時間 12,321 ms
コンパイル使用メモリ 224,080 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2024-04-19 03:39:07
合計ジャッジ時間 15,739 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 1 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 1 ms
5,376 KB
testcase_26 AC 1 ms
5,376 KB
testcase_27 AC 1 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 1 ms
5,376 KB
testcase_30 AC 1 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 1 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 2 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
testcase_41 AC 2 ms
5,376 KB
testcase_42 AC 2 ms
5,376 KB
testcase_43 AC 3 ms
5,376 KB
testcase_44 AC 3 ms
5,376 KB
testcase_45 AC 3 ms
5,376 KB
testcase_46 AC 3 ms
5,376 KB
testcase_47 AC 3 ms
5,376 KB
testcase_48 AC 3 ms
5,376 KB
testcase_49 AC 4 ms
5,376 KB
testcase_50 AC 5 ms
5,376 KB
testcase_51 AC 5 ms
5,376 KB
testcase_52 AC 7 ms
5,760 KB
testcase_53 AC 7 ms
5,632 KB
testcase_54 AC 8 ms
5,760 KB
testcase_55 AC 9 ms
6,016 KB
testcase_56 AC 6 ms
5,376 KB
testcase_57 AC 5 ms
5,376 KB
testcase_58 AC 10 ms
5,376 KB
testcase_59 AC 12 ms
5,376 KB
testcase_60 AC 10 ms
5,376 KB
testcase_61 AC 6 ms
5,376 KB
testcase_62 AC 14 ms
5,376 KB
testcase_63 AC 19 ms
5,376 KB
testcase_64 AC 17 ms
5,376 KB
testcase_65 AC 17 ms
5,376 KB
testcase_66 AC 14 ms
5,376 KB
testcase_67 AC 15 ms
5,376 KB
testcase_68 AC 9 ms
5,376 KB
testcase_69 AC 43 ms
6,144 KB
testcase_70 AC 56 ms
6,272 KB
testcase_71 AC 59 ms
6,400 KB
testcase_72 AC 39 ms
6,016 KB
testcase_73 AC 27 ms
5,760 KB
testcase_74 AC 30 ms
6,144 KB
testcase_75 AC 12 ms
5,376 KB
testcase_76 WA -
testcase_77 AC 3 ms
5,376 KB
testcase_78 AC 14 ms
5,376 KB
testcase_79 AC 8 ms
5,376 KB
testcase_80 AC 15 ms
5,376 KB
testcase_81 AC 14 ms
5,376 KB
testcase_82 AC 18 ms
5,376 KB
testcase_83 AC 11 ms
5,376 KB
testcase_84 WA -
testcase_85 AC 11 ms
5,376 KB
testcase_86 WA -
testcase_87 WA -
testcase_88 AC 11 ms
5,376 KB
testcase_89 AC 19 ms
5,376 KB
testcase_90 WA -
testcase_91 AC 25 ms
5,376 KB
testcase_92 AC 56 ms
6,272 KB
testcase_93 AC 13 ms
5,376 KB
testcase_94 AC 28 ms
5,760 KB
testcase_95 AC 16 ms
5,376 KB
testcase_96 AC 14 ms
5,376 KB
testcase_97 AC 43 ms
6,272 KB
testcase_98 AC 31 ms
6,016 KB
testcase_99 AC 50 ms
5,376 KB
testcase_100 AC 42 ms
6,016 KB
testcase_101 WA -
testcase_102 AC 29 ms
5,888 KB
testcase_103 AC 37 ms
6,016 KB
testcase_104 AC 53 ms
6,272 KB
testcase_105 AC 18 ms
5,376 KB
testcase_106 AC 15 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"math/big"
	"os"
)

type UnionFind struct {
	n  int
	ig []int
	gi [][]int
}

func MakeUF(n int) *UnionFind {
	uf := new(UnionFind)
	uf.n = n
	uf.ig = make([]int, n)
	uf.gi = make([][]int, n)
	for i := 0; i < n; i++ {
		uf.ig[i] = i
		uf.gi[i] = []int{i}
	}
	return uf
}

func (uf *UnionFind) merge(a, b int) {
	a = uf.ig[a]
	b = uf.ig[b]
	if a == b {
		return
	}
	if len(uf.gi[a]) < len(uf.gi[b]) {
		tmp := a
		a = b
		b = tmp
	}
	for _, c := range uf.gi[b] {

		uf.ig[c] = a
	}
	uf.gi[a] = append(uf.gi[a], uf.gi[b]...)
}

func (uf *UnionFind) same(a, b int) bool {
	return uf.ig[a] == uf.ig[b]
}

var pr [10000]bool

func init() {
	for i := 2; i < 10000; i++ {
		pr[i] = true
	}
	for i := 2; i < 10000; i++ {
		if pr[i] == false {
			continue
		}
		for j := 2 * i; j < 10000; j += i {
			pr[j] = false
		}
	}
}

func solve1(n int) int {
	for i := 2; i <= n; i++ {
		uf := MakeUF(n + 1)
		for j := 1; j <= n; j++ {
			if j%i != 1 {
				if !pr[j] && !pr[j-1] {
					uf.merge(j, j-1)
				}
			}
			if i < j {
				if !pr[j] && !pr[j-i] {
					uf.merge(j, j-i)
				}
			}
		}
		if uf.same(1, n) {
			return i
		}
	}
	os.Exit(1)
	return -1
}

func solve2(n *big.Int) int {
	for i := 2; i < 1000; i++ {
		{
			uf := MakeUF(1001)
			for j := 1; j <= 1000; j++ {
				if j%i != 1 {
					if !pr[j] && !pr[j-1] {
						uf.merge(j, j-1)
					}
				}
				if i < j {
					if !pr[j] && !pr[j-i] {
						uf.merge(j, j-i)
					}
				}
			}
			if !uf.same(1, 1000/i*i) {
				continue
			}
		}
		{
			uf := MakeUF(1001)
			ii := big.NewInt(int64(i))
			bpr := make([]bool, 1001)
			for jj := 0; jj <= 1000; jj++ {
				j := n
				j.Sub(j, big.NewInt(int64(1000-jj)))
				bpr[jj] = j.ProbablyPrime(100)
			}
			for jj := 0; jj <= 1000; jj++ {
				j := n
				j.Sub(j, big.NewInt(int64(1000-jj)))
				if k := j; jj > 0 && k.Mod(k, ii).Cmp(big.NewInt(1)) != 0 {
					if !bpr[jj] && !bpr[jj-1] {
						uf.merge(jj, jj-1)
					}
				}
				if i <= jj {
					if !bpr[jj] && !bpr[jj-1] {
						uf.merge(jj, jj-i)
					}
				}
			}
			if nn := n; uf.same(1000-int(nn.Mod(nn, big.NewInt(int64(i))).Int64()), 1000) {
				return i
			}
		}
	}
	os.Exit(1)
	return -1
}

func main() {
	b := new(big.Int)
	fmt.Scan(b)
	if b.Cmp(big.NewInt(10000)) == -1 {
		fmt.Println(solve1(int(b.Int64())))
	} else {
		fmt.Println(solve2(b))
	}
}
0