gpt4 book ai didi

java - 如何实现这个签名?

转载 作者:行者123 更新时间:2023-12-04 04:59:50 26 4
gpt4 key购买 nike

我有字符串 s 的指纹,
f(s) = (S[1]r^m-1) xor (S[2]r^m-2) xor.....(xor S[n]r^0)) mod (2^32)
让 s 只包含 a 和 b(分别为 0,1)。

如果它是加法而不是异或那么它会很容易。我们可以使用以下规则解决它:
(a + b)mod m = ((a mod m) + (b mod m)) mod m,但这里不是这样。
因此,请记住我无法计算的任何想法,例如r^m-1 因为我可能会面临整数溢出。

最佳答案

如果担心溢出,我建议使用 BigInteger假设您有足够的内存,这不会溢出:

javadoc :

Semantics of arithmetic operations exactly mimic those of Java's integer arithmetic operators, as defined in The Java Language Specification. For example, division by zero throws an ArithmeticException, and division of a negative by a positive yields a negative (or zero) remainder. All of the details in the Spec concerning overflow are ignored, as BigIntegers are made as large as necessary to accommodate the results of an operation.



SO explaning overflow

BigInteger is not really a type. It's a class. It's a wrapper class designed to give you the same functionality as an int, but allows you to use numbers as big as you need without the worry of overflow.

Types do overflow because they are simply a couple of bytes (exact amount depends on the type) of memory, and once that small amount of memory overflows, so do the numbers.

No class "overflows" unless it is specifically designed to do so (or if you run out of resources). A class is defined with enough memory for everything it contains, which would mostly be references to other classes or other data structures.

关于java - 如何实现这个签名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16286038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com