예제 #1
0
 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;
 }
예제 #2
0
  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");
    }
  }
예제 #3
0
  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)));
        }
      }
    }
  }
예제 #4
0
파일: Main.java 프로젝트: mrain/acm
 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;
 }
예제 #5
0
 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();
   }
 }
예제 #6
0
 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);
 }
예제 #7
0
  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;
  }
예제 #9
0
 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);
   }
 }
예제 #10
0
  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);
  }
예제 #11
0
  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) 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();
  }
예제 #13
0
 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;
 }
예제 #14
0
파일: uva10219.java 프로젝트: de-yu/uva
  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());
    }
  }
예제 #15
0
 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);
 }