結果

問題 No.1779 Magical Swap
コンテスト
ユーザー yudedako
提出日時 2022-02-02 13:28:25
言語 Scala(Beta)
(3.8.1)
コンパイル:
scalac _filename_
実行:
java -cp .:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala3-library_3/3.8.1/scala3-library_3-3.8.1.jar:/home/linuxbrew/.linuxbrew/Cellar/scala/3.8.1/libexec/maven2/org/scala-lang/scala-library/3.8.1/scala-library-3.8.1.jar _class_
結果
AC  
実行時間 964 ms / 2,000 ms
コード長 1,208 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 11,377 ms
コンパイル使用メモリ 277,092 KB
実行使用メモリ 79,980 KB
最終ジャッジ日時 2026-03-09 19:55:29
合計ジャッジ時間 27,345 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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