Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares

Introduction

This theme

This theme, iterative squares

0 1 1 0 1 Decimal notation
Original number $0*2^4$ $1*2^3$ $1*2^2$ $0*2^1$ $1*2^0$ 13
0th digit bit inversion $1*2^4$ 13+16=29
1st digit bit inversion $0*2^3$ 13- 8= 5
2nd digit bit inversion $0*2^2$ 13- 4= 9
3rd digit bit inversion $1*2^1$ 13+ 2=15
4th digit bit inversion $0*2^0$ 13- 1=12

If the given value is 01101, it can be decomposed as shown in the table above. In addition, the following holds as a distributive law, so $(a+b)\%m=(a\%m+b\%m)\%m$ The entire value can be obtained at high speed by inserting and removing the value of the bit-inverted digit. Ruby

ruby.rb


n = gets.to_i
x = gets.chomp
xs = x.to_i(2)
xc = x.count('1')
def mpow(n, p, mod)
  return 0 if mod.zero?
  r = 1
  while p > 0
    r = r * n % mod if p & 1 == 1
    n = n * n % mod
    p >>= 1
  end
  r
end
def popcount(u)
  return 0 if u.zero?
  a = u % u.to_s(2).count('1')
  1 + popcount(a)
end
xsp = mpow(xs, 1, xc + 1)
xsm = mpow(xs, 1, xc - 1)
n.times do |i|
  if x[i] == '0'
    y = xsp + mpow(2, n - i - 1, xc + 1)
    y = mpow(y, 1, xc + 1)
  elsif xc == 1
    puts '0'
    next
  else
    y = xsm - mpow(2, n - i - 1, xc - 1)
    y = mpow(y, 1, xc - 1)
  end
  puts popcount(y) + 1
end

Use the mpow method of the previously posted * Solve with Ruby, Perl, Java and Python AtCoder ATC 002 B * ~~ copy and paste ~~ ..

mpow.rb


def mpow(n, p, mod)
  return 0 if mod.zero?
  r = 1
  while p > 0
    r = r * n % mod if p & 1 == 1
    n = n * n % mod
    p >>= 1
  end
  r
end

However, this time the divisor may be 0, so it corresponds to that.

inout.rb


n.times do |i|
  if x[i] == '0'
    y = xsp + mpow(2, n - i - 1, xc + 1)
    y = mpow(y, 1, xc + 1)
  elsif xc == 1
    puts '0'
    next
  else
    y = xsm - mpow(2, n - i - 1, xc - 1)
    y = mpow(y, 1, xc - 1)
  end
  puts popcount(y) + 1
end

Here, the numerical value of the bit-inverted digit is put in and out. Again, we need to do something when the divisor is 0.

popcount.rb


def popcount(u)
  return 0 if u.zero?
  a = u % u.to_s(2).count('1')
  1 + popcount(a)
end

The second and subsequent pop counts are recursively calculated. If you make a note of this, it will be a little faster. Python

python.py


from sys import stdin

def mpow(n, p, mod):
    if mod == 0:
        return 0
    r = 1
    while p > 0:
        if p & 1 == 1:
            r = r * n % mod
        n = n * n % mod
        p >>= 1
    return r
def popcount(u):
    if u == 0:
        return 0
    a = u % bin(u).count('1')
    return 1 + popcount(a)
def main():
    input = stdin.readline
    n = int(input())
    x = '0b' + input()
    xs = int(x, 0)
    xc = x.count('1')
    xsp = mpow(xs, 1, xc + 1)
    xsm = mpow(xs, 1, xc - 1)
    for i in range(2, n + 2):
        if x[i] == '0':
            y = xsp + mpow(2, n - i + 1, xc + 1)
            y = mpow(y, 1, xc + 1)
        elif xc == 1:
            print(0)
            continue
        else:
            y = xsm - mpow(2, n - i + 1, xc - 1)
            y = mpow(y, 1, xc - 1)
        print(popcount(y) + 1)
main()
Ruby Python
Code length(Byte) 629 872
Execution time(ms) 625 1048
memory(KB) 15392 9636

Summary

Referenced site

Recommended Posts

Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
AtCoder ARC080 D simulation solved in Ruby and Python
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Scraping with Node, Ruby and Python
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solve AtCoder ABC168 with python (A ~ D)
Encrypt with Ruby (Rails) and decrypt with Python
Easy web scraping with Python and Ruby
AtCoder ABC130 D Cumulative Sum Binary Search Solved by Ruby and Python
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solving the Lorenz 96 model with Julia and Python
Solving with Ruby AtCoder ABC110 C String Manipulation
Solve AtCoder 167 with python
Ruby, Python and map
Python and Ruby split
Try to operate DB with Python and visualize with d3
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Version control of Node, Ruby and Python with anyenv
Programming with Python and Tkinter
Encryption and decryption with Python
AtCoder ABC155 Problem D Pairs Review Note 2 NumPy and Python
Solve AtCoder ABC166 with python
Light blue with AtCoder @Python
Python and hardware-Using RS232C with Python-
Python on Ruby and angry Ruby on Python
Solving Sudoku with Python (Incomplete)
Zundokokiyoshi with python / ruby / Lua
Solving Sudoku with Python (Part 2)
Ruby and Python syntax ~ branch ~
AtCoder ABC 182 Python (A ~ D)
python with pyenv and venv
Solve AtCoder ABC 186 with Python
AtCoder ABC168 A case expression solved in Ruby and Python
How to log in to AtCoder with Python and submit automatically
Works with Python and R
AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression
I compared the speed of Hash with Topaz, Ruby and Python
Create AtCoder Contest appointments on Google Calendar with Python and GAS
Communicate with FX-5204PS with Python and PyUSB