結果

問題 No.64 XORフィボナッチ数列
ユーザー irisAshirisAsh
提出日時 2017-08-13 22:53:39
言語 JavaScript
(node v21.7.1)
結果
AC  
実行時間 69 ms / 5,000 ms
コード長 2,738 bytes
コンパイル時間 43 ms
コンパイル使用メモリ 5,376 KB
実行使用メモリ 39,424 KB
最終ジャッジ日時 2024-04-21 01:52:08
合計ジャッジ時間 1,591 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
39,168 KB
testcase_01 AC 69 ms
39,168 KB
testcase_02 AC 67 ms
39,168 KB
testcase_03 AC 66 ms
39,168 KB
testcase_04 AC 63 ms
39,424 KB
testcase_05 AC 65 ms
39,296 KB
testcase_06 AC 63 ms
39,168 KB
testcase_07 AC 63 ms
39,168 KB
testcase_08 AC 66 ms
39,424 KB
testcase_09 AC 64 ms
39,168 KB
testcase_10 AC 65 ms
39,424 KB
testcase_11 AC 66 ms
39,424 KB
testcase_12 AC 63 ms
39,296 KB
testcase_13 AC 63 ms
39,296 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

const xor = (a, b) => {
  let x = a, y = b
  if (x.length < y.length) {
    x = b
    y = a
  }
  let u = ''
  if (x.length != y.length) {
    let d = x.length - y.length
    u = x.substr(0, d)
    x = x.substr(d)
  }
  let xl = x.split('')
  let yl = y.split('')
  let r = xl.map((e, i) => e ^ yl[i]).join('')
  return u + r
}
const plus = (a, b) => {
  let r = [], u = 0
  let x = a.concat().reverse(), y = b.concat().reverse()
  if (x.length < y.length) {
    let t = x
    x = y
    y = t
  }
  x.forEach((e,i) => {
    let k = e + u + (i < y.length ? y[i] : 0)
    u = Math.floor(k / 100000000)
    k %= 100000000
    r.unshift(k)
  })
  if (u != 0) r.unshift(u)
  return r
}
const minus = (a, b) => {
  let r = [], u = 0
  let x = a.concat().reverse(), y = b.concat().reverse()
  if (x.length < y.length) {
    let t = x
    x = y
    y = t
  }
  if (x.length == y.lenght) {
    for (let c = length - 1; c <= 0 ; c--) {
      if (x[c] > y[c]) break
      else if (x[c] < y[c]) {
        let t = x
        x = y
        y = t
        break
      }
    }
  }
  x.forEach((e,i) => {
    let k = e - u - (i < y.length ? y[i] : 0)
    if (k >= 0) u = 0
    else {
      u = 1
      k += 100000000
    }
    r.unshift(k)
  })
  let d = 0
  for (let c = 0; c < r.length; c++) {
    if (r[c] != 0) break
    d++
  }
  for (; d > 0; d--) r.shift()
  return r
}
const binaryToDecimal = (a) => {
  let r = [0], p = [1]
  let b = a.split('').reverse()
  b.forEach((e) => {
    if (e != 0) r = plus(r, p)
    p = plus(p, p)
  })
  return r
}
const decimalToBinary = (a) => {
  let t = a.split('').reverse()
  let x = []
  t.forEach((e,i) => {
    let k = Math.floor(i / 8)
    x[k] = e + (x[k] === void 0 ? '' : x[k])
  })
  x.reverse()
  let b = [1]
  let bl = []
  while (true) {
    bl.push(b)
    if (ge(b, x)) break
    b = plus(b,b)
  }
  let r = ''
  for (let c = bl.length-2; c >= 0; c--) {
    if (ge(x, bl[c])) {
      x = minus(x, bl[c])
      r += '1'
    }
    else r += 0
  }
  return r
}
const ge = (a, b) => {
  if (a.length > b.length) return true
  if (a.length < b.length) return false
  for (let c = 0; c < a.length; c++) {
    if (a[c] < b[c]) return false
    if (a[c] > b[c]) return true
  }
  return true
}

let i = require('fs').readFileSync('/dev/stdin', 'utf8').split('\n')[0].split(' ')
let c = i[2] % 3
let o = ''
if (c == 0) o = i[0]
else if (c == 1) o = i[1]
else {
  if (i[0] > 10000000000000000 || i[1] > 10000000000000000) {
    let aa = decimalToBinary(i[0])
    let bb = decimalToBinary(i[1])
    let cc = xor(aa, bb)
    let rr = binaryToDecimal(cc)
    rr.forEach((e,i) => {
      o += ('00000000' + e).slice(-8)
      if (i == 0) o = parseInt(o, 10) + ''
    })
  }
  else o = i[0] ^ i[1]
}
console.log(o)
0