結果
問題 | No.3030 ミラー・ラビン素数判定法のテスト |
ユーザー | kemuniku |
提出日時 | 2023-11-06 03:18:45 |
言語 | Nim (2.0.2) |
結果 |
AC
|
実行時間 | 242 ms / 9,973 ms |
コード長 | 4,653 bytes |
コンパイル時間 | 4,353 ms |
コンパイル使用メモリ | 74,228 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-25 22:58:25 |
合計ジャッジ時間 | 5,587 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
5,248 KB |
testcase_01 | AC | 1 ms
5,376 KB |
testcase_02 | AC | 1 ms
5,376 KB |
testcase_03 | AC | 2 ms
5,376 KB |
testcase_04 | AC | 138 ms
5,376 KB |
testcase_05 | AC | 141 ms
5,376 KB |
testcase_06 | AC | 61 ms
5,376 KB |
testcase_07 | AC | 63 ms
5,376 KB |
testcase_08 | AC | 63 ms
5,376 KB |
testcase_09 | AC | 242 ms
5,376 KB |
ソースコード
import macros;macro ImportExpand(s:untyped):untyped = parseStmt($s[2]) ImportExpand "cplib/tmpl/sheep.nim" <=== "when not declared CPLIB_TMPL_SHEEP:\n const CPLIB_TMPL_SHEEP* = 1\n {.warning[UnusedImport]: off.}\n {.hint[XDeclaredButNotUsed]: off.}\n import algorithm\n import sequtils\n import tables\n import macros\n import math\n import sets\n import strutils\n import strformat\n import sugar\n import heapqueue\n import streams\n import deques\n import bitops\n #入力系\n proc scanf(formatstr: cstring){.header: \"<stdio.h>\", varargs.}\n proc getchar(): char {.importc: \"getchar_unlocked\", header: \"<stdio.h>\", discardable.}\n proc ii(): int {.inline.} = scanf(\"%lld\\n\", addr result)\n proc si(): string {.inline.} =\n result = \"\"\n var c: char\n while true:\n c = getchar()\n if c == ' ' or c == '\\n':\n break\n result &= c\n proc lii(long: int): seq[int] = newSeqWith(long, ii())\n #chmin,chmax\n template `max=`(x, y) = x = max(x, y)\n template `min=`(x, y) = x = min(x, y)\n #bit演算\n proc `%`(x: int, y: int): int = (((x mod y)+y) mod y)\n proc `//`(x: int, y: int): int = (((x) - (x%y)) div (y))\n proc `%=`(x: var int, y: int): void = x = x%y\n proc `//=`(x: var int, y: int): void = x = x//y\n proc `**`(x: int, y: int): int = x^y\n proc `^`(x: int, y: int): int = x xor y\n proc `|`(x: int, y: int): int = x or y\n proc `&`(x: int, y: int): int = x and y\n proc `>>`(x: int, y: int): int = x shr y\n proc `<<`(x: int, y: int): int = x shl y\n proc `^=`(x: var int, y: int): void = x = x ^ y\n proc `&=`(x: var int, y: int): void = x = x & y\n proc `|=`(x: var int, y: int): void = x = x | y\n proc `>>=`(x: var int, y: int): void = x = x >> y\n proc `<<=`(x: var int, y: int): void = x = x << y\n proc `[]`(x: int, n: int): bool = (x and (1 shl n)) != 0\n proc `@`(x: int): seq[int] =\n for i in 0..<64:\n if x[i]:\n result.add(i)\n #便利な変換\n proc `!`(x: char, a = '0'): int = int(x)-int(a)\n #定数\n const INF = int(3300300300300300491)\n #converter\n\n #range\n iterator range(start: int, ends: int, step: int): int =\n var i = start\n if step < 0:\n while i > ends:\n yield i\n i += step\n elif step > 0:\n while i < ends:\n yield i\n i += step\n iterator range(ends: int): int = (for i in 0..<ends: yield i)\n iterator range(start: int, ends: int): int = (for i in\n start..<ends: yield i)\n #powmod\n proc pow(a, n: int, m = INF): int =\n var rev: int = 1\n var a = a\n var n = n\n while n > 0:\n if n % 2 != 0:\n rev = (rev * a) mod m\n if n > 1:\n a = (a * a) mod m\n n >>= 1\n return rev\n\n #ここまでテンプレート v1.1.0\n discard\n" ImportExpand "cplib/math/isprime.nim" <=== "when not declared CPLIB_MATH_ISPRIME:\n const COMPETITIVE_MATH_ISPRIME* = 1\n #[ import cplib/math/powmod ]#\n when not declared CPLIB_MATH_POWMOD:\n const COMPETITIVE_MATH_POWMOD* = 1\n proc mul(a, b, m:int):int {.importcpp: \"(__int128)(#) * (#) % (#)\", nodecl.}\n proc powmod*(a, n, m: int): int =\n var\n rev = 1\n a = a\n n = n\n while n > 0:\n if n mod 2 != 0: rev = mul(rev,a,m)\n if n > 1: a = mul(a,a,m)\n n = n shr 1\n return rev\n discard\n import std/bitops\n\n proc isprime*(N:int):bool=\n let bases = [2,325,9375,28178,450775,9780504,1795265022]\n if N == 2:\n return true\n if N < 2 or (N and 1) == 0:\n return false\n let N1 = N-1\n var d = N1\n var s = 0\n while (d and 1) == 0:\n d = d shr 1\n s += 1\n for a in bases:\n var t:int\n if a mod N == 0:\n continue\n t = powmod(a,d,N)\n if t == 1 or t == N1:\n continue\n block test:\n for _ in 0..<(s-1):\n t = powmod(t,2,N)\n if t == N1:\n break test\n return false\n return true\n discard\n" var N = ii() for i in 0..<N: let x = ii() if isprime(x): echo x," ",1 else: echo x," ",0