AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression

Introduction

This theme

AtCoder JSC2019 Qual B - Kleene Inversion Difficulty: 797

This theme, sum of arithmetic progression + inverse element Ruby I've never heard of inversions, but let's see what kind ofinversionsfor example 1 3 2.

K 3 2 1 total
132 1 1
132 132 4 1 5
132 132 132 7 4 1 12

One 1 3 2 is 1, two side by side 1 3 2 1 3 2 is 4 + 1, and three are lined up1 3 2 1 3 2 1 3 2is 7 You can see that it is the sum of + 4 + 1 and arithmetic progression. For the formula of the sum of arithmetic progressions, refer to * here *.

ruby.rb


n, k = gets.split.map(&:to_i)
a = gets.split.map(&:to_i)
b = a + a
c = 0
(a.size - 1).times do |i|
  (i + 1).upto(a.size - 1) do |j|
    c += 1 if a[i] > a[j]
  end
end
d = 0
(b.size - 1).times do |i|
  (i + 1).upto(b.size - 1) do |j|
    d += 1 if b[i] > b[j]
  end
end
d -= c * 2
def modpow(n, p, mod)
  r = 1
  while p > 0
    r = r * n % mod if p & 1 == 1;
    n = n * n % mod
    p >>= 1
  end
  r
end
def modinv(n, mod)
  modpow(n, mod - 2, mod)
end
MOD = 1_000_000_007
ans = (k - 1) * d % MOD
ans += 2 * c
ans %= MOD
ans *= k
ans %= MOD
ans *= modinv(2, MOD)
ans %= MOD
puts ans

cal.rb


c = 0
(a.size - 1).times do |i|
  (i + 1).upto(a.size - 1) do |j|
    c += 1 if a[i] > a[j]
  end
end
d = 0
(b.size - 1).times do |i|
  (i + 1).upto(b.size - 1) do |j|
    d += 1 if b[i] > b[j]
  end
end
d -= c * 2

The first term c and the tolerance d are calculated here.

wa.rb


ans = (k - 1) * d % MOD
ans += 2 * c
ans %= MOD
ans *= k
ans %= MOD
ans *= modinv(2, MOD)
ans %= MOD

It is the formula part of the sum of arithmetic progressions, but since there is division by 2, it is necessary to find the inverse element. Python

python.py


n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a + a
c = 0
for i in range(len(a)):
    for j in range(i + 1, len(a)):
        if a[i] > a[j]:
            c += 1
d = 0
for i in range(len(b)):
    for j in range(i + 1, len(b)):
        if b[i] > b[j]:
            d += 1
d -= c * 2
def modpow(n, p, mod):
    r = 1
    while p > 0:
        if p & 1 == 1:
            r = r * n % mod
        n = n * n % mod
        p >>= 1
    return r
def modinv(n, mod):
    return modpow(n, mod - 2, mod)
MOD = 10 ** 9 + 7
ans = (k - 1) * d % MOD
ans += 2 * c
ans %= MOD
ans *= k
ans %= MOD
ans *= modinv(2, MOD)
ans %= MOD
print(ans)

Even with PyPy3 (2.4.0), it could be used as it is.

Ruby Python PyPy3
Code length(Byte) 621 676 676
Execution time(ms) 948 1903 322
memory(KB) 1916 3188 41692

Summary

Referenced site

Recommended Posts

AtCoder JSC2019 Qual B to solve with Ruby and Python Inverse element of arithmetic progression
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solve AtCoder 167 with python
Comparison of CoffeeScript with JavaScript, Python and Ruby grammar
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solve AtCoder ABC166 with python
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
A collection of competitive pro techniques to solve with Python
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Return the image data with Flask of Python and draw it to the canvas element of HTML
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 and Python AtCoder ABC138 D Adjacency list
Solve AtCoder ABC 186 with Python
How to log in to AtCoder with Python and submit automatically
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Scraping with Node, Ruby and Python
Solve AtCoder Problems Recommendation with python (20200517-0523)
Coexistence of Python2 and 3 with CircleCI (1.0)
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
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, Perl, Java and Python AtCoder ABC 165 D Floor function
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Python code to train and test with Custom Vision of Cognitive Service
[AtCoder explanation] Control the A, B, C problems of ABC187 with Python!
Try to solve a set problem of high school math with Python
[AtCoder explanation] Control the A, B, C problems of ABC184 with Python!
Fractal to make and play with Python
I wanted to solve ABC160 with Python
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Encrypt with Ruby (Rails) and decrypt with Python
AtCoder Green tried to solve with Go
Easy web scraping with Python and Ruby
I wanted to solve ABC172 with Python
[AtCoder explanation] Control the A, B, (C), D problems of ABC165 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC183 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC181 with Python!
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
Correspondence summary of array operation of ruby and python
Try to solve the man-machine chart with Python