biginteger

Which is a good C++ BigInteger class for programming contests?

天涯浪子 提交于 2019-12-02 04:43:05
问题 I was just wondering which will the best BigInteger class in C++ for programming contests which do not allow external libraries? Mainly I was looking for a class which could be used in my code( I will of course write it on my own, on similar grounds ). The primary factors which I think are important are( according to their importance ): Arbitrary length numbers and their operations should be supported. Should be as small as possible, code-wise. Usually there's a limit on the size of the

Error assigning to an an element in an array of BigInteger

删除回忆录丶 提交于 2019-12-02 03:08:18
This is my code. It shows an error when I create an array of BigInteger and try to assign a value. package test; import java.math.*; import java.lang.*; import java.util.*; public class Test { public static void main(String[] args) { BigInteger[] coef = new BigInteger[78]; int a=24; coef[a]=676557656534345345654645654654645645645645665656567; // Error comes here why System.out.println(coef[a]); } } KEEP IN MIND ALWAYS All numbers greater then 2147483647 will not be allowed as input because int range is -2147483648 to 2147483647 (Never forget it). If just in case your output is greater than the

Problems encrypting a String using RSA algorithm in Java

笑着哭i 提交于 2019-12-02 02:33:39
问题 I'm trying to adopt RSA algorithm for encrypting String objects, but seems that BigInteger -> String and String -> BigInteger conversions do not work properly. Here's my code: public class RSAEncryptor { private BigInteger n, d, e; public RSAEncryptor(int bitlen) { SecureRandom r = new SecureRandom(); BigInteger p = new BigInteger(bitlen / 2, 100, r); BigInteger q = new BigInteger(bitlen / 2, 100, r); n = p.multiply(q); BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract

What are the limits of BigDecimal and BigInteger? [duplicate]

南笙酒味 提交于 2019-12-02 01:53:46
问题 This question already has answers here : Is there an upper bound to BigInteger? [duplicate] (3 answers) What does BigInteger having no limit mean? (4 answers) Closed 6 years ago . I was multiplying very two huge BigIntegervalues in a program. It failed. What are the limits of BigInteger and BigDecimal ? 回答1: You won't get NumberFormatException multiplying large numbers. If the number produced is too large, you will get a cryptic NegativeArraySizeException as the size of the array overflows.

Which is a good C++ BigInteger class for programming contests?

让人想犯罪 __ 提交于 2019-12-02 01:22:39
I was just wondering which will the best BigInteger class in C++ for programming contests which do not allow external libraries? Mainly I was looking for a class which could be used in my code( I will of course write it on my own, on similar grounds ). The primary factors which I think are important are( according to their importance ): Arbitrary length numbers and their operations should be supported. Should be as small as possible, code-wise. Usually there's a limit on the size of the source code which can be submitted to ~50KB, so the code should be ( much )smaller than that. Should be as

How can I multiply and divide 64-bit ints accurately?

孤街浪徒 提交于 2019-12-01 23:20:43
I have a C function: int64_t fn(int64_t a, int32_t b, int32_t c, int32_t d) { /* should return (a * b * c)/d */ } It is possible for a to be near INT64_MAX, but for the final result not to overflow, for instance if b = 1, c = d = 40. However, I am having trouble figuring out how to compute this so that I never lose data to rounding (by doing the division first) or have an intermediate result overflow. If I had access to a large enough datatype to fit the whole product of a, b, and c, I would just do the math in that type and then truncate, but is there some way I can do this without big

_umul128 on Windows 32 bits

只愿长相守 提交于 2019-12-01 22:36:39
问题 In Visual C++, _umul128 is undefined when targeting Windows 32 bits. How can two unsigned 64 bit integers be multiplied when targeting Win32? The solution only needs to work on Visual C++ 2017 targeting Windows 32 bits. 回答1: This answer has a version of the xmrrig function from the other answer optimized for MSVC 32-bit mode. The original is fine with other compilers, especially clang. I looked at MSVC's output for @Augusto's function, and it's really bad. Using __emulu for 32x32 => 64b

Problems encrypting a String using RSA algorithm in Java

江枫思渺然 提交于 2019-12-01 20:59:09
I'm trying to adopt RSA algorithm for encrypting String objects, but seems that BigInteger -> String and String -> BigInteger conversions do not work properly. Here's my code: public class RSAEncryptor { private BigInteger n, d, e; public RSAEncryptor(int bitlen) { SecureRandom r = new SecureRandom(); BigInteger p = new BigInteger(bitlen / 2, 100, r); BigInteger q = new BigInteger(bitlen / 2, 100, r); n = p.multiply(q); BigInteger m = (p.subtract(BigInteger.ONE)).multiply(q.subtract(BigInteger.ONE)); e = new BigInteger("3"); while (m.gcd(e).intValue() > 1) { e = e.add(new BigInteger("2")); } d

_umul128 on Windows 32 bits

て烟熏妆下的殇ゞ 提交于 2019-12-01 20:21:09
In Visual C++, _umul128 is undefined when targeting Windows 32 bits. How can two unsigned 64 bit integers be multiplied when targeting Win32? The solution only needs to work on Visual C++ 2017 targeting Windows 32 bits. This answer has a version of the xmrrig function from the other answer optimized for MSVC 32-bit mode. The original is fine with other compilers, especially clang. I looked at MSVC's output for @Augusto's function, and it's really bad. Using __emulu for 32x32 => 64b multiplication improved it significantly (because MSVC is dumb and doesn't optimize uint64_t * uint64_t = uint64

Printing very big BigIntegers

匆匆过客 提交于 2019-12-01 17:27:55
I'm trying to figure out the following issue related to BigIntegers in Java 7 x64. I am attempting to calculate a number to an extremely high power. Code is below, followed by a description of the problem. import java.math.BigInteger; public class main { public static void main(String[] args) { // Demo calculation; Desired calculation: BigInteger("4096").pow(800*600) BigInteger images = new BigInteger("2").pow(15544); System.out.println( "The number of possible 16 bpc color 800x600 images is: " + images.toString()); } } I am encountering issues printing the result of this operation. When this