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;
 }
Exemplo n.º 2
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.º 3
0
  static {
    // TODO: DecimalFormat uses ROUND_HALF_EVEN, not ROUND_HALF_UP
    // Float: precision of 7 (6 digits after .)
    floatFormatter = new DecimalFormat();
    floatFormatter.applyPattern("0.######E0");

    // Double: precision of 16 (15 digits after .)
    doubleFormatter = new DecimalFormat();
    doubleFormatter.applyPattern("0.###############E0");

    bigIntTenPow = new BigInteger[20];
    bigIntMinUnscaled = new BigInteger[20];
    bigIntMaxUnscaled = new BigInteger[20];

    for (int i = 0; i < bigIntTenPow.length; i++) {
      bigIntTenPow[i] = bigIntTen.pow(i);
      if (i < 19) {
        bigIntMaxUnscaled[i] = bigIntTenPow[i].subtract(BigInteger.ONE);
        bigIntMinUnscaled[i] = bigIntMaxUnscaled[i].negate();
      } else {
        bigIntMaxUnscaled[i] = BigInteger.valueOf(Long.MAX_VALUE);
        bigIntMinUnscaled[i] = BigInteger.valueOf(Long.MIN_VALUE);
      }
    }
  }
  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.º 5
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.º 6
0
  /** @param args */
  public static void main(String[] args) { // array: 99*99 = 9801;
    BigInteger[] array = new BigInteger[9801];
    int cont = 0;

    for (int i = 2; i < 101; i++) {
      BigInteger dos = new BigInteger("2");
      BigInteger uno = new BigInteger("1");

      for (int j = 2; j < 101; j++) {
        array[cont] = dos.pow(i);
        cont++;
        dos = dos.add(uno);
      }
    }

    for (int x = 0; x < array.length; x++) {
      for (int j = 1; j + x < array.length; j++) {
        int res = array[x].compareTo(array[x + j]);
        if (res == 0) {
          cont--;
          System.out.println("salto!");

          break;
        }
      }
    }
    System.out.println(cont);
  }
Exemplo n.º 7
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;
  }
 public static BigInteger largeSum(ArrayList<BigInteger> nums) {
   BigInteger sum = BigInteger.ZERO;
   for (BigInteger i : nums) {
     sum = sum.add(i);
   }
   return sum;
 }
Exemplo n.º 9
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)));
        }
      }
    }
  }
Exemplo n.º 10
0
  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();
  }
Exemplo n.º 11
0
  public static void testICDE() {
    // Number of total operations
    int numberOfTests = 5;
    // Length of the p, note that n=p.q
    int lengthp = 512;

    Paillier esystem = new Paillier();
    Random rd = new Random();
    PaillierPrivateKey key = KeyGen.PaillierKey(lengthp, 122333356);
    esystem.setDecryptEncrypt(key);
    // let's test our algorithm by encrypting and decrypting few instances

    long start = System.currentTimeMillis();
    for (int i = 0; i < numberOfTests; i++) {
      BigInteger m1 = BigInteger.valueOf(Math.abs(rd.nextLong()));
      BigInteger m2 = BigInteger.valueOf(Math.abs(rd.nextLong()));
      BigInteger c1 = esystem.encrypt(m1);
      BigInteger c2 = esystem.encrypt(m2);
      BigInteger c3 = esystem.multiply(c1, m2);
      c1 = esystem.add(c1, c2);
      c1 = esystem.add(c1, c3);

      esystem.decrypt(c1);
    }
    long stop = System.currentTimeMillis();
    System.out.println(
        "Running time per comparison in milliseconds: " + ((stop - start) / numberOfTests));
  }
Exemplo n.º 12
0
 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 PaillierCryptoSystem(int a, int b) {
   p = a;
   q = b;
   n = BigInteger.valueOf(p).multiply(BigInteger.valueOf(q));
   lambda = psi = lcm(p - 1, q - 1);
   g = n.add(BigInteger.valueOf(1));
   nu = psi.modInverse(n);
 }
Exemplo n.º 14
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.º 15
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;
  }
 public PaillierCryptoSystem() {
   p = 100000 + (new Random().nextInt(4) + 1) * 10000 + 1;
   while (!isPrime(p)) p += 2;
   q = p + 2;
   while (!isPrime(q)) q += 2;
   n = BigInteger.valueOf(p).multiply(BigInteger.valueOf(q));
   lambda = psi = lcm(p - 1, q - 1);
   g = n.add(BigInteger.valueOf(1));
   nu = psi.modInverse(n);
 }
Exemplo n.º 18
0
 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;
 }
Exemplo n.º 19
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.º 20
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.º 21
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);
   }
 }
 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));
   }
 }
Exemplo n.º 23
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.º 24
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.º 25
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.º 26
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.º 27
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.º 28
0
  public static List range(BigInteger start, BigInteger end) {
    List l = new List();

    long st = start.longValue();
    long en = start.longValue();
    if (BigInteger.valueOf(st).equals(start) && BigInteger.valueOf(en).equals(end)) {
      int dir = st < en ? 1 : -1;
      while (st != en) {
        l.add(BigInteger.valueOf(st));
        st = st + dir;
      }
    } else {
      BigInteger dir;
      if (start.compareTo(end) < 0) {
        dir = BigInteger.ONE;
      } else {
        dir = BigInteger.valueOf(-1);
      }
      while (!start.equals(end)) {
        l.add(start);
        start = start.add(dir);
      }
    }

    return l;
  }
Exemplo n.º 29
0
 public static void main(String[] args) {
   Scanner cin = new Scanner(System.in);
   int Case = 0;
   int t = cin.nextInt();
   while (t > 0) {
     t--;
     if (Case != 0) System.out.println();
     ;
     BigInteger a = cin.nextBigInteger();
     BigInteger b = cin.nextBigInteger();
     System.out.println("Case " + (++Case) + ":");
     System.out.println(a + " + " + b + " = " + a.add(b));
   }
 }
Exemplo n.º 30
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);
  }