forked from kalmar/DALGLI0
54 lines
1.4 KiB
Java
54 lines
1.4 KiB
Java
|
import java.util.ArrayList;
|
||
|
import java.util.Scanner;
|
||
|
|
||
|
public class Main {
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
|
||
|
ArrayList<Integer> A = new ArrayList<>();
|
||
|
ArrayList<Integer> B = new ArrayList<>();
|
||
|
|
||
|
Scanner input = new Scanner(System.in);
|
||
|
System.out.print("Podaj modulo: ");
|
||
|
int m = input.nextInt();
|
||
|
|
||
|
System.out.print("Podaj najwyższą potęgę współczynników A: ");
|
||
|
int n = input.nextInt();
|
||
|
System.out.print("Podaj liczby A: ");
|
||
|
for(int i=0; i<=n; i++){
|
||
|
A.add(input.nextInt());
|
||
|
}
|
||
|
|
||
|
System.out.print("Podaj najwyższą potęgę współczynników B: ");
|
||
|
n = input.nextInt();
|
||
|
System.out.print("Podaj liczby B: ");
|
||
|
for(int i=0; i<=n; i++){
|
||
|
B.add(input.nextInt());
|
||
|
}
|
||
|
mnozenie(A,B, A.size(), B.size(), m);
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
public static void mnozenie(ArrayList<Integer> A, ArrayList<Integer> B, int A_size, int B_size, int modulo){
|
||
|
ArrayList<Integer> wynik = new ArrayList<>();
|
||
|
int[] pr = new int[A_size+B_size-1];
|
||
|
|
||
|
for (int i = 0; i<A_size+B_size-1; i++)
|
||
|
pr[i]=0;
|
||
|
for (int i=0; i<A_size; i++)
|
||
|
{
|
||
|
for (int j=0; j<B_size; j++) {
|
||
|
pr[i + j] += A.get(i) * B.get(j);
|
||
|
}
|
||
|
}
|
||
|
for(int i = 0; i<A_size+B_size-1; i++){
|
||
|
wynik.add(i, pr[i]%modulo);
|
||
|
|
||
|
}
|
||
|
System.out.println( wynik);
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|