Matrix A, consisting of N rows and N columns of non-negative integers, is given. Rows are numbered from 0 to N−1 (from top to bottom). Columns are numbered from 0 to N−1 (from left to right). We would like to find a path that starts at the upper-left corner (0, 0) and, moving only right and down, finishes at the bottom-right corner (N−1, N−1). Then, all the numbers on this path will be multiplied together.
Find a path such that the product of all the numbers on the path contain the minimal number of trailing zeros. We assume that 0 has 1 trailing zero.
Write a function:
class Solution { public int solution(int[][] A); }
that, given matrix A, returns the minimal number of trailing zeros.
Examples:
1. Given matrix A below:
the function should return 1. The optimal path is: (0,0) → (0,1) → (0,2) → (1,2) → (2,2) → (2,3) → (3,3). The product of numbers 2, 10, 1, 4, 2, 1, 1 is 160, which has one trailing zero. There is no path that yields a product with no trailing zeros.
2. Given matrix A below:
the function should return 2. One of the optimal paths is: (0,0) → (1,0) → (1,1) → (1,2) → (2,2) → (3,2) → (3,3). The product of numbers 10, 1, 1, 1, 10, 1, 1 is 100, which has two trailing zeros. There is no path that yields a product with fewer than two trailing zeros.
3. Given matrix A below:
the function should return 1. One of the optimal paths is: (0,0) → (0,1) → (1,1) → (1,2) → (2,2). The product of numbers 10, 10, 0, 10, 10 is 0, which has one trailing zero. There is no path that yields a product with no trailing zeros.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..500];
- each element of matrix A is an integer within the range [0..1,000,000,000].
using System;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class Solution {
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(10000000);
if(1 < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(1);
if(1 < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(10000000);
if(10000000 < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(10000000);
if(10000000 < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
// write your code in C# 6.0 with .NET 4.5 (Mono)
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(122,32): error CS1519: Unexpected symbol `=' in class, struct, or interface member declaration
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.)
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.)
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private static CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private static CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<10000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private static CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private static CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private static CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1) return minimum.Distance;
}
return 0;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private static CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero)
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private static CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private static CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest, int maxRow, int maxCol){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private static CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private static CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down");
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down");
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right");
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down");
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right");
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, 1, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down");
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right");
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
newNodeZero.SetIsNodeZero();
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down");
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right");
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public bool IsNodeZero {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
IsNodeZero = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down");
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right");
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
public void SetIsNodeZero(){
IsNodeZero = true;
}
public void ForceDistance(int distance){
Distance = distance;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down");
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right");
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(node.IsNodeZero){
existsSomeZero = true;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down");
if(!down.Picked){
down.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right");
if(!right.Picked){
right.ForceDistance(BIG_DISTANCE);
if(BIG_DISTANCE < minDistance){
nodeMinDistance = right;
}
}
}
}
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(1000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(1000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
Console.WriteLine("(" + x + "," + y + ")");
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0stdout:
(0,0) (0,0) (0,1) (0,0) (0,1) (0,2) (0,0) (0,1) (0,2) (0,3) (0,0) (0,1) (0,2) (0,3) (1,3) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (3,1) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (3,1) (1,0) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (3,1) (1,0) (2,0) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (3,1) (1,0) (2,0) (3,0)
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0stdout:
(0,0) (0,0) (0,1) (0,0) (0,1) (0,2) (0,0) (0,1) (0,2) (0,3) (0,0) (0,1) (0,2) (0,3) (1,3) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (3,1) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (3,1) (1,0) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (3,1) (1,0) (2,0) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3) (3,3) (1,2) (2,2) (3,2) (1,1) (2,1) (3,1) (1,0) (2,0) (3,0)
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0stdout:
(0,0) (0,0) (0,1) (0,0) (0,1) (0,2) (0,0) (0,1) (0,2) (1,2) (0,0) (0,1) (0,2) (1,2) (2,2) (0,0) (0,1) (0,2) (1,2) (2,2) (1,1) (0,0) (0,1) (0,2) (1,2) (2,2) (1,1) (2,1) (0,0) (0,1) (0,2) (1,2) (2,2) (1,1) (2,1) (1,0) (0,0) (0,1) (0,2) (1,2) (2,2) (1,1) (2,1) (1,0) (2,0)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
Console.WriteLine("(" + x + "," + y + ")");
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
(0,0) (0,0) (0,1) (0,0) (0,1) (0,2) (0,0) (0,1) (0,2) (0,3) (0,0) (0,1) (0,2) (0,3) (1,3) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3)
(0,0) (0,0) (0,1) (0,0) (0,1) (0,2) (0,0) (0,1) (0,2) (0,3) (0,0) (0,1) (0,2) (0,3) (1,3) (0,0) (0,1) (0,2) (0,3) (1,3) (2,3)
(0,0) (0,0) (0,1) (0,0) (0,1) (0,2) (0,0) (0,1) (0,2) (1,2)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
Console.WriteLine("(" + x + "," + y + "):");
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
(0,0): True (0,0): True (0,1): True (0,0): True (0,1): True (0,2): True (0,0): True (0,1): True (0,2): True (0,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (2,3): True
(0,0): True (0,0): True (0,1): True (0,0): True (0,1): True (0,2): True (0,0): True (0,1): True (0,2): True (0,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (2,3): True
(0,0): True (0,0): True (0,1): True (0,0): True (0,1): True (0,2): True (0,0): True (0,1): True (0,2): True (1,2): True
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0stdout:
(0,0): True
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0stdout:
(0,0): True
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0stdout:
(0,0): True
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
(0,0): True (0,0): True (0,1): True (0,0): True (0,1): True (0,2): True (0,0): True (0,1): True (0,2): True (0,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (2,3): True
(0,0): True (0,0): True (0,1): True (0,0): True (0,1): True (0,2): True (0,0): True (0,1): True (0,2): True (0,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (2,3): True
(0,0): True (0,0): True (0,1): True (0,0): True (0,1): True (0,2): True (0,0): True (0,1): True (0,2): True (1,2): True
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
(0,0): True (0,0): True (0,1): True (0,0): True (0,1): True (0,2): True (0,0): True (0,1): True (0,2): True (0,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (2,3): True
(0,0): True (0,0): True (0,1): True (0,0): True (0,1): True (0,2): True (0,0): True (0,1): True (0,2): True (0,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (0,0): True (0,1): True (0,2): True (0,3): True (1,3): True (2,3): True
(0,0): True (0,0): True (0,1): True (0,0): True (0,1): True (0,2): True (0,0): True (0,1): True (0,2): True (1,2): True
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
if(down == null) Console.WriteLine("down2");
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
if(right == null) Console.WriteLine("right2");
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private printNode(Node node){
if(node == null) Console.WriteLine("null");
else Console.WriteLine("")
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private printNode(Node node){
if(node == null) Console.WriteLine("null");
else Console.WriteLine("(" + x + "," + y + "")
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private printNode(Node node){
if(node == null) Console.WriteLine("null");
else Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(119,13): error CS1520: Class, struct, or interface method must have a return type
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
(0,0): True (1,0: 1) (0,1: 1) (0,0): True (1,0: 1) (0,1: 1) (0,1): True (1,1: 2) (0,2: 1) (0,0): True (1,0: 1) (0,1: 1) (0,1): True (1,1: 2) (0,2: 1) (0,2): True (1,2: 1) (0,3: 1) (0,0): True (1,0: 1) (0,1: 1) (0,1): True (1,1: 2) (0,2: 1) (0,2): True (1,2: 1) (0,3: 1) (0,3): True (1,3: 2) (0,0): True (1,0: 1) (0,1: 1) (0,1): True (1,1: 2) (0,2: 1) (0,2): True (1,2: 1) (0,3: 1) (0,3): True (1,3: 2) (1,3): True (2,3: 2) (0,0): True (1,0: 1) (0,1: 1) (0,1): True (1,1: 2) (0,2: 1) (0,2): True (1,2: 1) (0,3: 1) (0,3): True (1,3: 2) (1,3): True (2,3: 2) (2,3): True (3,3: 2)
(0,0): True (1,0: 1) (0,1: 1) (0,0): True (1,0: 1) (0,1: 1) (0,1): True (1,1: 1) (0,2: 2) (0,0): True (1,0: 1) (0,1: 1) (0,1): True (1,1: 1) (0,2: 2) (0,2): True (1,2: 2) (0,3: 2) (0,0): True (1,0: 1) (0,1: 1) (0,1): True (1,1: 1) (0,2: 2) (0,2): True (1,2: 2) (0,3: 2) (0,3): True (1,3: 3) (0,0): True (1,0: 1) (0,1: 1) (0,1): True (1,1: 1) (0,2: 2) (0,2): True (1,2: 2) (0,3: 2) (0,3): True (1,3: 3) (1,3): True (2,3: 3) (0,0): True (1,0: 1) (0,1: 1) (0,1): True (1,1: 1) (0,2: 2) (0,2): True (1,2: 2) (0,3: 2) (0,3): True (1,3: 3) (1,3): True (2,3: 3) (2,3): True (3,3: 3)
(0,0): True (1,0: 2) (0,1: 2) (0,0): True (1,0: 2) (0,1: 2) (0,1): True (1,1: 10000000) (0,2: 3) (0,0): True (1,0: 2) (0,1: 2) (0,1): True (1,1: 10000000) (0,2: 3) (0,2): True (1,2: 4) (0,0): True (1,0: 2) (0,1: 2) (0,1): True (1,1: 10000000) (0,2: 3) (0,2): True (1,2: 4) (1,2): True (2,2: 5)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
Console.WriteLine("(" + x + "," + y + "): " + node.Picked);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
(0,0): True (1,0: 1, False) (0,1: 1, False) (0,0): True (1,0: 1, False) (0,1: 1, True) (0,1): True (1,1: 2, False) (0,2: 1, False) (0,0): True (1,0: 1, False) (0,1: 1, True) (0,1): True (1,1: 2, False) (0,2: 1, True) (0,2): True (1,2: 1, False) (0,3: 1, False) (0,0): True (1,0: 1, False) (0,1: 1, True) (0,1): True (1,1: 2, False) (0,2: 1, True) (0,2): True (1,2: 1, False) (0,3: 1, True) (0,3): True (1,3: 2, False) (0,0): True (1,0: 1, False) (0,1: 1, True) (0,1): True (1,1: 2, False) (0,2: 1, True) (0,2): True (1,2: 1, False) (0,3: 1, True) (0,3): True (1,3: 2, True) (1,3): True (2,3: 2, False) (0,0): True (1,0: 1, False) (0,1: 1, True) (0,1): True (1,1: 2, False) (0,2: 1, True) (0,2): True (1,2: 1, False) (0,3: 1, True) (0,3): True (1,3: 2, True) (1,3): True (2,3: 2, True) (2,3): True (3,3: 2, False)
(0,0): True (1,0: 1, False) (0,1: 1, False) (0,0): True (1,0: 1, False) (0,1: 1, True) (0,1): True (1,1: 1, False) (0,2: 2, False) (0,0): True (1,0: 1, False) (0,1: 1, True) (0,1): True (1,1: 1, False) (0,2: 2, True) (0,2): True (1,2: 2, False) (0,3: 2, False) (0,0): True (1,0: 1, False) (0,1: 1, True) (0,1): True (1,1: 1, False) (0,2: 2, True) (0,2): True (1,2: 2, False) (0,3: 2, True) (0,3): True (1,3: 3, False) (0,0): True (1,0: 1, False) (0,1: 1, True) (0,1): True (1,1: 1, False) (0,2: 2, True) (0,2): True (1,2: 2, False) (0,3: 2, True) (0,3): True (1,3: 3, True) (1,3): True (2,3: 3, False) (0,0): True (1,0: 1, False) (0,1: 1, True) (0,1): True (1,1: 1, False) (0,2: 2, True) (0,2): True (1,2: 2, False) (0,3: 2, True) (0,3): True (1,3: 3, True) (1,3): True (2,3: 3, True) (2,3): True (3,3: 3, False)
(0,0): True (1,0: 2, False) (0,1: 2, False) (0,0): True (1,0: 2, False) (0,1: 2, True) (0,1): True (1,1: 10000000, False) (0,2: 3, False) (0,0): True (1,0: 2, False) (0,1: 2, True) (0,1): True (1,1: 10000000, False) (0,2: 3, True) (0,2): True (1,2: 4, False) (0,0): True (1,0: 2, False) (0,1: 2, True) (0,1): True (1,1: 10000000, False) (0,2: 3, True) (0,2): True (1,2: 4, True) (1,2): True (2,2: 5, False)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
(0,0: 0, True) (1,0: 1, False) (0,1: 1, False) (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) (0,2: 1, True) (1,2: 1, False) (0,3: 1, False) (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) (0,3: 1, True) (1,3: 2, False) (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) (0,3: 1, True) (1,3: 2, True) (1,3: 2, True) (2,3: 2, False) (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) (0,3: 1, True) (1,3: 2, True) (1,3: 2, True) (2,3: 2, True) (2,3: 2, True) (3,3: 2, False)
(0,0: 1, True) (1,0: 1, False) (0,1: 1, False) (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) (0,1: 1, True) (1,1: 1, False) (0,2: 2, False) (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) (0,2: 2, True) (1,2: 2, False) (0,3: 2, False) (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) (0,3: 2, True) (1,3: 3, False) (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) (0,3: 2, True) (1,3: 3, True) (1,3: 3, True) (2,3: 3, False) (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) (0,3: 2, True) (1,3: 3, True) (1,3: 3, True) (2,3: 3, True) (2,3: 3, True) (3,3: 3, False)
(0,0: 1, True) (1,0: 2, False) (0,1: 2, False) (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) (0,2: 3, True) (1,2: 4, False) (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) (0,2: 3, True) (1,2: 4, True) (1,2: 4, True) (2,2: 5, False)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
C
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, False) ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, False) ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) ---- (0,3: 1, True) (1,3: 2, False) ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) ---- (0,3: 1, True) (1,3: 2, True) ---- (1,3: 2, True) (2,3: 2, False) ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) ---- (0,3: 1, True) (1,3: 2, True) ---- (1,3: 2, True) (2,3: 2, True) ---- (2,3: 2, True) (3,3: 2, False)
---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, False) ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, False) ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, False) ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) ---- (0,3: 2, True) (1,3: 3, False) ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) ---- (0,3: 2, True) (1,3: 3, True) ---- (1,3: 3, True) (2,3: 3, False) ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) ---- (0,3: 2, True) (1,3: 3, True) ---- (1,3: 3, True) (2,3: 3, True) ---- (2,3: 3, True) (3,3: 3, False)
---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, False) ---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (0,2: 3, True) (1,2: 4, False) ---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (0,2: 3, True) (1,2: 4, True) ---- (1,2: 4, True) (2,2: 5, False)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
Con
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, False) ----end ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ----end ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, False) ----end ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) ---- (0,3: 1, True) (1,3: 2, False) ----end ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) ---- (0,3: 1, True) (1,3: 2, True) ---- (1,3: 2, True) (2,3: 2, False) ----end ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) ---- (0,3: 1, True) (1,3: 2, True) ---- (1,3: 2, True) (2,3: 2, True) ---- (2,3: 2, True) (3,3: 2, False) ----end
---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, False) ----end ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, False) ----end ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, False) ----end ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) ---- (0,3: 2, True) (1,3: 3, False) ----end ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) ---- (0,3: 2, True) (1,3: 3, True) ---- (1,3: 3, True) (2,3: 3, False) ----end ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) ---- (0,3: 2, True) (1,3: 3, True) ---- (1,3: 3, True) (2,3: 3, True) ---- (2,3: 3, True) (3,3: 3, False) ----end
---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, False) ----end ---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ----end ---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (0,2: 3, True) (1,2: 4, False) ----end ---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (0,2: 3, True) (1,2: 4, True) ---- (1,2: 4, True) (2,2: 5, False) ----end
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
C
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (0,1: 1, True) ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ----end minimum: (0,2: 1, True) ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, False) ----end minimum: (0,3: 1, True) ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) ---- (0,3: 1, True) (1,3: 2, False) ----end minimum: (1,3: 2, True) ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) ---- (0,3: 1, True) (1,3: 2, True) ---- (1,3: 2, True) (2,3: 2, False) ----end minimum: (2,3: 2, True) ---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, True) ---- (0,3: 1, True) (1,3: 2, True) ---- (1,3: 2, True) (2,3: 2, True) ---- (2,3: 2, True) (3,3: 2, False) ----end minimum: (3,3: 2, True)
---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (0,1: 1, True) ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, False) ----end minimum: (0,2: 2, True) ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, False) ----end minimum: (0,3: 2, True) ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) ---- (0,3: 2, True) (1,3: 3, False) ----end minimum: (1,3: 3, True) ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) ---- (0,3: 2, True) (1,3: 3, True) ---- (1,3: 3, True) (2,3: 3, False) ----end minimum: (2,3: 3, True) ---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, True) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, True) ---- (0,2: 2, True) (1,2: 2, False) (0,3: 2, True) ---- (0,3: 2, True) (1,3: 3, True) ---- (1,3: 3, True) (2,3: 3, True) ---- (2,3: 3, True) (3,3: 3, False) ----end minimum: (3,3: 3, True)
---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, False) ----end minimum: (0,1: 2, True) ---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ----end minimum: (0,2: 3, True) ---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (0,2: 3, True) (1,2: 4, False) ----end minimum: (1,2: 4, True) ---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, True) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (0,2: 3, True) (1,2: 4, True) ---- (1,2: 4, True) (2,2: 5, False) ----end minimum: (2,2: 5, True)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = false;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ----end minimum: (0,1: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ----end minimum: (2,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ----end minimum: (0,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, False) ----end minimum: (1,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, False) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ----end minimum: (0,3: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ----end minimum: (2,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, True) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ---- (2,2: 1, True) (3,2: 2, False) (2,3: 1, False) ----end mini
---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ----end minimum: (0,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, False) ----end minimum: (1,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, False) (1,2: 1, False) ----end minimum: (2,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, False) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ----end minimum: (1,2: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ----end minimum: (2,0: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ----end minimum: (0,2: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, True) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ---- (0,2: 2, True) (1,2: 1, True) (0,3
---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, False) ----end minimum: (1,0: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, False) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ----end minimum: (0,1: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ----end minimum: (2,0: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ---- (2,0: 3, True) (2,1: 4, False) ----end minimum: (0,2: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, False) ---- (0,2: 3, True) (1,2: 4, False) ----end minimum: (2,1: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, False) ---- (2,1: 4, True) (2,2: 5, False) ----end minimum: (1,2: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, True) ---- (2,1: 4, True) (2,2: 5, False) ---- (1,2: 4, True) (2,2: 5, False) ----end minimum: (2,2: 5, True)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = A[0];
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = A[0][0] == 0;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0) existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = A[0][0] == 0;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ----end minimum: (0,1: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ----end minimum: (2,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ----end minimum: (0,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, False) ----end minimum: (1,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, False) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ----end minimum: (0,3: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ----end minimum: (2,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, True) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ---- (2,2: 1, True) (3,2: 2, False) (2,3: 1, False) ----end mini
---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ----end minimum: (0,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, False) ----end minimum: (1,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, False) (1,2: 1, False) ----end minimum: (2,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, False) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ----end minimum: (1,2: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ----end minimum: (2,0: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ----end minimum: (0,2: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, True) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ---- (0,2: 2, True) (1,2: 1, True) (0,3
---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, False) ----end minimum: (1,0: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, False) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ----end minimum: (0,1: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ----end minimum: (2,0: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ---- (2,0: 3, True) (2,1: 4, False) ----end minimum: (0,2: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, False) ---- (0,2: 3, True) (1,2: 4, False) ----end minimum: (2,1: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, False) ---- (2,1: 4, True) (2,2: 5, False) ----end minimum: (1,2: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, True) ---- (2,1: 4, True) (2,2: 5, False) ---- (1,2: 4, True) (2,2: 5, False) ----end minimum: (2,2: 5, True)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0)´ existsSomeZero = true;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = A[0][0] == 0;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
if(A[x][y] == 0){
existsSomeZero = true;
Console.WriteLine("existsSomeZero");
}
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = A[0][0] == 0;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ----end minimum: (0,1: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ----end minimum: (2,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ----end minimum: (0,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, False) ----end minimum: (1,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, False) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ----end minimum: (0,3: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ----end minimum: (2,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, True) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ---- (2,2: 1, True) (3,2: 2, False) (2,3: 1, False) ----end mini
---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ----end minimum: (0,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, False) ----end minimum: (1,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, False) (1,2: 1, False) ----end minimum: (2,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, False) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ----end minimum: (1,2: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ----end minimum: (2,0: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ----end minimum: (0,2: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, True) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ---- (0,2: 2, True) (1,2: 1, True) (0,3
---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, False) ----end minimum: (1,0: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, False) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ----end minimum: (0,1: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ----end minimum: (2,0: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ---- (2,0: 3, True) (2,1: 4, False) ----end minimum: (0,2: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, False) ---- (0,2: 3, True) (1,2: 4, False) ----end minimum: (2,1: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, False) ---- (2,1: 4, True) (2,2: 5, False) ----end minimum: (1,2: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, True) ---- (2,1: 4, True) (2,2: 5, False) ---- (1,2: 4, True) (2,2: 5, False) ----end minimum: (2,2: 5, True)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = A[0][0] == 0;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = A[0][0] == 0;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = A[0][0] == 0;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
Compilation failed: 1 error(s), 0 warnings Solution.cs(167,35): error CS1501: No overload for method `GetNode' takes `5' arguments Solution.cs(90,17): (Location of the symbol related to previous error)
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, );
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
bool existsSomeZero = A[0][0] == 0;
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ----end minimum: (0,1: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ----end minimum: (2,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ----end minimum: (0,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, False) ----end minimum: (1,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, False) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ----end minimum: (0,3: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ----end minimum: (2,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, True) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ---- (2,2: 1, True) (3,2: 2, False) (2,3: 1, False) ----end mini
---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ----end minimum: (0,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, False) ----end minimum: (1,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, False) (1,2: 1, False) ----end minimum: (2,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, False) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ----end minimum: (1,2: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ----end minimum: (2,0: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ----end minimum: (0,2: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, True) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ---- (0,2: 2, True) (1,2: 1, True) (0,3
---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, False) ----end minimum: (1,0: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, False) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ----end minimum: (0,1: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ----end minimum: (2,0: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ---- (2,0: 3, True) (2,1: 4, False) ----end minimum: (0,2: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, False) ---- (0,2: 3, True) (1,2: 4, False) ----end minimum: (2,1: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, False) ---- (2,1: 4, True) (2,2: 5, False) ----end minimum: (1,2: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, True) ---- (2,1: 4, True) (2,2: 5, False) ---- (1,2: 4, True) (2,2: 5, False) ----end minimum: (2,2: 5, True) min 1 dist
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
[[1]]
[[0]]
[[2]]
[[10]]
---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ----end minimum: (0,1: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ----end minimum: (2,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ----end minimum: (0,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, False) ----end minimum: (1,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, False) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ----end minimum: (0,3: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ----end minimum: (2,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, True) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ---- (2,2: 1, True) (3,2: 2, False) (2,3: 1, False) ----end mini
---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ----end minimum: (0,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, False) ----end minimum: (1,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, False) (1,2: 1, False) ----end minimum: (2,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, False) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ----end minimum: (1,2: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ----end minimum: (2,0: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ----end minimum: (0,2: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, True) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ---- (0,2: 2, True) (1,2: 1, True) (0,3
---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, False) ----end minimum: (1,0: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, False) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ----end minimum: (0,1: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ----end minimum: (2,0: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ---- (2,0: 3, True) (2,1: 4, False) ----end minimum: (0,2: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, False) ---- (0,2: 3, True) (1,2: 4, False) ----end minimum: (2,1: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, False) ---- (2,1: 4, True) (2,2: 5, False) ----end minimum: (1,2: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, True) ---- (2,1: 4, True) (2,2: 5, False) ---- (1,2: 4, True) (2,2: 5, False) ----end minimum: (2,2: 5, True) min 1 dist
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0stdout:
---- (0,0: 0, True) ----end
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0stdout:
---- (0,0: 10000000, True) ----end
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0stdout:
---- (0,0: 0, True) ----end
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0 [ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object at Solution.solution (System.Int32[][] A) [0x00000] in <filename unknown>:0 at SolutionWrapper.run (System.String input, System.String output) [0x00000] in <filename unknown>:0 at SolutionWrapper.Main (System.String[] args) [0x00000] in <filename unknown>:0stdout:
---- (0,0: 1, True) ----end
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if()
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
}
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
return
}
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
return Math.Min(total2, total5);
}
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
return Math.Min(node.Total2, node.Total5);
}
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
minDistance = Math.Min(node.Total2, node.Total5);
break;
}
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
[[1]]
[[0]]
[[2]]
[[10]]
---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ----end minimum: (0,1: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ----end minimum: (2,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ----end minimum: (0,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, False) ----end minimum: (1,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, False) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ----end minimum: (0,3: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ----end minimum: (2,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, True) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ---- (2,2: 1, True) (3,2: 2, False) (2,3: 1, False) ----end mini
---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ----end minimum: (0,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, False) ----end minimum: (1,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, False) (1,2: 1, False) ----end minimum: (2,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, False) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ----end minimum: (1,2: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ----end minimum: (2,0: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ----end minimum: (0,2: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, True) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ---- (0,2: 2, True) (1,2: 1, True) (0,3
---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, False) ----end minimum: (1,0: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, False) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ----end minimum: (0,1: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ----end minimum: (2,0: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ---- (2,0: 3, True) (2,1: 4, False) ----end minimum: (0,2: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, False) ---- (0,2: 3, True) (1,2: 4, False) ----end minimum: (2,1: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, False) ---- (2,1: 4, True) (2,2: 5, False) ----end minimum: (1,2: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, True) ---- (2,1: 4, True) (2,2: 5, False) ---- (1,2: 4, True) (2,2: 5, False) ----end minimum: (2,2: 5, True) min 1 dist
function result: 0
---- (0,0: 0, True) ----end
function result: 0
---- (0,0: 10000000, True) ----end min 1 dist
function result: 0
---- (0,0: 0, True) ----end
function result: 1
---- (0,0: 1, True) ----end
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
minDistance = no
break;
}
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
minDistance = node.Distance;
break;
}
minimum.SetPicked();
shortest.Add(minimum);
Console.WriteLine("minimum:");
printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
[[1]]
[[0]]
[[2]]
[[10]]
---- (0,0: 0, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ----end minimum: (0,1: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, False) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ----end minimum: (2,0: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, False) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ----end minimum: (0,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, False) (0,3: 1, False) ----end minimum: (1,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, False) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ----end minimum: (0,3: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, False) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ----end minimum: (2,2: 1, True) ---- (0,0: 0, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 1, True) (1,1: 2, False) ---- (0,1: 1, True) (1,1: 2, False) (0,2: 1, True) ---- (2,0: 1, True) (3,0: 3, False) (2,1: 2, False) ---- (0,2: 1, True) (1,2: 1, True) (0,3: 1, True) ---- (1,2: 1, True) (2,2: 1, True) (1,3: 2, False) ---- (0,3: 1, True) (1,3: 2, False) ---- (2,2: 1, True) (3,2: 2, False) (2,3: 1, False) ----end mini
---- (0,0: 1, True) (1,0: 1, False) (0,1: 1, False) ----end minimum: (1,0: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, False) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ----end minimum: (0,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, False) ---- (0,1: 1, True) (1,1: 1, False) (0,2: 2, False) ----end minimum: (1,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, False) (1,2: 1, False) ----end minimum: (2,1: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, False) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ----end minimum: (1,2: 1, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, False) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ----end minimum: (2,0: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, False) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ----end minimum: (0,2: 2, True) ---- (0,0: 1, True) (1,0: 1, True) (0,1: 1, True) ---- (1,0: 1, True) (2,0: 2, True) (1,1: 1, True) ---- (0,1: 1, True) (1,1: 1, True) (0,2: 2, True) ---- (1,1: 1, True) (2,1: 1, True) (1,2: 1, True) ---- (2,1: 1, True) (3,1: 2, False) (2,2: 2, False) ---- (1,2: 1, True) (2,2: 2, False) (1,3: 2, False) ---- (2,0: 2, True) (3,0: 2, False) (2,1: 1, True) ---- (0,2: 2, True) (1,2: 1, True) (0,3
---- (0,0: 1, True) (1,0: 2, False) (0,1: 2, False) ----end minimum: (1,0: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, False) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ----end minimum: (0,1: 2, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, False) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ----end minimum: (2,0: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, False) ---- (2,0: 3, True) (2,1: 4, False) ----end minimum: (0,2: 3, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, False) ---- (0,2: 3, True) (1,2: 4, False) ----end minimum: (2,1: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, False) ---- (2,1: 4, True) (2,2: 5, False) ----end minimum: (1,2: 4, True) ---- (0,0: 1, True) (1,0: 2, True) (0,1: 2, True) ---- (1,0: 2, True) (2,0: 3, True) (1,1: 10000000, False) ---- (0,1: 2, True) (1,1: 10000000, False) (0,2: 3, True) ---- (2,0: 3, True) (2,1: 4, True) ---- (0,2: 3, True) (1,2: 4, True) ---- (2,1: 4, True) (2,2: 5, False) ---- (1,2: 4, True) (2,2: 5, False) ----end minimum: (2,2: 5, True) min 1 dist
function result: 0
---- (0,0: 0, True) ----end
function result: 1
---- (0,0: 10000000, True) ----end min 1 dist
function result: 0
---- (0,0: 0, True) ----end
function result: 1
---- (0,0: 1, True) ----end
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
minDistance = node.Distance;
break;
}
minimum.SetPicked();
shortest.Add(minimum);
//Console.WriteLine("minimum:");
//printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
//printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
//printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
//printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
//Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
minDistance = node.Distance;
break;
}
minimum.SetPicked();
shortest.Add(minimum);
//Console.WriteLine("minimum:");
//printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
[[1]]
[[0]]
[[2]]
[[10]]
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- min 1 dist
function result: 0
----
function result: 1
---- min 1 dist
function result: 0
----
function result: 1
----
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
//Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
//printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
//printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
//printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
//Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
minDistance = node.Distance;
break;
}
minimum.SetPicked();
shortest.Add(minimum);
//Console.WriteLine("minimum:");
//printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
[[1]]
[[0]]
[[2]]
[[10]]
function result: 0
function result: 1
min 1 dist
function result: 0
function result: 1
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
//Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
//printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
//printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
//printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
//Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
minDistance = node.Distance;
break;
}
minimum.SetPicked();
shortest.Add(minimum);
//Console.WriteLine("minimum:");
//printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
//Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
//Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
//printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
//printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
//printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
//Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
minDistance = node.Distance;
break;
}
minimum.SetPicked();
shortest.Add(minimum);
//Console.WriteLine("minimum:");
//printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
//Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
[[1]]
[[0]]
[[2]]
[[10]]
function result: 0
function result: 1
function result: 0
function result: 1
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
//Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
//printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
//printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
//printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
//Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
minDistance = node.Distance;
break;
}
minimum.SetPicked();
shortest.Add(minimum);
//Console.WriteLine("minimum:");
//printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
//Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
[[1]]
[[0]]
[[2]]
[[10]]
function result: 0
function result: 1
function result: 0
function result: 1
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
//Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
//printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
//printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
//printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
//Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
minDistance = node.Distance;
break;
}
minimum.SetPicked();
shortest.Add(minimum);
//Console.WriteLine("minimum:");
//printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
//Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
[[1]]
[[0]]
[[2]]
[[10]]
function result: 0
function result: 1
function result: 0
function result: 1
using System;
using System.Collections.Generic;
using System.Linq;
// you can also use other imports, for example:
// using System.Collections.Generic;
// you can write to stdout for debugging purposes, e.g.
// Console.WriteLine("this is a debug message");
class CountDivisor{
private Dictionary<int,int> divisorsOf2;
private Dictionary<int,int> divisorsOf5;
public CountDivisor(){
divisorsOf2 = new Dictionary<int,int>(10000);
divisorsOf5 = new Dictionary<int,int>(10000);
}
private int getCountDivisor(int n, int d){
int count = 0;
while(n%d == 0){
n/=d;
count++;
}
return count;
}
public int getCountDivisors2(int n){
if(divisorsOf2.ContainsKey(n)) return divisorsOf2[n];
var count = getCountDivisor(n, 2);
divisorsOf2[n] = count;
return count;
}
public int getCountDivisors5(int n){
if(divisorsOf5.ContainsKey(n)) return divisorsOf5[n];
var count = getCountDivisor(n, 5);
divisorsOf5[n] = count;
return count;
}
}
class Node{
public int X {get;}
public int Y {get;}
public int Total2 {get;}
public int Total5 {get;}
public int Distance {get; private set;}
public bool Picked {get; private set;}
public Node(int x, int y, int d, int total2, int total5){
X = x;
Y = y;
Distance = d;
Total2 = total2;
Total5 = total5;
Picked = false;
}
public void SetPicked(){
Picked = true;
}
}
class NodeRepository{
private const int ZERO = 0;
private const int BIG_DISTANCE = 10000000;
private CountDivisor countDivisor;
private Dictionary<Tuple<int,int>, Node> nodes;
public NodeRepository(){
countDivisor = new CountDivisor();
nodes = new Dictionary<Tuple<int,int>, Node>(1000);
}
public Node GetNode(int[][] A, int x, int y, int total2, int total5, ref bool existsSomeZero){
var key = Tuple.Create(x, y);
if(nodes.ContainsKey(key)) return nodes[key];
var currValue = A[x][y];
if(currValue == ZERO){
existsSomeZero = true;
var newNodeZero = new Node(x, y, BIG_DISTANCE, 0, 0);
nodes[key] = newNodeZero;
return newNodeZero;
}
total2 += countDivisor.getCountDivisors2(currValue);
total5 += countDivisor.getCountDivisors5(currValue);
var distance = Math.Min(total2, total5);
var newNode = new Node(x, y, distance, total2, total5);
nodes[key] = newNode;
return newNode;
}
}
class Solution {
private const int BIG_DISTANCE = 10000000;
private NodeRepository nodeRepository;
public Solution(){
nodeRepository = new NodeRepository();
}
private void printNode(Node node){
if(node == null) Console.WriteLine("null");
else
Console.WriteLine("(" + node.X + "," + node.Y + ": " + node.Distance + ", " + node.Picked + ")");
}
// Get minimum from neighbors
private Node getMimimumNoPicked(int[][] A, List<Node> shortest
, int maxRow, int maxCol, ref bool existsSomeZero){
var minDistance = int.MaxValue;
Node nodeMinDistance = null;
foreach(var node in shortest){
//Console.WriteLine("----");
var x = node.X;
var y = node.Y;
var total2 = node.Total2;
var total5 = node.Total5;
//printNode(node);
if(x <= maxRow - 2){
var down = nodeRepository.GetNode(A, x+1, y, total2, total5, ref existsSomeZero);
//printNode(down);
if(!down.Picked){
if(down.Distance < minDistance){
minDistance = down.Distance;
nodeMinDistance = down;
}
}
}
if(y <= maxCol - 2){
var right = nodeRepository.GetNode(A, x, y+1, total2, total5, ref existsSomeZero);
//printNode(right);
if(!right.Picked){
if(right.Distance < minDistance){
minDistance = right.Distance;
nodeMinDistance = right;
}
}
}
}
//Console.WriteLine("----end");
return nodeMinDistance;
}
public int solution(int[][] A) {
int n = A.Length;
bool existsSomeZero = false;
var node = nodeRepository.GetNode(A, 0, 0, 0, 0, ref existsSomeZero);
node.SetPicked();
var shortest = new List<Node>(250000);
shortest.Add(node);
int minDistance = 0;
for(int i=0; i<100000000; i++){
var minimum = getMimimumNoPicked(A, shortest, n, n, ref existsSomeZero);
if(minimum == null){
minDistance = node.Distance;
break;
}
minimum.SetPicked();
shortest.Add(minimum);
//Console.WriteLine("minimum:");
//printNode(minimum);
if(minimum.X == n-1 && minimum.Y == n-1){
minDistance = minimum.Distance;
break;
}
}
if(existsSomeZero){
//Console.WriteLine("min 1 dist");
return Math.Min(1, minDistance);
}
return minDistance;
}
}
The following issues have been detected: timeout errors.
alternating diagonals consist of 2 or 5
running time: >6.00 sec., time limit: 0.10 sec.
all elements in A are neither dividable by 2 or 5; length of A is close to 500
running time: >6.00 sec., time limit: 0.10 sec.
all elements in A are dividable by either power of 2 or 5; length of A is close to 500
running time: >6.00 sec., time limit: 0.10 sec.
tests with corner cases values, maximal A size
running time: >6.00 sec., time limit: 0.58 sec.
tests with defined result; length of A is close to 500
running time: >6.00 sec., time limit: 0.10 sec.
alternating diagonals consist of 2 or 5
running time: >6.00 sec., time limit: 0.29 sec.