結果

問題 No.1779 Magical Swap
ユーザー yudedako
提出日時 2022-02-02 13:28:25
言語 Scala(Beta)
(3.6.2)
結果
AC  
実行時間 1,376 ms / 2,000 ms
コード長 1,208 bytes
コンパイル時間 10,280 ms
コンパイル使用メモリ 261,440 KB
実行使用メモリ 81,560 KB
最終ジャッジ日時 2024-06-11 09:21:26
合計ジャッジ時間 32,986 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
import scala.io.StdIn.*
import scala.util.chaining.*
import scala.math.*

def calcPrime(upperLimit: Int): Array[Int] =
  val isPrime = Array.fill(upperLimit + 1){true}
  for p <- 2 to sqrt(upperLimit).toInt do
    if isPrime(p) then
      for j <- p * p to upperLimit by p do
        isPrime(j) = false
  (2 to upperLimit).filter(isPrime).toArray

extension (array: Array[Int])
  def upperBound(value: Int): Int =
    var min = 0
    var max = array.length
    while min < max do
      val mid = (min + max) >>> 1
      if array(mid) <= value then
        min = mid + 1
      else
        max = mid
    max

@main def main =
  val prime = calcPrime(100000)
  val testCase = readLine().toInt
  val isOk = Array.fill(testCase){false}
  for t <- 0 until testCase do
    val n = readLine().toInt
    val a = readLine().split(' ').map(_.toInt)
    val b = readLine().split(' ').map(_.toInt)
    if a.sorted sameElements b.sorted then
      isOk(t) = a(0) == b(0) && (prime.upperBound(n / 2) until prime.upperBound(n)).forall(i => a(prime(i) - 1) == b(prime(i) - 1))
  println(
    isOk.map(if _ then "Yes" else "No").mkString("\n")
  )
0