示例#1
1
 public BigInteger count(int n) {
   BigInteger[] prev = new BigInteger[k];
   Arrays.fill(prev, BigInteger.ZERO);
   prev[s] = BigInteger.ONE;
   while (n-- > 0) {
     BigInteger[] next = new BigInteger[k];
     Arrays.fill(next, BigInteger.ZERO);
     for (int i = 0; i < k; ++i) {
       if (prev[i].signum() <= 0) {
         continue;
       }
       for (int j = 0; j < z; ++j) {
         if (e[j][i] < 0) {
           continue;
         }
         next[e[j][i]] = next[e[j][i]].add(prev[i]);
       }
     }
     prev = next;
   }
   BigInteger ans = BigInteger.ZERO;
   for (int term : t) {
     ans = ans.add(prev[term]);
   }
   return ans;
 }
示例#2
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;
 }
 public static BigInteger largeSum(ArrayList<BigInteger> nums) {
   BigInteger sum = BigInteger.ZERO;
   for (BigInteger i : nums) {
     sum = sum.add(i);
   }
   return sum;
 }
示例#4
0
 BigInteger getResult(int n, int r) {
   BigInteger ans;
   ans = F[n];
   ans = ans.divide(F[r]);
   ans = ans.divide(F[n - r]);
   return ans;
 }
  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 static void countPairs(Integer[] arr) {

    HashMap<Integer, Integer> seenElements = new HashMap<Integer, Integer>();

    // ArrayList<Integer> countList = new ArrayList<Integer

    for (int i = 0; i < arr.length; i++) {

      if (!seenElements.containsKey(arr[i])) {
        seenElements.put(arr[i], 1);
      } else {

        seenElements.put(arr[i], seenElements.get(arr[i]) + 1);
      }
    }

    BigInteger count = null;
    for (int element : seenElements.keySet()) {
      if (seenElements.get(element) > 1) {
        System.out.println("count is :" + seenElements.get(element));
        count.add(
            factorial(seenElements.get(element)).divide(factorial(seenElements.get(element) - 2)));
        System.out.println(count);
      }
    }

    if (count == null) {
      System.out.println("0");
    }
  }
示例#7
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);
 }
示例#8
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;
  }
示例#10
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);
   }
 }
示例#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);
  }
示例#12
0
 public static void main(String args[]) {
   Scanner cin = new Scanner(System.in);
   BigInteger a = BigInteger.ONE;
   int cas = 0;
   while (true) {
     cas++;
     a = cin.nextBigInteger();
     if (a.compareTo(BigInteger.ZERO) == 0) break;
     long ans = calc(a);
     System.out.println("Case " + cas + ": Public Key = " + a + " Private Key = " + ans);
   }
 }
示例#13
0
 void solve(int caseNum) {
   int n = in.nextInt();
   BigInteger[] past = new BigInteger[n], d = new BigInteger[n];
   for (int i = 0; i < n; i++) past[i] = new BigInteger(in.next());
   Arrays.sort(past);
   for (int i = 0; i < n; i++) d[i] = past[i].subtract(past[0]);
   for (int i = 2; i < n; i++) d[i] = d[i].gcd(d[i - 1]);
   BigInteger gcd = d[n - 1], mod = past[0].mod(gcd);
   System.out.printf("Case #%d: ", caseNum);
   if (mod.equals(BigInteger.ZERO)) System.out.println(0);
   else System.out.println(gcd.subtract(mod));
 }
  public void testReferenceLinkAttribute() {
    Session ssn = SessionManager.getSession();
    DataObject user = ssn.create(getModelName() + ".User");
    user.set("id", BigInteger.valueOf(0));
    user.set("email", "*****@*****.**");
    user.set("firstName", "foo");
    user.set("lastNames", "bar");
    user.save();

    DataObject[] images = new DataObject[2];
    for (int i = 0; i < images.length; i++) {
      images[i] = ssn.create(getModelName() + ".Image");
      images[i].set("id", BigInteger.valueOf(i));
      byte[] bytes = "This is the image.".getBytes();
      images[i].set("bytes", bytes);
      images[i].save();
    }

    // set image
    user.set("image", images[0]);
    user.save();

    // retrieve and then update caption
    DataAssociationCursor dac = ((DataAssociation) images[0].get("users")).cursor();
    dac.next();
    assertNull(dac.getLinkProperty("caption"));
    assertEquals(user, dac.getDataObject());
    DataObject link = dac.getLink();
    link.set("caption", "caption");
    link.save();

    dac = ((DataAssociation) images[0].get("users")).cursor();
    dac.next();
    assertEquals("caption", dac.getLinkProperty("caption"));
    assertEquals(1L, ((DataAssociation) images[0].get("users")).size());

    // set new image as image
    user.set("image", images[1]);
    user.save();

    // check that old image is no longer associated with user
    assertEquals(0L, ((DataAssociation) images[0].get("users")).size());

    // check that new image is associated with user and has no caption
    dac = ((DataAssociation) images[1].get("users")).cursor();
    dac.next();
    assertNull(dac.getLinkProperty("caption"));
    assertEquals(1L, ((DataAssociation) images[1].get("users")).size());
  }
示例#15
0
文件: uva619.java 项目: de-yu/uva
 public static void abcd(String str) {
   BigInteger sum = BigInteger.ZERO;
   StringBuffer n = new StringBuffer("");
   for (int i = 0; i < str.length(); i++) {
     BigInteger ab = BigInteger.valueOf(((int) str.charAt(i)) - 96);
     sum = sum.add(div.pow(str.length() - i - 1).multiply(ab));
   }
   String s = sum.toString();
   for (int i = 0; i < s.length(); i++) {
     if (i % 3 == 0 && i != 0) n.insert(0, ",");
     n.insert(0, s.charAt(s.length() - i - 1));
   }
   while (str.length() < 22) str = str + " ";
   System.out.println(str + "" + n);
 }
示例#16
0
  void solve(InputReader in) {

    try {

      long start = System.currentTimeMillis();

      F[0] = BigInteger.ONE;
      for (i = 1; i <= 30; ++i) F[i] = F[i - 1].multiply(BigInteger.valueOf(i));

      for (i = 1; i <= 30; ++i) {
        for (j = 1; j <= i; ++j) {
          C[i][j] = getResult(i, j);
        }
      }

      t = in.nextInt();
      while (t-- > 0) {
        for (i = 1; i <= 30; ++i) g[i] = new ArrayList();
        n = in.nextInt();
        for (i = 1; i <= n; ++i) a[i] = in.nextInt();
        root = a[1];
        p = 2;
        Construct(root, 1, 30);
        Pair ans = Calculate(root);
        System.out.println(ans.x);
      }

      long end = System.currentTimeMillis();
      if (time) System.out.println("Execution time: " + (double) (end - start) / 1000.0);
    } catch (Exception e) {
      System.out.println("Error: " + e);
    }
    ;
  }
示例#17
0
  static byte[] buildDSA(DSAPublicKey key) {
    DataByteOutputStream out = new DataByteOutputStream();
    BigInteger q = key.getParams().getQ();
    BigInteger p = key.getParams().getP();
    BigInteger g = key.getParams().getG();
    BigInteger y = key.getY();
    int t = (p.toByteArray().length - 64) / 8;

    out.writeByte(t);
    out.writeBigInteger(q);
    out.writeBigInteger(p);
    out.writeBigInteger(g);
    out.writeBigInteger(y);

    return out.toByteArray();
  }
示例#18
0
文件: uva619.java 项目: de-yu/uva
 public static void num(String str) {
   BigInteger a = new BigInteger(str);
   StringBuffer sb = new StringBuffer("");
   StringBuffer n = new StringBuffer("");
   while (!(a.divide(div).equals(zero))) {
     sb.insert(0, (char) (a.mod(div).intValue() + 96));
     a = a.divide(div);
   }
   sb.insert(0, (char) (a.intValue() + 96));
   for (int i = 0; i < str.length(); i++) {
     if (i % 3 == 0 && i != 0) n.insert(0, ",");
     n.insert(0, str.charAt(str.length() - i - 1));
   }
   while (sb.length() < 22) sb.append(" ");
   System.out.println(sb + "" + n);
 }
示例#19
0
 /**
  * Writes a BigInteger to the stream, encoded as binary data. If present, the leading 0 byte is
  * removed.
  *
  * @param i The BigInteger to be written
  */
 public void writeBigInteger(BigInteger i) {
   byte[] b = i.toByteArray();
   try {
     if (b[0] == 0) write(b, 1, b.length - 1);
     else write(b);
   } catch (IOException e) {
   }
 }
示例#20
0
  public static void main(String[] args) {
    PrintWriter pw;
    Scanner sc;
    try {
      sc = new Scanner(new File("input.txt"));
      BigInteger a, b, c;
      a = sc.nextBigInteger();
      b = sc.nextBigInteger();
      c = a.divide(b);
      // System.out.println(firstTeam+" "+secondTeam);
      pw = new PrintWriter(new File("output.txt"));
      pw.print(c);

      pw.close();
    } catch (IOException e) {
    }
  }
示例#21
0
  private void decryptTabelEntry(BigInteger[] cLabels) {
    int col = 0;

    for (int i = 0; i < nCIBits; i++) if (cLabels[i].testBit(0)) col |= (1 << i);

    BigInteger target = EGTable[col];

    BigInteger colKey = BigInteger.ZERO;
    for (int k = nCIBits - 1; k >= 0; k--)
      colKey = colKey.shiftLeft(Wire.labelBitLength).xor(cLabels[k]);

    BigInteger res = Cipher.decrypt(colKey, target, nBits * Wire.labelBitLength);

    BigInteger mask = BigInteger.ONE.shiftLeft(Wire.labelBitLength).subtract(BigInteger.ONE);
    for (int k = nBits - 1; k >= 0; k--)
      outputLabels[k] = res.shiftRight(Wire.labelBitLength * k).and(mask);
  }
