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(); } }
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 process() { Arrays.sort(bookHeight); BigInteger sum = BigInteger.valueOf(0L); BigInteger temp; int iCount = 0; for (int i = bookHeight.length - 1; i >= 0; i--) { sum = sum.add(BigInteger.valueOf((long) (bookHeight[i]))); temp = sum.max(b); iCount++; if (temp.equals(sum)) break; } System.out.print(iCount); }
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()); }
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"); } }
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); } ; }
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; }
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; } }
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; }
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()); } }
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); }
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); }
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++; } }
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); }
void run() { int N = input.nextInt(); f = new BigInteger[max(N + 1, 5)]; f[1] = BigInteger.ZERO; f[2] = BigInteger.valueOf(2); f[3] = BigInteger.valueOf(4); g = new BigInteger[max(N + 1, 5)]; g[1] = BigInteger.valueOf(2); g[2] = BigInteger.valueOf(2); g[3] = BigInteger.valueOf(8); for (int i = 4; i <= N; ++i) { g[i] = f[i - 1].multiply(BigInteger.valueOf(2)).add(g[i - 1]).add(g[i - 2]).subtract(g[i - 3]); f[i] = f[i - 1].add(g[i - 1]); } System.out.println(f[N]); }
public class Main { /** * Static variables for 1024 bit Diffie-Hellman algorithm. * * <p>This is required to have matching moduli between client and server. */ private static final byte SKIP_1024_MODULUS_BYTES[] = { (byte) 0xF4, (byte) 0x88, (byte) 0xFD, (byte) 0x58, (byte) 0x4E, (byte) 0x49, (byte) 0xDB, (byte) 0xCD, (byte) 0x20, (byte) 0xB4, (byte) 0x9D, (byte) 0xE4, (byte) 0x91, (byte) 0x07, (byte) 0x36, (byte) 0x6B, (byte) 0x33, (byte) 0x6C, (byte) 0x38, (byte) 0x0D, (byte) 0x45, (byte) 0x1D, (byte) 0x0F, (byte) 0x7C, (byte) 0x88, (byte) 0xB3, (byte) 0x1C, (byte) 0x7C, (byte) 0x5B, (byte) 0x2D, (byte) 0x8E, (byte) 0xF6, (byte) 0xF3, (byte) 0xC9, (byte) 0x23, (byte) 0xC0, (byte) 0x43, (byte) 0xF0, (byte) 0xA5, (byte) 0x5B, (byte) 0x18, (byte) 0x8D, (byte) 0x8E, (byte) 0xBB, (byte) 0x55, (byte) 0x8C, (byte) 0xB8, (byte) 0x5D, (byte) 0x38, (byte) 0xD3, (byte) 0x34, (byte) 0xFD, (byte) 0x7C, (byte) 0x17, (byte) 0x57, (byte) 0x43, (byte) 0xA3, (byte) 0x1D, (byte) 0x18, (byte) 0x6C, (byte) 0xDE, (byte) 0x33, (byte) 0x21, (byte) 0x2C, (byte) 0xB5, (byte) 0x2A, (byte) 0xFF, (byte) 0x3C, (byte) 0xE1, (byte) 0xB1, (byte) 0x29, (byte) 0x40, (byte) 0x18, (byte) 0x11, (byte) 0x8D, (byte) 0x7C, (byte) 0x84, (byte) 0xA7, (byte) 0x0A, (byte) 0x72, (byte) 0xD6, (byte) 0x86, (byte) 0xC4, (byte) 0x03, (byte) 0x19, (byte) 0xC8, (byte) 0x07, (byte) 0x29, (byte) 0x7A, (byte) 0xCA, (byte) 0x95, (byte) 0x0C, (byte) 0xD9, (byte) 0x96, (byte) 0x9F, (byte) 0xAB, (byte) 0xD0, (byte) 0x0A, (byte) 0x50, (byte) 0x9B, (byte) 0x02, (byte) 0x46, (byte) 0xD3, (byte) 0x08, (byte) 0x3D, (byte) 0x66, (byte) 0xA4, (byte) 0x5D, (byte) 0x41, (byte) 0x9F, (byte) 0x9C, (byte) 0x7C, (byte) 0xBD, (byte) 0x89, (byte) 0x4B, (byte) 0x22, (byte) 0x19, (byte) 0x26, (byte) 0xBA, (byte) 0xAB, (byte) 0xA2, (byte) 0x5E, (byte) 0xC3, (byte) 0x55, (byte) 0xE9, (byte) 0x2F, (byte) 0x78, (byte) 0xC7 }; // In oder to use skip, we need a BigInteger representation of that modulus private static final BigInteger MODULUS = new BigInteger(1, SKIP_1024_MODULUS_BYTES); // We also need a base for Dittie-Hellman, which SKIP defines as 2 private static final BigInteger BASE = BigInteger.valueOf(2); // we can wrap those two SKIP parameter into one DHParamterSpec, which we'll use to initialize our // keyAgreement latger private static final DHParameterSpec PARAMETER_SPEC = new DHParameterSpec(MODULUS, BASE); public static void main(String[] args) throws Exception { // prompt user to enter a port number System.out.print("Enter the port number: "); Scanner scan = new Scanner(System.in); int port = scan.nextInt(); scan.nextLine(); System.out.print("Enter the host name: "); String hostName = scan.nextLine(); // Initialize a key pair generator with the SKIP parameters we sepcified, and genrating a pair // This will take a while: 5...15 seconrds System.out.println("Generating a Diffie-Hellman keypair: "); KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH"); kpg.initialize(PARAMETER_SPEC); KeyPair keyPair = kpg.genKeyPair(); System.out.println("key pair has been made..."); // one the key pair has been generated, we want to listen on // a given port for a connection to come in // once we get a connection, we will get two streams, One for input // and one for output // open a port and wait for a connection ServerSocket ss = new ServerSocket(port); System.out.println("Listeining on port " + port + " ..."); Socket socket = ss.accept(); // use to output and input primitive data type DataOutputStream out = new DataOutputStream(socket.getOutputStream()); // next thing to do is send our public key and receive client's // this corresponds to server step 3 and step 4 in the diagram System.out.println("Sending my public key..."); byte[] keyBytes = keyPair.getPublic().getEncoded(); out.writeInt(keyBytes.length); out.write(keyBytes); System.out.println("Server public key bytes: " + CryptoUtils.toHex(keyBytes)); // receive the client's public key System.out.println("Receiving client's public key..."); DataInputStream in = new DataInputStream(socket.getInputStream()); keyBytes = new byte[in.readInt()]; in.readFully(keyBytes); // create client's public key KeyFactory kf = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509Spec = new X509EncodedKeySpec(keyBytes); PublicKey clientPublicKey = kf.generatePublic(x509Spec); // print out client's public key bytes System.out.println( "Client public key bytes: " + CryptoUtils.toHex(clientPublicKey.getEncoded())); // we can now use the client's public key and // our own private key to perform the key agreement System.out.println("Performing the key agreement ... "); KeyAgreement ka = KeyAgreement.getInstance("DH"); ka.init(keyPair.getPrivate()); ka.doPhase(clientPublicKey, true); // in a chat application, each character is sendt over the wire, separetly encrypted, // Instead of using ECB, we are goin to use CFB, with a block size of 8 bits(1byte) // to send each character. We will encrypt the same character in a different way // each time. But in order to use CFB8, we need an IVof 8 bytes. We will create // that IV randomly and and send it to the client. It doesn't matter if somoene // eavesdrops on the IV when it is sent over the wire. it's not sensitive info // creating the IV and sending it corresponds to step 6 and 7 byte[] iv = new byte[8]; SecureRandom sr = new SecureRandom(); sr.nextBytes(iv); out.write(iv); // we generate the secret byte array we share with the client and use it // to create the session key (Step 8) byte[] sessionKeyBytes = ka.generateSecret(); // create the session key SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede"); DESedeKeySpec DESedeSpec = new DESedeKeySpec(sessionKeyBytes); SecretKey sessionKey = skf.generateSecret(DESedeSpec); // printout session key bytes System.out.println("Session key bytes: " + CryptoUtils.toHex(sessionKey.getEncoded())); // now use tha that session key and IV to create a CipherInputStream. We will use them to read // all character // that are sent to us by the client System.out.println("Creating the cipher stream ..."); Cipher decrypter = Cipher.getInstance("DESede/CFB8/NoPadding"); IvParameterSpec spec = new IvParameterSpec(iv); decrypter.init(Cipher.DECRYPT_MODE, sessionKey, spec); CipherInputStream cipherIn = new CipherInputStream(socket.getInputStream(), decrypter); // we just keep reading the input and print int to the screen, until -1 sent over int theCharacter = 0; theCharacter = cipherIn.read(); while (theCharacter != -1) { System.out.print((char) theCharacter); theCharacter = cipherIn.read(); } // once -1 is received we want to close up our stream and exit cipherIn.close(); in.close(); out.close(); socket.close(); } }
private static BigInteger bi(long n1) { return BigInteger.valueOf(n1); }
long gcd(long a, long b) { BigInteger x = BigInteger.valueOf(a); BigInteger y = BigInteger.valueOf(b); return x.gcd(y).longValue(); }
public void testDeepLink() { Session ssn = SessionManager.getSession(); DataObject[] users = new DataObject[4]; DataObject group = getSession().create(getModelName() + ".Group"); group.set("id", BigInteger.valueOf(users.length)); group.set("email", "*****@*****.**"); group.set("name", "SIPB"); group.save(); DataAssociation members = (DataAssociation) group.get("members"); for (int i = 0; i < users.length; i++) { users[i] = ssn.create(getModelName() + ".User"); users[i].set("id", BigInteger.valueOf(i)); users[i].set("email", "*****@*****.**"); users[i].set("firstName", "foo"); users[i].set("lastNames", "bar"); users[i].save(); members.add(users[i]); } group.save(); DataObject[] images = new DataObject[users.length / 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(); } // create link between user i and image i/2 with caption i for (int i = 0; i < users.length; i++) { // set image DataAssociation imageUsers = (DataAssociation) images[i / 2].get("users"); DataObject link = imageUsers.add(users[i]); link.set("caption", String.valueOf(i)); link.save(); } DataCollection dc = ssn.retrieve(getModelName() + ".Group"); dc.addEqualsFilter("members.image.link.caption", "0"); assertEquals(1, dc.size()); dc = ssn.retrieve(getModelName() + ".User"); dc.addPath("image.link.caption"); assertEquals(users.length, dc.size()); while (dc.next()) { assertEquals(dc.get("id").toString(), dc.get("image.link.caption")); } dc = ssn.retrieve(getModelName() + ".User"); dc.addPath("image.id"); assertEquals(users.length, dc.size()); while (dc.next()) { int id = ((BigInteger) dc.get("id")).intValue(); assertEquals(BigInteger.valueOf(id / 2), dc.get("image.id")); } DataCollection dcUp = ssn.retrieve(getModelName() + ".User"); DataCollection dcDown = ssn.retrieve(getModelName() + ".User"); dcUp.addOrder("image.link.caption asc"); dcDown.addOrder("image.link.caption desc"); dcUp.next(); dcDown.next(); assertEquals(BigInteger.valueOf(0), dcUp.get("id")); assertEquals(BigInteger.valueOf(users.length - 1), dcDown.get("id")); dcUp.close(); dcDown.close(); dcUp = ssn.retrieve(getModelName() + ".Image"); dcDown = ssn.retrieve(getModelName() + ".Image"); dcUp.addOrder("users.link.caption asc"); dcDown.addOrder("users.link.caption desc"); dcUp.next(); dcDown.next(); assertEquals(BigInteger.valueOf(0), dcUp.get("id")); assertEquals(BigInteger.valueOf(images.length - 1), dcDown.get("id")); dcUp.close(); dcDown.close(); dc = ssn.retrieve(getModelName() + ".Group"); dc.addFilter("members.image.id = 0"); assertEquals(2, dc.size()); dc = ssn.retrieve(getModelName() + ".Image"); dc.addFilter("users.id = 0 and users.link.caption = '1'"); assertEquals(0, dc.size()); dc = ssn.retrieve(getModelName() + ".Group"); dc.addPath("members.id"); dc.addFilter("members.image.id = 0 and members.image.link.caption = '1'"); assertTrue(dc.next()); assertEquals(BigInteger.valueOf(1), dc.get("members.id")); assertFalse(dc.next()); }
public void build() { fact[0] = BigInteger.valueOf(1); for (int i = 1; i <= 10004; ++i) fact[i] = fact[i - 1].multiply(BigInteger.valueOf(i)); }