saurav_kalsoor

Complex Multiplication - JAVA

Jul 26th, 2021 (edited)
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Scanner;
  5.  
  6. public class Problem {
  7.  
  8.     public static void main(String[] args){
  9.         Scanner sc = new Scanner(System.in);
  10.         int n = sc.nextInt();
  11.         ArrayList<Long> real = new ArrayList<>();
  12.         ArrayList<Long> imaginary = new ArrayList<>();
  13.  
  14.         for(int i=0; i<n; i++){
  15.             long num = sc.nextLong();
  16.             real.add(num);
  17.         }
  18.  
  19.         for(int i=0; i<n; i++){
  20.             long num = sc.nextLong();
  21.             imaginary.add(num);
  22.         }
  23.  
  24.  
  25.         ArrayList<Long> result = multiplyComplexNumbers(real, imaginary, n);
  26.         System.out.println(result.get(0) + " + (" + result.get(1) + ")i");
  27.     }
  28.  
  29.     public static ArrayList<Long> multiplyComplexNumbers(ArrayList<Long> real, ArrayList<Long> imaginary,  int n){
  30.         ArrayList<Long> result = new ArrayList<>();
  31.         result.add(1L);
  32.         result.add(0L);
  33.  
  34.         for(int i=0; i < n; i++){
  35.             result = multiply(result.get(0), result.get(1), real.get(i), imaginary.get(i));
  36.         }
  37.         return result;
  38.     }
  39.  
  40.     public static ArrayList<Long> multiply(long a, long b, long x, long y){
  41.         // ax - by + i(bx + ay)
  42.         long MOD = 1000000007;
  43.         ArrayList<Long> result = new ArrayList<>();
  44.         result.add((a*x - b*y)%MOD);
  45.         result.add((b*x + a*y)%MOD);
  46.         return result;
  47.     }
  48.  
  49. }
Add Comment
Please, Sign In to add comment