Esempio n. 1
0
File: Main.java Progetto: 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;
 }
Esempio n. 2
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();
   }
 }
Esempio n. 3
0
 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;
     }
   }
 }
Esempio n. 4
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);
 }
Esempio n. 5
0
  public static void main(String args[]) {
    int t;
    Scanner s = new Scanner(System.in);
    t = s.nextInt();

    BigInteger b1 = new BigInteger("0");
    int b2;
    BigInteger b3 = new BigInteger("0");
    BigInteger b4 = new BigInteger("10");
    while (t > 0) {
      b1 = s.nextBigInteger();
      b2 = s.nextInt();
      b3 = b1.pow(b2);
      System.out.println(b3.mod(b4));
      t--;
    }
    s.close();
  }
  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;
  }
Esempio n. 7
0
  /** This main method basically tests the different features of the Paillier encryption */
  public static void test() {
    Random rd = new Random();
    long num = 0;
    long num1 = 0;
    BigInteger m = null;
    BigInteger c = null;
    int numberOfTests = 10;
    int j = 0;
    BigInteger decryption = null;
    Paillier esystem = new Paillier();
    PaillierPrivateKey key = KeyGen.PaillierKey(512, 122333356);
    esystem.setDecryptEncrypt(key);
    // let's test our algorithm by encrypting and decrypting a few instances
    for (int i = 0; i < numberOfTests; i++) {
      num = Math.abs(rd.nextLong());
      m = BigInteger.valueOf(num);
      System.out.println("number chosen  : " + m.toString());
      c = esystem.encrypt(m);
      System.out.println("encrypted value: " + c.toString());
      decryption = esystem.decrypt(c);
      System.out.println("decrypted value: " + decryption.toString());
      if (m.compareTo(decryption) == 0) {
        System.out.println("OK");
        j++;
      } else System.out.println("PROBLEM");
    }
    System.out.println(
        "out of "
            + (new Integer(numberOfTests)).toString()
            + "random encryption,# many of "
            + (new Integer(j)).toString()
            + " has passed");
    // Let us check the commutative properties of the paillier encryption
    System.out.println("Checking the additive properteries of the Paillier encryption");
    //   Obviously 1+0=1
    System.out.println(
        "1+0="
            + (esystem.decrypt(esystem.add(esystem.encryptone(), esystem.encryptzero())))
                .toString());
    // 1+1=2
    System.out.println(
        "1+1="
            + (esystem.decrypt(
                    esystem.add(esystem.encrypt(BigInteger.ONE), esystem.encrypt(BigInteger.ONE))))
                .toString());

    // 1+1+1=3
    System.out.println(
        "1+1+1="
            + (esystem.decrypt(
                    esystem.add(
                        esystem.add(
                            esystem.encrypt(BigInteger.ONE), esystem.encrypt(BigInteger.ONE)),
                        esystem.encrypt(BigInteger.ONE))))
                .toString());

    // 0+0=0
    System.out.println(
        "0+0="
            + (esystem.decrypt(
                    esystem.add(
                        esystem.encrypt(BigInteger.ZERO), esystem.encrypt(BigInteger.ZERO))))
                .toString());
    // 1+-1=0
    System.out.println(
        "1+-1="
            + (esystem.decrypt(
                    esystem.add(
                        esystem.encrypt(BigInteger.valueOf(-1).mod(key.getN())),
                        esystem.encrypt(BigInteger.ONE))))
                .toString());

    do {
      num = rd.nextLong();
    } while (key.inModN(BigInteger.valueOf(num)) == false);
    do {
      num1 = rd.nextLong();
    } while (key.inModN(BigInteger.valueOf(num1)) == false);
    BigInteger numplusnum1 = BigInteger.valueOf(num).add(BigInteger.valueOf(num1));
    BigInteger summodnsquare = numplusnum1.mod(key.getN());
    // D(E(num)+E(num1))=num+num1
    System.out.println(numplusnum1.toString());
    System.out.println(
        summodnsquare.toString()
            + "=\n"
            + esystem
                .decrypt(
                    esystem.add(
                        esystem.encrypt(BigInteger.valueOf(num)),
                        esystem.encrypt(BigInteger.valueOf(num1))))
                .toString());
    // Let us check the multiplicative properties
    System.out.println("Checking the multiplicative properties");
    // D(multiply(E(2),3))=6
    System.out.println(
        "6="
            + esystem.decrypt(
                esystem.multiply(
                    esystem.add(esystem.encrypt(BigInteger.ONE), esystem.encrypt(BigInteger.ONE)),
                    3)));
  }