示例#22
0
 static long calc(BigInteger k1) {
   long x = 1;
   while (true) {
     long res = mul(k1, x, BigInteger.valueOf(1000000000000l)).longValue();
     if (x == res && res > 100000000000l) return res;
     x = res;
   }
 }
示例#23
0
文件: C.java 项目: kohyatoh/contests
 public static int turn(int p, int ix) {
     char c = a.charAt(ix);
     if ('0' <= c && c <= '9') {
         scs[p] = scs[p].add(BigInteger.valueOf((int)(c - '0')));
     }
     else {
         char op = c;
         while (!('0' <= c && c <= '9')) {
             ix++;
             if (ix == a.length()) return ix;
             c = a.charAt(ix);
         }
         if (op == 'X') scs[p] = scs[p].multiply(BigInteger.valueOf((int)(c - '0')));
         if (op == 'D') scs[p] = scs[p].divide(BigInteger.valueOf((int)(c - '0')));
         if (op == 'S') scs[p] = scs[p].subtract(BigInteger.valueOf((int)(c - '0')));
     }
     return ix + 1;
 }
示例#24
0
文件: uva10925.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));

    int n = 1;
    while (scanner.hasNextInt()) {
      int a = scanner.nextInt();
      int b = scanner.nextInt();
      if (a == 0) break;
      BigInteger sum = BigInteger.ZERO;
      BigInteger bb = BigInteger.valueOf(b);
      for (int i = 0; i < a; i++) sum = sum.add(scanner.nextBigInteger());

      System.out.println(
          "Bill #" + n + " costs " + sum + ": each friend should pay " + sum.divide(bb) + "\n");
      n++;
    }
  }
示例#25
0
 private void exchangeKeys() {
   try {
     output.write(modulus.toByteArray());
     byte[] buffer = new byte[ciphertextBlockSize];
     input.read(buffer);
     recipModulus = new BigInteger(1, buffer);
   } catch (IOException ioe) {
     System.err.println("Error establishing keys");
   }
 }
示例#26
0
  /** @param args the command line arguments */
  public static void main(String[] args) {

    // Takes user input and converts into a character array
    Scanner sc = new Scanner(System.in);
    int n = Integer.parseInt(sc.nextLine());
    String input = sc.nextLine();
    String[] a = input.split(" ");

    BigInteger[] storage = new BigInteger[n];
    BigInteger sum = new BigInteger("0");

    // Converts and Stores Big Integers
    for (int i = 0; i < n; i++) storage[i] = new BigInteger(a[i]);

    // Sums the BigIntegers in storage
    for (int j = 0; j < n; j++) sum = sum.add(storage[j]);

    // Outputs the sum of values in storage as a string
    System.out.println(sum);
  }
  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();
  }
示例#28
0
 public void DP() {
   for (int i = 0; i <= m; i++) {
     for (int j = 0; j <= sz; j++) {
       dp[i][j] = BigInteger.ZERO;
     }
   }
   dp[0][0] = BigInteger.ONE;
   for (int i = 0; i < m; i++) {
     for (int j = 0; j < sz; j++)
       if (dp[i][j].compareTo(BigInteger.valueOf(0)) == 1) {
         for (int k = 0; k < CD; k++) {
           if (flag[ch[j][k]] > 0 || flag[j] > 0) continue;
           dp[i + 1][ch[j][k]] = dp[i + 1][ch[j][k]].add(dp[i][j]);
         }
       }
   }
   ans = BigInteger.ZERO;
   for (int i = 0; i < sz; i++) ans = ans.add(dp[m][i]);
   out.println(ans);
 }
示例#29
0
  public static String md5(File file)
      throws NoSuchAlgorithmException, FileNotFoundException, IOException {
    MessageDigest md = MessageDigest.getInstance("MD5");

    InputStream is = new FileInputStream(file);

    byte[] buffer = new byte[8192];
    int read = 0;

    while ((read = is.read(buffer)) > 0) md.update(buffer, 0, read);

    byte[] md5 = md.digest();
    BigInteger bi = new BigInteger(1, md5);

    is.close();
    String hex = bi.toString(16);
    while (hex.length() < 32) {
      hex = "0" + hex;
    } // Padding
    return hex;
  }
示例#30
0
  static byte[] buildDH(DHPublicKey key) {
    DataByteOutputStream out = new DataByteOutputStream();
    BigInteger p = key.getParams().getP();
    BigInteger g = key.getParams().getG();
    BigInteger y = key.getY();

    int pLength, gLength, yLength;
    if (g.equals(TWO) && (p.equals(DHPRIME768) || p.equals(DHPRIME1024))) {
      pLength = 1;
      gLength = 0;
    } else {
      pLength = BigIntegerLength(p);
      gLength = BigIntegerLength(g);
    }
    yLength = BigIntegerLength(y);

    out.writeShort(pLength);
    if (pLength == 1) {
      if (p.bitLength() == 768) out.writeByte((byte) 1);
      else out.writeByte((byte) 2);
    } else out.writeBigInteger(p);
    out.writeShort(gLength);
    if (gLength > 0) out.writeBigInteger(g);
    out.writeShort(yLength);
    out.writeBigInteger(y);

    return out.toByteArray();
  }