Editorial of MindCoding 2015 Test Round Redux

10 - A+B

A+B is one of the most complex problems on this website due to its large variety of solutions.

C

    1 #include<stdio.h>
    2 
    3 int main(void) {
    4 	int a, b;
    5 	scanf("%d%d", &a, &b);
    6 	printf("%d", a + b);
    7 	return 0;
    8 }

C++

    1 #include<iostream>
    2 
    3 int main(void){
    4 	int a, b;
    5 	std::cin >> a >> b;
    6 	std::cout << a + b;
    7 	return 0;
    8 }

D

    1 import std.stdio;
    2 
    3 void main(){
    4     long a, b;
    5     readf("%d %d", &a, &b);
    6     writeln(a+b);
    7 }

Go

    1 package main
    2 import "fmt"
    3 
    4 func main() {
    5 	var a, b int
    6 	fmt.Scanf("%d%d", &a, &b)
    7 	fmt.Print(a + b)
    8 }

GolfScript
    1 ~+

Haskell

    1 import Control.Applicative
    2 
    3 main :: IO ()
    4 main = do
    5         [ a, b ] <- map read . words <$> getLine
    6         print (a + b :: Int)

Java

In Java, the class must be named prog.
    1 public class prog {
    2 	public static void main(final String[] args) {
    3 		java.util.Scanner scan = new java.util.Scanner(System.in);
    4 		System.out.println(scan.nextInt() + scan.nextInt());
    5 	}
    6 }

Oberon

    1 MODULE prog;
    2 
    3 IMPORT In, Out;
    4 
    5 VAR a, b : INTEGER;
    6 
    7 BEGIN
    8   In.Int(a);
    9   In.Int(b);
   10   Out.Int(a + b, 0);
   11   Out.Ln
   12 END prog.

OCaml

    1 let sum a b = a + b
    2 
    3 let _=
    4     Printf.printf "%i\n" ((Scanf.scanf "%i %i" sum))

Pascal

    1 var a,b:integer;
    2 begin
    3 read(a,b);
    4 write(a+b);
    5 end.

Perl

    1 #!/usr/bin/perl
    2 use v5.14;
    3 use warnings;
    4 
    5 my ($x, $y) = split ' ', <>;
    6 say $x + $y;

PHP

    1 <?php
    2 fscanf(STDIN, "%d %d\n", $a, $b);
    3 echo $a+$b . "\n";
    4 ?>

Python

    1 #!/usr/bin/python
    2 print sum(map(int, raw_input().split()))

Python3

    1 #!/usr/bin/python3
    2 print(sum(map(int, input().split())))

Ruby

    1 p gets.split(' ').map(&:to_i).inject 0, :+

Rust

    1 use std::io;
    2 
    3 fn main() {
    4     let mut input = String::new();
    5 
    6     let _ = io::stdin().read_line(&mut input);
    7 
    8     let mut sum: i64 = 0;
    9     for x in input.trim().split(" ").collect::<Vec<&str>>() {
   10         sum += match x.parse::<i64>() {
   11             Ok(val) => val,
   12             _       => 0,
   13         };
   14     }
   15 
   16     println!("{}", sum);
   17 }

SBCL

    1 (format t "~d" (+ (read) (read)))

250 - Pharm

The easy solution (Marius Gavrilescu)

We will use a flood fill function that computes the area of a zone.

For the first kind of queries we will call the function for each cell and print the largest value returned by the function.

For the second kind of queries, if we change the color of a cell, we should change it to the color of one of its neighbours (otherwise it does not increase the area of a region). Thus, for each cell we can take each of its neighours, change the color of the cell to that of the neighbour and call our fill function.

    1 #include<stdio.h>
    2 const int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};
    3 
    4 char v[405][405];
    5 int us[405][405], filln = 1;
    6 
    7 int fill(int x, int y, int c){
    8     int ans = 1;
    9     us[x][y] = filln;
   10     for(int i = 0; i < 4; i++){
   11         int nx = x + dx[i], ny = y + dy[i];
   12         if(v[nx][ny] == c && us[nx][ny] < filln)
   13             ans += fill(nx, ny, c);
   14     }
   15     return ans;
   16 }
   17 
   18 int main(void){
   19     int t, m, n;
   20     scanf("%d%d%d ", &t, &m, &n);
   21     for(int i = 1; i <= m; i++)
   22         scanf("%s ", &v[i][1]);
   23 
   24     int best = 0, bestx, besty, cur;
   25     char bestc;
   26     for(int x = 1; x <= m; x++)
   27         for(int y = 1; y <= n; y++){
   28             cur = fill(x, y, v[x][y]);
   29             if(cur > best)
   30                 best = cur, bestx = x, besty = y, bestc = v[x][y];
   31             if(t == 1)
   32                 continue;
   33             for(int k = 0; k < 4; k++){
   34                 int nx = x + dx[k], ny = y + dy[k];
   35                 if(!v[nx][ny] || v[nx][ny] == v[x][y])
   36                     continue;
   37                 cur = fill(x, y, v[nx][ny]);
   38                 filln++;
   39                 if(cur > best)
   40                     best = cur, bestx = x, besty = y, bestc = v[nx][ny];
   41             }
   42         }
   43 
   44     if(t == 1)
   45         printf("%d\n", best);
   46     else
   47         printf("%d %d\n%c\n", bestx, besty, bestc);
   48     return 0;
   49 }

The fast solution (Gabriel Inelus)

A more efficient way of solving the type 2 queries is the following:

First of all we should define some helping structures:

  • Let a component be the maximal region of the same colour. A position (x,y) is a member of the component C if and only if Fill(x,y) denotes the full component C.
  • Let quant be an array which contains the area of a specific component.
  • Let used mean both that the coordinate (x,y) has been visited and also used[x][y] means to which component does (x,y) belong.

Then, we can use these structures as follows: quant[used[x][y]] is the area of the component that (x,y) belongs to.

To answer the type 2 query, we must scan the matrix and for each (x,y) we should test the relevant characters (only the ones of the 4 neighbours) and use the quant vector to calculate the new area.

The complexity of this algorithm is O(N*M*K) where K = 16 (for every position we choose 4 neighbours and 4 colours)

250 - Triples

Solution by Gabriel Inelus.

In number theory, Fermat's Last Theorem (sometimes called Fermat's conjecture) states that no three positive integers x, y, and z can satisfy the equation xn + yn = zn for any integer value of n greater than two. However, during the contest it was easy to observe this by using a brute force algorithm which tests all tuples {(x,y,z,j) | x^j + y^j = z^j , 0 <= x <= y <= z <= M}.

Hence, we can use a M3 algorithm to count all (x,y,z,2) tuples and than for each j ≥ 3, we have only tuples like (0,x,x,j)>. There are N-2 powers of j >= 3 and there are M+1 possible values of x for each power of j. Thus, we add (N-2)*(M+1) to the solution. The algorithm is repeated for each input set and the final complexity is M^3*T.

    1 #include<stdio.h>
    2 
    3 int main() {
    4     int n, m, i, j, k;
    5     while(scanf("%d%d", &m, &n) == 2) { // Read all imput sets
    6         int total = 0;
    7 
    8         for(i = 0; i <= m; ++i)
    9             for(j = i; j <= m; ++j)
   10                 for(k = j; k <= m; ++k)
   11                     if(i*i + j*j == k*k) // Test for x^2 + y^2 = z^2
   12                         total++;
   13 
   14         total += (m + 1) * (n - 2); // Add the remaining tuples
   15         printf("%d\n",total);
   16     }
   17     return 0;
   18 }
Questions?

Sponsors Gold