結果

問題 No.2858 Make a Palindrome
ユーザー kemunikukemuniku
提出日時 2024-08-25 15:30:46
言語 Nim
(2.0.2)
結果
AC  
実行時間 2,717 ms / 3,000 ms
コード長 10,194 bytes
コンパイル時間 5,507 ms
コンパイル使用メモリ 93,420 KB
実行使用メモリ 21,424 KB
最終ジャッジ日時 2024-08-25 15:31:59
合計ジャッジ時間 72,090 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,717 ms
11,052 KB
testcase_01 AC 230 ms
11,000 KB
testcase_02 AC 229 ms
10,780 KB
testcase_03 AC 250 ms
10,752 KB
testcase_04 AC 227 ms
10,836 KB
testcase_05 AC 204 ms
10,844 KB
testcase_06 AC 175 ms
10,832 KB
testcase_07 AC 176 ms
10,820 KB
testcase_08 AC 181 ms
10,636 KB
testcase_09 AC 176 ms
10,876 KB
testcase_10 AC 2,118 ms
10,636 KB
testcase_11 AC 2,099 ms
10,772 KB
testcase_12 AC 2,160 ms
10,808 KB
testcase_13 AC 2,149 ms
10,780 KB
testcase_14 AC 2,098 ms
10,832 KB
testcase_15 AC 2,047 ms
11,736 KB
testcase_16 AC 2,013 ms
11,572 KB
testcase_17 AC 2,014 ms
11,584 KB
testcase_18 AC 2,068 ms
11,664 KB
testcase_19 AC 1,996 ms
11,580 KB
testcase_20 AC 2,214 ms
18,592 KB
testcase_21 AC 1,778 ms
18,632 KB
testcase_22 AC 781 ms
14,940 KB
testcase_23 AC 1,899 ms
18,560 KB
testcase_24 AC 1,677 ms
17,452 KB
testcase_25 AC 1,665 ms
17,872 KB
testcase_26 AC 1,465 ms
17,772 KB
testcase_27 AC 2,080 ms
19,396 KB
testcase_28 AC 1,190 ms
17,328 KB
testcase_29 AC 2,017 ms
18,916 KB
testcase_30 AC 2,072 ms
19,456 KB
testcase_31 AC 2,017 ms
19,576 KB
testcase_32 AC 1,210 ms
19,668 KB
testcase_33 AC 2,311 ms
21,424 KB
testcase_34 AC 1,925 ms
19,460 KB
testcase_35 AC 2,061 ms
19,492 KB
testcase_36 AC 2,525 ms
19,812 KB
testcase_37 AC 2,014 ms
19,504 KB
testcase_38 AC 2,626 ms
19,760 KB
testcase_39 AC 2,212 ms
19,396 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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    import std/lenientops\n    import options\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 lii(N: int): seq[int] {.inline.} = newSeqWith(N, ii())\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    #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 =\n        result = x mod y\n        if y > 0 and result < 0: result += y\n        if y < 0 and result > 0: result += y\n    proc `//`*(x: int, y: int): int{.inline.} =\n        result = x div y\n        if y > 0 and result * y > x: result -= 1\n        if y < 0 and result * y < x: result -= 1\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: var int, y: int): void = x = 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: int): int = not x\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    #便利な変換\n    proc `!`(x: char, a = '0'): int = int(x)-int(a)\n    #定数\n    #[ include cplib/utils/constants ]#\n    when not declared CPLIB_UTILS_CONSTANTS:\n        const CPLIB_UTILS_CONSTANTS* = 1\n        const INF32*: int32 = 100100111.int32\n        const INF64*: int = int(3300300300300300491)\n    const INF = INF64\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\n    #joinが非stringでめちゃくちゃ遅いやつのパッチ\n    proc join*[T: not string](a: openArray[T], sep: string = \"\"): string = a.mapit($it).join(sep)\n"
ImportExpand "cplib/str/hash_string.nim" <=== "import random\ntype HashString* =object\n    hash* :uint\n    size:int\nconst MASK30 = (1u shl 30) - 1\nconst MASK31 = (1u shl 31) - 1\nconst RH_MOD = (1u shl 61) - 1\nconst POW_CALC = 500000\n\nrandomize()\n\nproc calc_mod(x: uint): uint =\n    result = (x shr 61) + (x and RH_MOD)\n    if result >= RH_MOD:\n        result -= RH_MOD\n\nproc mul(a, b: uint): uint =\n    let\n        a_upper = a shr 31\n        a_lower = a and MASK31\n        b_upper = b shr 31\n        b_lower = b and MASK31\n        mid = a_lower * b_upper + a_upper * b_lower\n        mid_upper = mid shr 30\n        mid_lower = mid and MASK30\n    result = a_upper * b_upper * 2 + mid_upper + (mid_lower shl 31) + a_lower * b_lower\n\n\nproc inner_pow(a:uint, n: int): uint =\n    var a = a\n    var n = n\n    result = 1\n    while n > 0:\n        if (n and 1) != 0:\n            result = mul(result, a).calc_mod\n        a = mul(a, a).calc_mod\n        n = n shr 1\n\nlet hashstring_base:uint = rand(129u..(1u shl 30))\nlet inv_hashstring_base:uint = inner_pow(hashstring_base,int(RH_MOD)-2)\nvar pows : seq[uint] = newseq[uint](POW_CALC+1)\nvar invpows : seq[uint] = newseq[uint](POW_CALC+1)\npows[0] = 1\ninvpows[0] = 1\nfor i in 1..POW_CALC:\n    pows[i] = (mul(pows[i-1],hashstring_base).calc_mod)\n    invpows[i] = (mul(invpows[i-1],inv_hashstring_base).calc_mod)\n\nproc base_pow(n:int):uint=\n    if n >= len(pows):\n        return inner_pow(hashstring_base,n)\n    else:\n        return pows[n]\n\nproc tohash*(S:string):HashString=\n    var hash = 0u\n    var tmp = 1u\n    for i in countdown(len(S)-1,0,1):\n        hash = (hash+mul(uint(int(S[i])),tmp)).calc_mod\n        tmp = mul(tmp,hashstring_base).calc_mod \n    result = HashString(hash:hash,size:len(S))\n\nproc tohash*(S:char):HashString=\n    result = HashString(hash:uint(int(S)),size:1)\n\nproc `&`*(L,R:HashString):HashString=\n    result = HashString(hash:(mul(L.hash,base_pow(R.size)).calc_mod+R.hash).calc_mod,size:L.size+R.size)\n\nproc `==`*(L,R:HashString):bool=\n    return (L.size == R.size) and (L.hash == R.hash)\n\nproc len*(H:HashString):int=int(H.size)\n\nproc `*`*(H:HashString,x:int):HashString=\n    result = \"\".toHash()\n    var\n        x = x\n        tmp = H\n    while x > 0:\n        if x mod 2 != 0: result = result&tmp\n        if x > 1: tmp = tmp & tmp\n        x = x shr 1\n\nproc removePrefix*(H,suffix:HashString):HashString=\n    var hash = (H.hash + (RH_MOD - mul(suffix.hash,base_pow(len(H)-len(suffix))).calc_mod)).calc_mod\n    var l = len(H)-len(suffix)\n    return HashString(hash:hash,size:l)\n\ntype RollingHashBase = ref object\n    S : string\n    prefixs : seq[uint]\n    size : int \n\ntype RollingHash* = object\n    R : RollingHashBase\n    l : int\n    r : int\n\nproc len*(S:RollingHashBase):int=\n    return int(S.size)\n\nproc len*(S:RollingHash):int=\n    return int(S.r-S.l)\n\nproc get_substring(R:RollingHashBase,l,r:int):RollingHash=\n    # 半開区間とする。\n    # 空文字列用にr=0も許容していることに注意。\n    # 空文字列はl=0,r=0のみ許容している。\n    assert l in 0..<R.size and r in 0..R.size and (l < r or (l == 0 and r == 0))\n    result.R = R\n    result.l = l\n    result.r = r\n\nproc `[]`*(R:RollingHashBase,slice:HSlice[int,int]):RollingHash=\n    assert slice.a >= 0 and slice.b >= 0\n    return R.get_substring(slice.a,slice.b+1)\n\n\nproc `[]`*(S:RollingHash,slice:HSlice[int,int]):RollingHash=\n    if len(slice) == 0:\n        return S.R.get_substring(0,0)\n    assert slice.a in 0..<len(S) and slice.b in 0..<len(S)\n    return S.R.get_substring(S.l+slice.a,S.l+slice.b+1)\n\nproc gethash(S:RollingHash,slice:HSlice[int,int]):uint=\n    return (S.R.prefixs[(S.l+slice.b+1)] + (RH_MOD - mul(S.R.prefixs[S.l+slice.a],base_pow(((S.l+slice.b+1)-(S.l+slice.a)))).calc_mod)).calc_mod\n\n\nproc `[]`*(S:RollingHash,idx:int):char=\n    return S.R.S[idx+int(S.l)]\n\nproc initRollingHash*(S:string):RollingHash=\n    var rolling = RollingHashBase()\n    rolling.S = S\n    rolling.prefixs = newSeq[uint](len(S)+1)\n    rolling.prefixs[0] = 0\n    for i in 1..len(S):\n        rolling.prefixs[i] = (mul(rolling.prefixs[i-1],hashstring_base) + uint(int(S[i-1]))).calc_mod()\n    rolling.size = (len(S))\n    return rolling[0..<len(S)]\n\n\n\nconverter toHashString*(self:RollingHash):HashString=\n    return HashString(hash:(self.R.prefixs[self.r] + (RH_MOD - mul(self.R.prefixs[self.l],base_pow(self.r-self.l)).calc_mod)).calc_mod,size:self.r-self.l)\n\nproc`$`*(S:RollingHash):string=\n    return S.R.S[S.l..<S.r]\n\nproc `==`*(S,T:RollingHash):bool=\n    return len(S) == len(T) and (S.R.prefixs[S.r] + (RH_MOD - mul(S.R.prefixs[S.l],base_pow(S.r-S.l)).calc_mod)).calc_mod == \n           (T.R.prefixs[T.r] + (RH_MOD - mul(T.R.prefixs[T.l],base_pow(T.r-T.l)).calc_mod)).calc_mod\n\nproc LCP*(S,T:RollingHash):int=\n    var ok = 0\n    var ng = min(len(S),len(T))+1\n    while ng-ok > 1:\n        var mid = (ok + ng) div 2\n        if S.gethash(0..<mid) == T.gethash(0..<mid): ok = mid\n        else: ng = mid\n    return ok\n\nproc cmp*(S,T:RollingHash):int=\n    var S = S\n    var T = T\n    var flg = 1\n    if len(S) > len(T):\n        swap(S,T)\n        flg *= -1\n    var lcp = LCP(S,T)\n    if len(S) == lcp:\n        if len(S) == len(T):\n            return 0\n        else:\n            return -1*flg\n    else:\n        if S[lcp] < T[lcp]:\n            return -1*flg\n        else:\n            return flg\n\nproc `<`*(S,T:RollingHash):bool=\n    return cmp(S,T) < 0\n"

