Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- class SpaceshipMomentum {
- private static String V, M;
- private static char[] temp;
- private static char[] velocity;
- private static char[] mass;
- public static void main(String[] args) {
- // long startTime = System.nanoTime();
- Scanner sc = new Scanner(System.in);
- PrintWriter pw = new PrintWriter(new BufferedWriter(
- new OutputStreamWriter(System.out)));
- int T, B;
- String output;
- T = sc.nextInt();
- for (int i = 1; i <= T; ++i) {
- B = sc.nextInt();
- sc.nextLine();
- V = sc.nextLine();
- M = sc.nextLine();
- // velocity = new char[V.length()];
- // mass = new char[M.length()];
- if (V.equals("0") || M.equals("0")) {
- output = "0";
- } else {
- velocity = V.toCharArray();
- mass = M.toCharArray();
- // preProcess(velocity, mass);
- output = multiply(velocity, mass);
- }
- pw.write(trimZeros(output));
- pw.write("\n");
- }
- // long endTime = System.nanoTime();
- //
- // long elapsedTime = (endTime - startTime);
- // double seconds = (double)elapsedTime / 1000000000.0;
- //
- // pw.write("timing: ");
- // pw.write("\n");
- // pw.write(String.valueOf(seconds));
- // pw.write("\n");
- pw.close();
- }
- /**
- * Convert the input strings (velocity and mass) into ArrayList
- */
- private static void preProcess(char[] velocity, char[] mass) {
- Arrays.fill(velocity, '0');
- Arrays.fill(mass, '0');
- int index = 0;
- for (int i = V.length(); i > 0; i--) {
- velocity[index] = V.charAt(i - 1);
- index++;
- }
- index = 0;
- for (int i = M.length(); i > 0; i--) {
- mass[index] = M.charAt(i - 1);
- index++;
- }
- }
- /**
- * Multiply velocity and mass, then output the result
- */
- private static String multiply(char[] v, char[] m) {
- String start = "1";
- char[] reversedOutput = new char[V.length() + M.length() + 1];
- String output = "";
- Arrays.fill(reversedOutput, '0');
- for (int i = m.length; i > 0; i--) {
- multiplication(v, m[i - 1], start);
- addition(reversedOutput);
- start = start.concat("0");
- }
- for (int i = reversedOutput.length; i > 0; i--) {
- output = output.concat(String.valueOf(reversedOutput[i - 1]));
- }
- return output;
- }
- /**
- * Add the temporary multiplication result to the final output
- */
- private static void addition(char[] reversedOutput) {
- int arg1, arg2;
- int tempResult;
- int carry = 0;
- int index = 0;
- for (int i = 0; i < temp.length; i++) {
- arg1 = parseDigit(reversedOutput[i]);
- arg2 = parseDigit(temp[i]);
- tempResult = arg1 + arg2 + carry;
- reversedOutput[i] = toDigit(tempResult % 10);
- carry = tempResult / 10;
- index = i;
- }
- if (carry > 0) {
- reversedOutput[index + 1] = toDigit(carry);
- }
- }
- /**
- * Multiply each digit of mass with velocity, then output the result
- */
- private static void multiplication(char[] v, char c, String start) {
- temp = new char[V.length() + start.length() + 1];
- Arrays.fill(temp, '0');
- int arg1 = parseDigit(c);
- int carry = 0;
- int index = start.length() - 1;
- for (int i = v.length; i > 0; i--) {
- int arg2 = parseDigit(v[i - 1]);
- int tempResult = arg1 * arg2;
- tempResult += carry;
- if (tempResult > 9) {
- carry = tempResult / 10;
- } else {
- carry = 0;
- }
- temp[index] = toDigit(tempResult % 10);
- index++;
- }
- if (carry != 0) {
- temp[index] = toDigit(carry);
- }
- }
- /**
- * Use to trim leading and trailing zeros on a result string.
- */
- private static String trimZeros(String input) {
- int left = 0;
- int right = input.length() - 1;
- int fp = input.indexOf('.');
- if (fp == -1) {
- fp = input.length();
- }
- while (left < fp - 1) {
- if (input.charAt(left) != '0')
- break;
- left++;
- }
- while (right >= fp) {
- if (input.charAt(right) != '0') {
- if (input.charAt(right) == '.')
- right--;
- break;
- }
- right--;
- }
- if (left >= fp)
- return "0" + input.substring(left, right + 1);
- return input.substring(left, right + 1);
- }
- /**
- * Convert digit to int (for reading)
- */
- private static int parseDigit(char c) {
- if (c <= '9') {
- return c - '0';
- }
- return c - 'A' + 10;
- }
- /**
- * Convert int to digit. (for printing)
- */
- private static char toDigit(int digit) {
- if (digit <= 9) {
- return (char) (digit + '0');
- }
- return (char) (digit - 10 + 'A');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement