結果
問題 | No.1845 Long Substrings |
ユーザー |
|
提出日時 | 2022-02-28 21:43:41 |
言語 | Scala(Beta) (3.6.2) |
結果 |
AC
|
実行時間 | 1,296 ms / 2,000 ms |
コード長 | 3,893 bytes |
コンパイル時間 | 16,098 ms |
コンパイル使用メモリ | 282,336 KB |
実行使用メモリ | 89,176 KB |
最終ジャッジ日時 | 2024-07-07 00:58:14 |
合計ジャッジ時間 | 54,178 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 34 |
ソースコード
import scala.collection.mutable.*import scala.io.StdIn.*import scala.util.chaining.*import scala.math.*import scala.reflect.ClassTagimport scala.util.*import scala.annotation.tailrecimport scala.collection.mutableobject ModInt:opaque type ModInt = Longinline def zero: ModInt = 0inline def one: ModInt = 1extension (value: Long)inline def toModInt(using inline mod: Int): ModInt = value % modinline def unsafeAsModInt: ModInt = valueextension (value: Int)inline def toModInt(using inline mod: Int): ModInt = value % modinline def unsafeAsModInt: ModInt = valuetrait Converter[T]:inline def convert(value: T)(using inline mod: Int): ModIntinline given Converter[Int] withoverride inline def convert(value: Int)(using inline mod: Int) = value.toModIntinline given Converter[Long] withoverride inline def convert(value: Long)(using inline mod: Int) = value.toModIntinline given Converter[ModInt] withoverride inline def convert(value: ModInt)(using inline mod: Int) = valueextension (modInt: ModInt)inline def toLong(using inline mod: Int): Long = (modInt + mod) % modinline def rawValue: Long = modIntinline def inverse(using inline mod: Int): ModInt =var x = modIntvar y = mod.toLongvar a = 1Lvar b = 0Lvar c = 0Lvar d = 1Lwhile y != 0 doval q = x / yval e = a - c * qval f = b - d * qa = cb = dc = ed = fx = yy = c * modInt + d * modrequire(x.abs == 1L)if x == 1 then a % mod else -a % modinline def powMod(exp: Long)(using inline mod: Int): ModInt =var result = 1Lvar base = modIntvar e = expwhile e > 0 doif (e & 1) == 1 thenresult = result * base % modbase = base * base % mode >>= 1resultinline def powMod(exp: Int)(using inline mod: Int): ModInt = powMod(exp.toLong)extension [L: Converter, R: Converter] (left: L)inline def +(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) + summon[Converter[R]].convert(right)).toModIntinline def -(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) - summon[Converter[R]].convert(right)).toModIntinline def *(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) * summon[Converter[R]].convert(right)).toModIntinline def /(right: R)(using inline mod: Int): ModInt = (summon[Converter[L]].convert(left) * summon[Converter[R]].convert(right).inverse).toModIntimport ModInt.*inline given mod: Int = 1000000007class Bit(size: Int):private val vec = Array.fill(size){zero}inline def get(position: Int)(using inline mod: Int): ModInt =var pos = positionvar result = zerowhile pos >= 0 doresult += vec(pos)pos -= ~pos & (pos + 1)resultinline def add(position: Int, value: ModInt)(using inline mod: Int) =var pos = positionwhile pos < size dovec(pos) += valuepos += ~pos & (pos + 1)inline def getAll(using inline mod: Int): ModInt = get(size - 1)@main def main =val n = readLine.toIntval length = readLine.split(' ').map(_.toInt)val str = readLineval prev = Array.fill(n + 1){-1}val lastPosition = Array.fill(26){0}for (c, i) <- str.zipWithIndex doprev(i + 1) = lastPosition(c - 'a')lastPosition(c - 'a') = i + 1val useAll = Array.fill(n + 1){zero}.tap(_(0) = one)val allPattern = Bit(n + 1).tap(_.add(0, one))for i <- 1 to n doval len = length(i - 1)val p = prev(i)val unique = allPattern.get(i) - allPattern.get(p)val expand = useAll(p)val newPattern = (unique + expand) * lenuseAll(i) = unique + expandallPattern.add(i, newPattern)println((allPattern.getAll - 1).toLong)