Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial

Introduction

This theme

AtCoder beginner Contets C - Reconciled? Difficulty: 647

This theme, factorial Ruby It is the factorial of the dog N and the factorial of the monkey M. If you make the factorial a function, you can use it for later contests.

ruby.rb


n, m = gets.split.map(&:to_i)
MOD = 1_000_000_007
def nPk(n, k)
  r = 1
  while k > 0
    r *= n
    r %= MOD
    n -= 1
    k -= 1
  end
  r
end
if (n - m).abs > 1
  puts 0
elsif n == m
  puts nPk(n, n) * nPk(m, m) * 2 % MOD
else
  puts nPk(n, n) * nPk(m, m) % MOD
end

nCk.rb


def nCk(n, k)
  r, j = 1, 1
  return 0 if k > n || k < 0
  k = n - k if n - k < k
  while j <= k
    r *= n
    n -= 1
    r /= j
    j += 1
  end
  r
end

This is also reusable ~~ copy ~~. Python

python.py


n, m = map(int, input().split())
MOD = 1000000007
def nPk(n, k):
    r = 1
    while k > 0:
        r *= n
        r %= MOD
        n -= 1
        k -= 1
    return r
if abs(n - m) > 1:
    print(0)
elif n == m:
    print(nPk(n, n) * nPk(m, m) * 2 % MOD)
else:
    print(nPk(n, n) * nPk(m, m) % MOD)

Perl

perl.pl


chomp (my ($n, $m) = split / /, <STDIN>);
my $MOD = 1_000_000_007;
sub nPk {
  my ($n, $k) = @_;
  my $r = 1;
  while ($k) {
    $r *= $n;
    $r %= $MOD;
    $n -= 1;
    $k -= 1;
  }
  $r;
}
if (abs($n - $m) > 1) {
  print "0\n";
} elsif ($n == $m) {
  print (nPk($n, $n) * nPk($m, $m) * 2 % $MOD), "\n";
} else {
  print (nPk($n, $n) * nPk($m, $m) % $MOD), "\n";
}

nCk.pl


sub nCk {
  my ($n, $k) = @_;
  my ($r, $j) = (1, 1);
  return 0 if $k > $n || $k < 0;
  $k = ($n - $k) if ($n - $k) < $k;
  while ($j <= $k) {
    $r *= $n--;
    $r /= $j++;
  }
  $r;
}

Java

java.java


import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        int m = Integer.parseInt(sc.next());
        sc.close();
        int MOD = 1_000_000_007;
        if (Math.abs(n - m) > 1) {
            System.out.println(0);
        } else if (n == m) {
            System.out.println((((nPk(n, n, MOD) * nPk(m, m, MOD)) % MOD) * 2) % MOD);
        } else {
            System.out.println((nPk(n, n, MOD) * nPk(m, m, MOD)) % MOD);
        }
    }

    static long nPk(int n, int k, int MOD) {
        long r = 1;
        while (k > 0) {
            r *= n;
            r %= MOD;
            n -= 1;
            k -= 1;
        }
        return r;
    }
}
Ruby Python Perl Java
Code length 287 Byte 314 Byte 386 Byte 794 Byte
Execution time 21 ms 64 ms 41 ms 111 ms
memory 1788 KB 3064 KB 384 KB 23764 KB

Summary

Recommended Posts

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 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 131 D Array Sorting
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
Solving with Ruby, Perl, Java and Python AtCoder diverta 2019 Programming Contest C String Manipulation
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
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
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, Python and networkx AtCoder ABC168 D Adjacency list
Solving with Ruby AtCoder ABC110 C String Manipulation
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solved AtCoder ABC 114 C-755 with Python3
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
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 and Python AtCoder AISING2020 D Iterative Squares
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
Solve AtCoder ABC166 with python
ABC163 C problem with python3
Benchmark for C, Java and Python with prime factorization
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
ABC188 C problem with python3
Solve AtCoder ABC 186 with Python
ABC187 C problem with python
[AtCoder explanation] Control ABC188 A, B, C problems with Python!
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
AtCoder ABC168 A case expression solved in Ruby and Python
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
Solve ABC163 A ~ C with Python
Scraping with Node, Ruby and Python
Solve ABC168 A ~ C with Python
Solve ABC162 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
[AtCoder commentary] Win the ABC165 C problem "Many Requirements" with Python!
List split and join strings with split and join (Perl / PowerShell / Java / Kotlin / Python)
[AtCoder] Solve ABC1 ~ 100 A problem with Python
Solve AtCoder ABC168 with python (A ~ D)
Easy web scraping with Python and Ruby
RaspberryPi L Chika with Python and C #
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
AtCoder ABC130 D Cumulative Sum Binary Search Solved by Ruby and Python
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
AtCoder Beginner Contest 170 B Problem "Crane and Turtle" Explanation (Python3, C ++, Java)