Pair Calculate(int u) { BigInteger ans; Pair temp; temp = new Pair(); if (g[u].size() == 0) { temp.x = BigInteger.ONE; temp.n = 1; return temp; } if (g[u].size() == 1) { temp = Calculate((int) g[u].get(0)); temp.n++; return temp; } int p, q; temp = Calculate((int) g[u].get(0)); ans = temp.x; p = temp.n; temp = Calculate((int) g[u].get(1)); ans = ans.multiply(temp.x); q = temp.n; ans = ans.multiply(C[p + q][p]); temp.x = ans; temp.n = p + q + 1; return temp; }
public BigInteger Encrypto(int m, BigInteger n, BigInteger g) { int r = new Random().nextInt(10) + 3; BigInteger Ans = BigInteger.ZERO; BigInteger nSquare = n.multiply(n); Ans = g.modPow(BigInteger.valueOf(m), nSquare); Ans = Ans.multiply(BigInteger.valueOf(r).modPow(n, nSquare)); Ans = Ans.remainder(nSquare); return Ans; }
public fraction add(fraction x) { BigInteger gcd = den.gcd(x.den); BigInteger A = x.den.divide(gcd); BigInteger B = den.divide(gcd); BigInteger newNum = num.multiply(A).add(x.num.multiply(B)); BigInteger newDen = den.multiply(A); fraction t = new fraction(newNum, newDen); t.fix(); return t; }
public static void main(String[] args) throws Exception { BigInteger T = BigInteger.valueOf(0); BigInteger TB = BigInteger.valueOf(0); BigInteger NTB = BigInteger.valueOf(0); BigInteger S = BigInteger.valueOf(0); BigInteger MAX = BigInteger.valueOf(1); int j; for (j = 0; j < 100; j++) MAX = MAX.multiply(BigInteger.valueOf(10)); for (; ; ) { int i, t, a, b; if (in.nextToken() != StreamTokenizer.TT_NUMBER) break; t = (int) in.nval; if (in.nextToken() != StreamTokenizer.TT_NUMBER) break; a = (int) in.nval; if (in.nextToken() != StreamTokenizer.TT_NUMBER) break; b = (int) in.nval; // System.out.print("("); // System.out.print(t); // System.out.print("^"); // System.out.print(a); // System.out.print("-1)/("); // System.out.print(t); // System.out.print("^"); // System.out.print(b); // System.out.print("-1) "); if (t == 1 || a % b != 0) { System.out.print("bad!\n"); continue; } T = BigInteger.valueOf(t); TB = BigInteger.valueOf(1); for (i = 0; i < b; i++) { TB = TB.multiply(T); if (TB.compareTo(MAX) >= 0) break; } NTB = BigInteger.valueOf(1); S = BigInteger.valueOf(0); for (i = 0; i < a; i += b) { S = S.add(NTB); if (S.compareTo(MAX) >= 0) break; NTB = NTB.multiply(TB); } if (S.compareTo(MAX) >= 0) System.out.print("bad!"); else System.out.print(S); System.out.print("\n"); } }
public static boolean check(long n, int p) { BigInteger P = BigInteger.TEN.pow(p), Q = BigInteger.TEN.pow(p - 1); BigInteger N = BigInteger.ONE, tmp = BigInteger.valueOf(2); while (n > 0) { if ((n % 2) == 1) { N = N.multiply(tmp); N = N.mod(P); } tmp = tmp.multiply(tmp); tmp = tmp.mod(P); n >>= 1; } N = N.divide(Q); if (N.equals(BigInteger.ONE) || N.equals(BigInteger.valueOf(2))) return true; else return false; }
public static void main(String args[]) throws IOException { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); while (true) { String str = in.readLine(); // Scanner in = new Scanner(System.in); if (str == null) { break; } else { BigInteger num = new BigInteger(str); if (str.equals("0") || str.equals("1")) { System.out.println(str); } else { // BigInteger num = new BigInteger(str); // // System.out.println(num.subtract(BigInteger.ONE).multiply(BigInteger.valueOf(2))); System.out.println(num.multiply(BigInteger.valueOf(2)).subtract(BigInteger.valueOf(2))); } } } }
public String toString(byte[] bytes) { BigInteger mantissa = ZERO; for (int blockoffset = 8; blockoffset >= 0; blockoffset -= 4) { for (int byteidx = 0; byteidx < 4; byteidx++) { mantissa = mantissa.multiply(BYTESHIFT_FACTOR); int idx = blockoffset + byteidx; mantissa = mantissa.add(new BigInteger(String.valueOf(bytes[idx] & 0xff), 10)); } } // The exponent is stored negative by .NET so we change it back here !!! int exponent = -bytes[13] & 0x1f; boolean negative = bytes[12] != 0; BigDecimal result = new BigDecimal(mantissa); if (exponent < 0) { for (int i = exponent; i < 0; i++) { result = result.divide(TEN, BigDecimal.ROUND_HALF_DOWN); } } else { for (int i = 0; i < exponent; i++) { result = result.multiply(TEN); } } if (negative) { result = result.negate(); } return result.toString(); }
public int Decrypto(BigInteger c) { BigInteger nSquare = n.multiply(n); BigInteger Ans = c.modPow(lambda, nSquare); Ans = Ans.subtract(BigInteger.ONE).divide(n); Ans = Ans.remainder(n).multiply(nu).remainder(n); return Ans.intValue(); }
public static void main(String args[]) throws Exception { Scanner cin = new Scanner(System.in); BigInteger s, M; int p, i; while (cin.hasNext()) { p = cin.nextInt(); s = BigInteger.valueOf(4); M = BigInteger.ONE; M = M.shiftLeft(p).subtract(BigInteger.ONE); for (i = 0; i < p - 2; ++i) { s = s.multiply(s).subtract(BigInteger.valueOf(2)); while (s.bitLength() > p) { s = s.shiftRight(p).add(s.and(M)); } } if (s.compareTo(BigInteger.ZERO) == 0 || s.compareTo(M) == 0) { System.out.println(0); continue; } String ans = ""; while (s.compareTo(BigInteger.ZERO) > 0) { long buf = s.mod(BigInteger.valueOf(16)).longValue(); ans += Integer.toHexString((int) buf); s = s.divide(BigInteger.valueOf(16)); } for (i = ans.length() - 1; i >= 0; --i) System.out.print(ans.charAt(i)); System.out.println(); } }
public void KeyGeneration(int bitLengthVal, int certainty, BigInteger[] publicKey) { bitLength = bitLengthVal; /*Constructs two randomly generated positive BigIntegers that are probably prime, with the specified bitLength and certainty.*/ n = publicKey[0]; nsquare = n.multiply(n); g = publicKey[1]; }
public static void main(String[] z) { BigInteger b = BigInteger.ONE; int i = 2, n = new java.util.Scanner(System.in).nextInt(); for (; i < 1501; i++) b = b.multiply(new BigInteger(String.valueOf(i))); System.out.println(b.add(new BigInteger("2"))); for (i = 0; i < n; i++) System.out.println(i + 2); }
public static void main(String[] args) { Scanner s = new Scanner(System.in); BigInteger a = new BigInteger(s.next()); BigInteger b = new BigInteger(s.next()); BigInteger ad = a.add(b); BigInteger mu = a.multiply(b); System.out.println(ad); System.out.println(mu); }
public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner scan = new Scanner(System.in); BigInteger bigInteger1 = scan.nextBigInteger(); BigInteger bigInteger2 = scan.nextBigInteger(); System.out.println(bigInteger1.add(bigInteger2)); System.out.println(bigInteger1.multiply(bigInteger2)); }
public static BigInteger factorial(int i) { BigInteger fact = new BigInteger("1"); while (i > 1) { fact = fact.multiply(new BigInteger(new Integer(i).toString())); i--; } return fact; }
public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextBigInteger()) { BigInteger N = in.nextBigInteger(); if (N.compareTo(BigInteger.ONE) > 0) { BigInteger two = new BigInteger("2"); N = N.multiply(two).subtract(two); } System.out.println(N); } }
public static BigInteger factorial(long n) { BigInteger one = new BigInteger("1"); BigInteger zero = new BigInteger("0"); BigInteger m = new BigInteger((n - 1) + ""); BigInteger result = new BigInteger(n + ""); if (result.equals(one) || result.equals(zero)) { return one; } else { return result.multiply(factorial(n - 1)); } }
void start() throws IOException { BigInteger n = new BigInteger(reader.next()); String s = reader.next(); BigInteger answer = BigInteger.ONE; while (n.compareTo(BigInteger.ONE) > 0) { answer = answer.multiply(n); n = n.subtract(BigInteger.valueOf(s.length())); } writer.println(answer); }
public RSA(Random random) { savedRandom = random; p = getPrime(random); // p and q are arbitrary large primes q = getPrime(random); n = p.multiply(q); phiN = (p.subtract(ONE)).multiply(q.subtract(ONE)); S = getPrime(random); // s is an arbitrary secret; we'll use a prime V = S.modPow(new BigInteger("2"), n); }
public static void main(String[] args) { BigInteger a = BigInteger.valueOf(1), // without OO b = 2, // with OO c1 = a.negate().add(b.multiply(b)).add(b.divide(a)), // without OO c2 = -a + b * b + b / a; // with OO if (c1.compareTo(c2) < 0 || c1.compareTo(c2) > 0) System.out.println("impossible"); // without OO if (c1 < c2 || c1 > c2) System.out.println("impossible"); // with OO HashMap<String, String> map = new HashMap<>(); if (!map.containsKey("qwe")) map.put("qwe", "asd"); // without OO if (map["qwe"] == null) map["qwe"] = "asd"; // with OO }
public static void main(String args[]) { int n, c; BigInteger inc = new BigInteger("1"); BigInteger fact = new BigInteger("1"); Scanner input = new Scanner(System.in); n = input.nextInt(); for (c = 1; c <= n; c++) { fact = fact.multiply(inc); inc = inc.add(BigInteger.ONE); } System.out.println(n + "! = " + fact); }
public static void main(String[] args) { Scanner in = new Scanner(System.in); n = in.nextInt(); l = new BigInteger(in.next()); k = in.nextInt(); m = in.nextInt(); BigInteger w = BigInteger.TEN.pow(m); for (int i = 0; i <= n; ++i) a[i] = new BigInteger(in.next()); for (int i = 0; i < Math.min(k, n + 1); ++i) { t = a[0]; for (int j = 1; j <= n; ++j) { t = t.multiply(l); t = t.add(a[j]); } t = t.mod(w); q[n][i] = t.mod(w); int ret = 0; c = t.toString().toCharArray(); int ll = c.length; for (int j = 0; j < Math.min(m, ll); ++j) { ret += (c[ll - 1 - j] - '0') * (c[ll - 1 - j] - '0'); } System.out.println(ret); l = l.add(BigInteger.ONE); } if (k > n) { for (int i = n - 1; i >= 0; --i) { for (int j = 0; j <= i; j++) q[i][j] = q[i + 1][j + 1].subtract(q[i + 1][j]).mod(w); } for (int i = 1; i <= n; ++i) q[0][i] = q[0][0]; int po = 1; for (int i = n + 1; i < k; ++i) { for (int j = 1; j <= n; ++j) q[j][(po + j) % (n + 1)] = q[j - 1][(po + j - 1) % (n + 1)].add(q[j][(po + j - 1) % (n + 1)]).mod(w); int ret = 0; c = q[n][(po + n) % (n + 1)].mod(w).toString().toCharArray(); int ll = c.length; for (int j = 0; j < Math.min(m, ll); ++j) { ret += (c[ll - 1 - j] - '0') * (c[ll - 1 - j] - '0'); } System.out.println(ret); l = l.add(BigInteger.ONE); ++po; } } }
public String getKDigits(int N, int K) { BigInteger ret = new BigInteger("1"); BigInteger ten = new BigInteger("10"); for (int i = 1; i <= N; i++) { ret = ret.multiply(new BigInteger(new Integer(i).toString())); } System.out.println(ret); while (ret.mod(ten).equals(BigInteger.ZERO)) ret = ret.divide(ten); String ans = ret.toString(); if (ans.length() > K) { ans = ans.substring(ans.length() - K, ans.length()); } return ans; }
public static void main(String[] args) throws IOException { Scanner scanner = new Scanner(new BufferedInputStream(System.in)); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); while (scanner.hasNextInt()) { int n = scanner.nextInt(); int k = scanner.nextInt(); BigInteger sum = BigInteger.ONE; for (int i = n; i > n - k; i--) { sum = sum.multiply(BigInteger.valueOf(i)); } for (int i = 2; i <= k; i++) { sum = sum.divide(BigInteger.valueOf(i)); } System.out.println(sum.toString().length()); } }
public static void main(String[] args) throws IOException { input = new BufferedReader(new InputStreamReader(System.in)); output = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); int cases = Integer.parseInt(read()); for (int i = 0; i < cases; i++) { int value = Integer.parseInt(read()); BigInteger factorial = new BigInteger("" + 1); for (int j = value; j >= 1; j--) { factorial = factorial.multiply(new BigInteger("" + j)); } output.println(factorial); } output.close(); }
private void makeKeys() { PrimeGenerator pg = new PrimeGenerator(513, 10, sr); do { p = pg.getStrongPrime(); } while (p.subtract(BigIntegerMath.ONE).mod(BigIntegerMath.THREE).equals(BigIntegerMath.ZERO)); do { q = pg.getStrongPrime(); } while (q.subtract(BigIntegerMath.ONE).mod(BigIntegerMath.THREE).equals(BigIntegerMath.ZERO)); modulus = p.multiply(q); // Use 3 as enciphering exponent - OK since we are using salt decipherExp = BigIntegerMath.THREE.modInverse( p.subtract(BigIntegerMath.ONE).multiply(q.subtract(BigIntegerMath.ONE))); ciphertextBlockSize = (modulus.bitLength() - 1) / 8 + 1; // Maximum size of plaintext is 6 bytes less than ciphertext // 1 to get under modulus // 4 for the salt // 1 for a pad byte plaintextBlockSize = ciphertextBlockSize - 6; }
/** * XOR in encrypted domain * * @param EncA * @param EncB * @param B * @return 12.03.29 winderif */ public BigInteger EncXOR(BigInteger EncA, BigInteger EncB, BigInteger B) { // [w] = [a + b - 2ab] = [a]*[b]*[a]^(-2b) return EncA.multiply(EncB).multiply(EncA.modPow(B.multiply(TWO).negate(), nsquare)); }
static BigInteger mul(BigInteger x, long y, BigInteger M) { if (y == 0) return BigInteger.ONE; if (y % 2 == 0) return mul(x.multiply(x).mod(M), y / 2, M).mod(M); else return mul(x.multiply(x).mod(M), y / 2, M).multiply(x).mod(M); }