3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Ruby と Perl と Java と Python で解く AtCoder ABC 065 C 階乗

Posted at

はじめに

AtCoder Problems の Recommendation を利用して、過去の問題を解いています。
AtCoder さん、AtCoder Problems さん、ありがとうございます。

今回のお題

AtCoder beginner Contets C - Reconciled?
Difficulty: 647

今回のテーマ、階乗

Ruby

N匹の階乗と猿M匹の階乗の掛け算になります。
階乗を関数化しておけば後々のコンテストに使用できます。

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

こちらもコピペ再利用可能です。

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;
    }
}

Java は気を許すと、整数のオーバーフローを食らいます。

Ruby Python Perl Java
コード長 287 Byte 314 Byte 386 Byte 794 Byte
実行時間 21 ms 64 ms 41 ms 111 ms
メモリ 1788 KB 3064 KB 384 KB 23764 KB

まとめ

  • ABC 065 C を解いた
  • Ruby に詳しくなった
  • Python に詳しくなった
  • Perl に詳しくなった
  • Java に詳しくなった
3
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?