Problem solution · Java

CCC 2005 S3 - Quantum Operations

CCC 2005 S3 - Quantum Operations: a Java solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
97 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For CCC 2005 S3 - Quantum Operations, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 97 lines of Java from the credited upstream file ccc05s3.java.
  • The implementation visibly relies on sequence storage.
  • 11 loop blocks detected.

Complexity

Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.

Check the problem constraints before deciding whether this complexity will pass.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 2005 S3 - Quantum Operations · JavaJava
Use this to learn the idea, then write your own version.
// The "CCC2005s3QuantumOperations" class.//// A 2d matrix type manipulation exercise// Not super tough, but you need to keep your wits about you// during the multiplication phase. import java.awt.*;import hsa.*; public class CCC2005s3QuantumOperations{    static Console cc;     public static void main (String[] args)    {	cc = new Console ();	TextInputFile f = new TextInputFile ("s3.1.in");	int[] [] product = new int [1024] [1024];	int[] [] a = new int [1024] [1024];	int[] [] b = new int [1024] [1024];	int[] rowSum = new int [1024];	int[] colSum = new int [1024];	int maxElement, minElement, maxRowSum, minRowSum, maxColSum, minColSum; 	int n, r, c, pr, pc; 	// b is 1 at start	pr = 1;	pc = 1;	b [0] [0] = 1; 	n = f.readInt ();	for (int k = 0 ; k < n ; k++)	{	    // read next matrix	    r = f.readInt ();	    c = f.readInt ();	    for (int i = 0 ; i < r ; i++)		for (int j = 0 ; j < c ; j++)		    a [i] [j] = f.readInt (); 	    // calculate product = a * b	    for (int p = 0 ; p < r * pr ; p++)		for (int q = 0 ; q < c * pc ; q++)		    product [p] [q] = b [p % pr] [q % pc] * a [p / pr] [q / pc]; 	    pr = r * pr;	    pc = c * pc;	    // b = product	    for (int p = 0 ; p < pr ; p++)		for (int q = 0 ; q < pc ; q++)		    b [p] [q] = product [p] [q];	} 	// gather stats	maxElement = product [0] [0];	minElement = product [0] [0]; 	for (int i = 0 ; i < pr ; i++)	    for (int j = 0 ; j < pc ; j++)	    {		rowSum [i] += product [i] [j];		colSum [j] += product [i] [j];		if (product [i] [j] > maxElement)		    maxElement = product [i] [j];		if (product [i] [j] < minElement)		    minElement = product [i] [j];	    } 	maxRowSum = rowSum [0];	minRowSum = rowSum [0];	maxColSum = colSum [0];	minColSum = colSum [0];	for (int i = 0 ; i < pr ; i++)	{	    if (rowSum [i] > maxRowSum)		maxRowSum = rowSum [i];	    if (rowSum [i] < minRowSum)		minRowSum = rowSum [i];	}	for (int j = 0 ; j < pc ; j++)	{	    if (colSum [j] > maxColSum)		maxColSum = colSum [j];	    if (colSum [j] < minColSum)		minColSum = colSum [j];	} 	cc.println (maxElement);	cc.println (minElement);	cc.println (maxRowSum);	cc.println (minRowSum);	cc.println (maxColSum);	cc.println (minColSum);    }} 

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