Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash

Introduction

This theme

AtCoder Regular Contest 066 C - Lining Up Difficulty: 927

This theme, iterative squares and hash Ruby This is a symmetrical problem, so if you assign it to a hash, you can expect it to be a values array like 2221222 for odd names and 22222222 for even names. In other cases, it can be determined that there is no arrangement.

Also, it's time for the previously posted * Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B* to help. Since it is the number of combinations of multiplication of 2, the iterative square method (function | method) created in the above post can be used as it is.

ruby.rb


n = gets.to_i
a = gets.split.map(&:to_i)
h = Hash.new(0)
a.each do |x|
  h[x] += 1
end
f = true
h.each do |k, v|
  if n.odd? && k == 0
    if v != 1
      f = false
      break
    end
  elsif v != 2
    f = false
    break
  end
end
MOD = 1_000_000_007
def mpow(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
puts (f ? mpow(2, n / 2, MOD) : 0)

mpow.rb


def mpow(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

I am using it as it is. Python

python.py


from collections import Counter

n = int(input())
a = list(map(int, input().split()))
h = Counter()
for x in a:
    h[x] += 1
f = True
for k, v in h.most_common():
    if n % 2 == 1 and k == 0:
        if v != 1:
            f = False
            break
    elif v != 2:
        f = False
        break
MOD = 1000000007
def mpow(n, p, mod):
    r = 1
    while p > 0:
        if p & 1 == 1:
            r = r * n % mod
        n = n * n % mod
        p >>= 1
    return r
print(mpow(2, n // 2, MOD) if f else 0)

hash.py


from collections import Counter

Declare from collections import Counter when using hashes.

mod.py


MOD = 1000000007
MOD = 1_000_000_007

In my ring Python 3.8.2 (default, Apr 16 2020, 16:12:56), you can write 1_000_000_007, but in the AtCoder environment it seems to be WA. Perl

perl.pl


chomp (my $n = <STDIN>);
chomp (my @a = split / /, <STDIN>);
my %h;
$h{$_}++ for @a;
my $f = 1;
for my $k (keys %h) {
  if ($n % 2 == 1 && $k == 0) {
    if ($h{$k} != 1) {
      $f = 0;
      last;
    }
  } elsif ($h{$k} != 2) {
    $f = 0;
    last;
  }
}
my $MOD = 1_000_000_007;
sub mpow {
  my ($n, $p) = @_;
  my $r = 1;
  while ($p > 0) {
    $r = $r * $n % $MOD if $p & 1;
    $n = $n * $n % $MOD;
    $p >>= 1;
  }
  $r;
}
print ($f ? mpow(2, int($n / 2)) : 0), "\n";

As it is ... Java

java.java


import java.math.BigInteger;
import java.util.*;

class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.next());
        HashMap<Integer, Integer> h = new HashMap<>();
        for (int i = 0; i < n; i++) {
            int a = Integer.parseInt(sc.next());
            if (h.containsKey(a)) {
                h.put(a, h.get(a) + 1);
            } else {
                h.put(a, 1);
            }
        }
        sc.close();
        boolean f = true;
        for (int k : h.keySet()) {
            if (n % 2 == 1 && k == 0) {
                if (h.get(k) != 1) {
                    f = false;
                    break;
                }
            } else if (h.get(k) != 2) {
                f = false;
                break;
            }
        }
        BigInteger bn = new BigInteger("2");
        BigInteger bm = new BigInteger("1000000007");
        BigInteger bp = new BigInteger(Integer.toString(n / 2));
        System.out.println((f ? bn.modPow(bp, bm) : 0));
    }
}
Ruby Python Perl Java
Code length 438 Byte 535 Byte 504 Byte 1100 Byte
Execution time 75 ms 125 ms 76 ms 435 ms
memory 12172 KB 16096 KB 20556 KB 49896 KB

Summary

Referenced site

Recommended Posts

Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
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 in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
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 ABC 165 D Floor function
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
AtCoder ARC080 D simulation solved in Ruby and Python
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solve with Ruby, Python and Java AtCoder ARC104 B Cumulative sum
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
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 ABC153 E Dynamic programming
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
AtCoder ABC168 A case expression solved in Ruby and Python
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Overlapping regular expressions in Python and Java
Differences between Ruby and Python in scope
How to write the correct shebang in Perl, Python and Ruby scripts
Implement FIR filters in Python and C
Differences in syntax between Python and Java
Write O_SYNC file in C and Python
Hash in Perl is a dictionary in Python
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
AtCoder Beginner Contest 170 B Problem "Crane and Turtle" Explanation (Python3, C ++, Java)
I compared the speed of regular expressions in Ruby, Python, and Perl (2013 version)
How to generate permutations in Python and C ++
I wrote a class in Python3 and Java
Solving with Ruby AtCoder ABC110 C String Manipulation
paiza POH ec-campaign (C # / Java / Python / Ruby) # paizahack_01
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
Benchmark for C, Java and Python with prime factorization
Difference between Ruby and Python in terms of variables
AtCoder Beginner Contest 176 C Problem "Step" Explanation (Python3, C ++, Java)
Solve Atcoder ABC176 (A, B, C, E) in Python
You walk in a spiral in a world of rising cross walls (regularity, Python / Ruby / C / C # / Java)
Daily AtCoder # 36 in Python
Daily AtCoder # 2 in Python
Daily AtCoder # 32 in Python
Daily AtCoder # 6 in Python
Daily AtCoder # 18 in Python
Daily AtCoder # 53 in Python
Daily AtCoder # 33 in Python
Daily AtCoder # 7 in Python
Daily AtCoder # 24 in Python
Daily AtCoder # 37 in Python