var T = ii()

for _ in range(T):
    var N,M = ii()
    var S = si()
    #Sを無限個連結した文字列について、
    #1.あるiからM文字連続のものが回文かを判定する。
    #2.あるiからM+1文字連続のものが回文かをはんていする。
    var ans = INF
    var HS = S.initRollingHash()
    var HRS = (reversed(S).join("")).initRollingHash()

    proc solve(i,x:int):bool=
        if (i+x) < N:
            return HS[i..<(i+x)] == HRS[(N-1-(i+x-1))..(N-1-i)]
        var tmp1 = HS[i..<N].toHashString()
        var tmp2 = HRS[0..(N-1-i)].toHashString()
        #echo (i,x)
       # echo (tmp1,tmp2,HS[i..<N],HS[0..(N-1-i)])
        tmp1 = tmp1 & (HS * ((x-len(i..<N)) // N))
        tmp2 = (HRS * ((x-len(i..<N)) // N)) & tmp2
        #echo (tmp1,tmp2)
        var nokori = x-(((x-len(i..<N)) // N)*N + len(i..<N))
        #echo nokori
        tmp1 = tmp1 & HS[0..<nokori]
        tmp2 = HRS[(N-nokori)..(N-1)] & tmp2
        #echo (tmp1,tmp2)
        return tmp1 == tmp2


    for i in range(N):
        #echo (i,solve(i,M),solve(i,M+1))
        if solve(i,M):
            ans.min = (M-(N-i)+(N-1))//N + 1
        if solve(i,M+1):
            ans.min = (M+1-(N-i)+(N-1))//N + 1

    if ans == INF:
        echo -1
    else:
        echo ans
0