結果

問題 No.1022 Power Equation
ユーザー yakamotoyakamoto
提出日時 2020-04-11 15:39:32
言語 Kotlin
(1.9.23)
結果
AC  
実行時間 1,576 ms / 2,000 ms
コード長 3,971 bytes
コンパイル時間 20,698 ms
コンパイル使用メモリ 462,196 KB
実行使用メモリ 62,236 KB
最終ジャッジ日時 2023-10-19 06:55:55
合計ジャッジ時間 31,108 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 322 ms
54,272 KB
testcase_01 AC 352 ms
54,380 KB
testcase_02 AC 349 ms
54,392 KB
testcase_03 AC 421 ms
57,468 KB
testcase_04 AC 1,222 ms
61,808 KB
testcase_05 AC 1,488 ms
61,864 KB
testcase_06 AC 1,515 ms
61,812 KB
testcase_07 AC 1,576 ms
61,764 KB
testcase_08 AC 1,164 ms
62,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.util.*
import kotlin.math.abs
import kotlin.math.max
import kotlin.math.min
import kotlin.math.sqrt

val MOD = 1_000_000_007L

fun factorize(a: Int): MutableMap<Int, Int> {
  val res = mutableMapOf<Int, Int>()
  var x = a
  loop@while(x > 1) {
    var i = 2
    while(i * i <= x) {
      if (x % i == 0) {
        res.merge(i, 1, Int::plus)
        x /= i
        continue@loop
      }
      i++
    }
    res.merge(x, 1, Int::plus)
    x = 0
  }
  return res
}

tailrec fun gcd(a: Int, b: Int): Int {
  if (a < b) return gcd(b, a)
  if (b == 0) return a
  return gcd(b, a % b)
}

fun gcd(A: IntArray): Int {
  var g = A[0]
  for (i in 1 until A.size) {
    g = gcd(g, A[i])
  }
  return g
}

class Solver(stream: InputStream, private val out: java.io.PrintWriter) {
  fun solve() {
    for (i in 0 until ni()) {
      out.println(solve2())
    }
  }

  fun solve2(): Long {
    val N = ni()
    var ans = 0L

    // a = c = 1
    ans += N.toLong() * N

    // a = c = x
    ans += (N - 1).toLong() * N

    // x > rt(N) の場合はlogxN < 2 なので a = c = xのケースしか存在しない
    for (x in 2 .. sqrt(N.toDouble()).toInt() + 10) {
//    for (x in 2 .. N) {
      val f = factorize(x)
      val p = f.map { it.value }
      val g = gcd(p.toIntArray())
      // 2 * 3^2 * 5^3 こういうのは最小だけど、 2^2 * 3^2 みたいのは 2^1 * 3^1が最小になる
      if (g == 1) {
        debug{"x:$x"}
        var pow = 1
        var y = x.toLong()
        val A = mutableListOf<Int>()
        while(y <= N) {
          A += pow
          y *= x
          pow++
        }
        for (i in 0 until A.size - 1) {
          for (j in i + 1 until A.size) {
            val a = A[i]
            val c = A[j]
            val m = max(a, c) / gcd(a, c)
            val cnt = N / m
            debug{"$a:$c m:$m cnt:$cnt"}
            ans += 2 * cnt
          }
        }
      }
    }

    return ans
  }













































  private val isDebug = try {
    // なんか本番でエラーでる
    System.getenv("MY_DEBUG") != null
  } catch (t: Throwable) {
    false
  }

  private var tokenizer: StringTokenizer? = null
  private val reader = BufferedReader(InputStreamReader(stream), 32768)
  private fun next(): String {
    while (tokenizer == null || !tokenizer!!.hasMoreTokens()) {
      tokenizer = StringTokenizer(reader.readLine())
    }
    return tokenizer!!.nextToken()
  }

  private fun ni() = next().toInt()
  private fun nl() = next().toLong()
  private fun ns() = next()
  private fun na(n: Int, offset: Int = 0): IntArray {
    return map(n) { ni() + offset }
  }
  private fun nal(n: Int, offset: Int = 0): LongArray {
    val res = LongArray(n)
    for (i in 0 until n) {
      res[i] = nl() + offset
    }
    return res
  }

  private fun na2(n: Int, offset: Int = 0): Array<IntArray> {
    val a  = Array(2){IntArray(n)}
    for (i in 0 until n) {
      for (e in a) {
        e[i] = ni() + offset
      }
    }
    return a
  }

  private inline fun map(n: Int, f: (Int) -> Int): IntArray {
    val res = IntArray(n)
    for (i in 0 until n) {
      res[i] = f(i)
    }
    return res
  }

  private inline fun debug(msg: () -> String) {
    if (isDebug) System.err.println(msg())
  }

  private fun debug(a: LongArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: IntArray) {
    debug { a.joinToString(" ") }
  }

  private fun debug(a: BooleanArray) {
    debug { a.map { if (it) 1 else 0 }.joinToString("") }
  }

  private fun debugDim(A: Array<IntArray>) {
    if (isDebug) {
      for (a in A) {
        debug(a)
      }
    }
  }

  /**
   * 勝手にimport消されるのを防ぎたい
   */
  private fun hoge() {
    min(1, 2)
    max(1, 2)
    abs(-10)
  }
}

fun main() {
  val out = java.io.PrintWriter(System.out)
  Solver(System.`in`, out).solve()
  out.flush()
}
0