import java.util.LinkedList; import java.util.Queue; public class Main { public static void main(String[] args) { String input = args[0]; String[] parts = input.split(" "); // n int mod = Integer.parseInt(parts[0]); //int number = Character.getNumericValue(numberStr.charAt(0)); String polynomialStr = parts[1]; // usuwam '[', ']' polynomialStr = polynomialStr.substring(1, polynomialStr.length()-1); // , String[] polynomialNumbers = polynomialStr.split(","); LinkedList polynomial = new LinkedList(); //test poly LinkedList polynomial2 = new LinkedList(); // wypelnienie wielomianu polynomial = fill(polynomialNumbers, polynomial); //test LinkedList> elements = new LinkedList>(); elements = createCandidates(polynomial,mod); // Idemp System.out.println(idempotent(elements,polynomial,mod)); //showPoly(polynomial); } // wypelnienie wielomianu public static LinkedList fill(String[] str, LinkedList polynomial){ for(int i=0; i poly) { System.out.println(poly.toString()); } public static int multiplier(int number, int expect, int mod) { /* if(nwd(number,mod) != 1 || nwd(number,mod) != number) { System.out.println("NIE DA SIE"); return 0; }*/ for(int i=1;i modPolynomial(int mod, LinkedList polynomial){ LinkedList result = new LinkedList(); while(!polynomial.isEmpty()) { if(polynomial.peek() < 0) { result.add(mod + polynomial.poll()); } else result.add(polynomial.poll() % mod); } return result; } public static LinkedList multiplyPolynomial(LinkedList p1, int multiplier){ LinkedList result = new LinkedList(); while(!p1.isEmpty()) { result.add(p1.poll() * multiplier); } return result; } public static LinkedList shiftList(LinkedList p, int places){ LinkedList result = new LinkedList(p); if(places == 0) { return result; } for(; places>0; places--) { result.addFirst(0); } return result; } public static LinkedList polynomialsMultiplication(LinkedList p1, LinkedList p2){ // suma dlugosci int amount = p1.size() + p2.size(); // nowy wielomian bedzie dlugosci sumy poteg wyrazow wiodacych obu tablic -1 LinkedList result = new LinkedList(); for(int i=0;i substractPolynomials(LinkedList p1, LinkedList p2){ LinkedList result = new LinkedList(); LinkedList p2Copy = new LinkedList(); for(int i=0; i1) { result.removeLast(); } else break; } return result; } public static LinkedList> createCandidates(LinkedList orig, int mod){ LinkedList> elements = new LinkedList>(); LinkedList result = new LinkedList<>(); result.add(0); int i = 0; while (result.size() < orig.size()){ elements.add(new LinkedList<>(result)); i = (i + 1) % mod; result.set(0, i); if (i == 0){ if (result.size() == 1){ result.add(1); }else { int tmp = (result.get(1) + 1); result.set(1, tmp); for (int k = 1; k < result.size(); ++k){ if (result.get(k) == 0 || result.get(k) % mod != 0) break; tmp = (result.get(k)) % mod; result.set(k, 0); if (tmp == 0){ if (k + 1 < result.size()){ tmp = (result.get(k + 1) + 1); result.set(k + 1, tmp); } else { result.add(1); } } } } } } return elements; } public static LinkedList dividePolynomials(LinkedList p1, LinkedList p2, int mod) { LinkedList result = new LinkedList(p1); LinkedList tmpP = new LinkedList(); int stP1 = p1.size(); int stP2 = p2.size(); if(p1.size() < p2.size()) { System.out.println("NIE DA SIE"); return result; } int tmp; // sprawdzenie czy reszta jest mniejszego stopnia while(stP1 >= stP2) { tmp = multiplier(p2.getLast(), result.getLast(), mod); // przesuniecie tmpP = shiftList(p2,stP1 - stP2); if(tmp != 0) { // mnoznie przez liczbe tmpP = multiplyPolynomial(tmpP,tmp); } // dzielenie modulo tmpP = modPolynomial(mod,tmpP); // odejmowanie result = substractPolynomials(result,tmpP); result = modPolynomial(mod, result); // st po odjeciu stP1 = result.size(); } return result; } }