Exemplo n.º 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;
 }
 public static BigInteger largeSum(ArrayList<BigInteger> nums) {
   BigInteger sum = BigInteger.ZERO;
   for (BigInteger i : nums) {
     sum = sum.add(i);
   }
   return sum;
 }
Exemplo n.º 3
0
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println(3 ^ 5 ^ 4 ^ 5 ^ 5 ^ 6 ^ 6 ^ 7);
    System.out.println(3 ^ 7);

    int[] a = new int[10];
    for (int i = 0; i < 10; i++) a[i] = input.nextInt();
    for (int aa : a) {
      System.out.print(aa);
    }
    sss(1, 3, 4, 5, 6, 7, 8, 9);

    BigInteger aa =
        new BigInteger(
            "12809348038290834905895869048359034859083495894038590438590348590834905890345");
    BigInteger bb = new BigInteger("111111111111111111111");
    System.out.println(aa.subtract(bb));
    BigDecimal rrrr = new BigDecimal(input.next());
    int aaa;
    double b;
    float c;
    StringBuilder sssss = new StringBuilder("1234234");
    System.out.println();

    Integer ttt = 4;
    Double t1 = 14.2;
  }
Exemplo n.º 4
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;
 }
Exemplo n.º 5
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;
 }
Exemplo n.º 6
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)));
        }
      }
    }
  }
  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");
    }
  }
Exemplo n.º 8
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);
 }
Exemplo n.º 9
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;
  }
Exemplo n.º 11
0
 private void constrain() {
   BigInteger offset = new BigInteger(Integer.toString(addressSize * (numUsableRows)));
   BigInteger endVal = startVal.add(offset);
   if (endVal.compareTo(scrollBar.getMaximumHP()) > 0) {
     startVal = scrollBar.getMaximumHP().subtract(offset);
     endVal = scrollBar.getMaximumHP();
     scrollBar.setValueHP(startVal);
     model.fireTableDataChanged();
   }
 }
Exemplo n.º 12
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);
  }
Exemplo n.º 13
0
 private void updateFromScrollBar() {
   beginUpdate();
   BigInteger oldStartVal = startVal;
   startVal = scrollBar.getValueHP();
   constrain();
   model.fireTableDataChanged();
   if (oldStartVal != null) {
     modifySelection(oldStartVal.subtract(startVal).intValue() / addressSize);
   }
   endUpdate();
 }
Exemplo n.º 14
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);
   }
 }
Exemplo n.º 15
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));
 }
Exemplo n.º 16
0
Arquivo: 009.java Projeto: kyokey/MIPT
 void work() {
   cin = new Scanner(System.in);
   int n = cin.nextInt();
   BigInteger a = BigInteger.ONE;
   BigInteger b = BigInteger.ONE;
   n -= 1;
   for (int i = 0; i < n; i++) {
     BigInteger c = a.add(b);
     a = b;
     b = c;
   }
   System.out.println(b.toString());
 }
Exemplo n.º 17
0
  public static void main(String[] args) {
    // Generics, varargs & boxing working together:
    List<Integer> li = Arrays.asList(1, 2, 3, 4, 5, 6, 7);
    Integer result = reduce(li, new IntegerAdder());
    print(result);

    result = reduce(li, new IntegerSubtracter());
    print(result);

    print(filter(li, new GreaterThan<Integer>(4)));

    print(forEach(li, new MultiplyingIntegerCollector()).result());

    print(
        forEach(filter(li, new GreaterThan<Integer>(4)), new MultiplyingIntegerCollector())
            .result());

    MathContext mc = new MathContext(7);
    List<BigDecimal> lbd =
        Arrays.asList(
            new BigDecimal(1.1, mc),
            new BigDecimal(2.2, mc),
            new BigDecimal(3.3, mc),
            new BigDecimal(4.4, mc));
    BigDecimal rbd = reduce(lbd, new BigDecimalAdder());
    print(rbd);

    print(filter(lbd, new GreaterThan<BigDecimal>(new BigDecimal(3))));

    // Use the prime-generation facility of BigInteger:
    List<BigInteger> lbi = new ArrayList<BigInteger>();
    BigInteger bi = BigInteger.valueOf(11);
    for (int i = 0; i < 11; i++) {
      lbi.add(bi);
      bi = bi.nextProbablePrime();
    }
    print(lbi);

    BigInteger rbi = reduce(lbi, new BigIntegerAdder());
    print(rbi);
    // The sum of this list of primes is also prime:
    print(rbi.isProbablePrime(5));

    List<AtomicLong> lal =
        Arrays.asList(
            new AtomicLong(11), new AtomicLong(47), new AtomicLong(74), new AtomicLong(133));
    AtomicLong ral = reduce(lal, new AtomicLongAdder());
    print(ral);

    print(transform(lbd, new BigDecimalUlp()));
  }
Exemplo n.º 18
0
  public static void main(String args[]) {

    Scanner in = new Scanner(System.in);
    int T;
    BigInteger sum = new BigInteger("0");
    BigInteger aux = new BigInteger("0");

    T = in.nextInt();
    for (int i = 0; i < T; i++) {
      aux = in.nextBigInteger();
      sum = sum.add(aux);
    }
    System.out.println(sum);
  }
  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());
  }
Exemplo n.º 20
0
 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);
 }
Exemplo n.º 21
0
  public static void main(String[] args) {
    BigInteger pCorr = new BigInteger("2");
    BigInteger pInc = new BigInteger("1");
    BigInteger incorrects = new BigInteger("0");

    // loop calculates number of hads in each incorrect iteration
    for (int x = 0; x < 100; x++) {
      // 6 + 2*pInc + pCorr
      pInc = pInc.pCorr = place;
      System.out.println(place + "");
      incorrects.add(new BigInteger(place + ""));
    }

    System.out.println(incorrects);
  }
Exemplo n.º 22
0
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    boolean blank = false;

    while (scanner.hasNextBigInteger()) {
      if (blank) {
        System.out.println();
      } else {
        blank = true;
      }
      BigInteger a = scanner.nextBigInteger();
      BigInteger b = scanner.nextBigInteger();
      System.out.println(a.gcd(b));
    }
  }
 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];
 }
Exemplo n.º 24
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);
    }
    ;
  }
Exemplo n.º 25
0
 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);
 }
Exemplo n.º 26
0
  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);
  }
Exemplo n.º 27
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) {
    }
  }
Exemplo n.º 28
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;
   }
 }
Exemplo n.º 29
0
  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
  }
Exemplo n.º 30
0
  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++;
    }
  }