I would like to calculate:
((a+b)/c)mod m
I would like to know if there is any efficient way since a is too big but b , c and m fit in a simple 32-bit int.
There is no division operator in modular arithmetic. Instead, you must calculate the modular inverse of the denominator, then multiply. Thus, in your example, you would calculate a+b modulo m, calculate the modular inverse of c modulo m, then multiply the two modulo m. The modular inverse can be found using the extended Euclidean algorithm. If you don't know how to compute the modular inverse, ask.
Related
I asked myself if one can compute the nth Fibonacci number in time O(n) or O(1) and why?
Can someone explain please?
Yes. It is called Binet's Formula, or sometimes, incorrectly, De Moivre's Formula (the real De Moivre's formula is another, but De Moivre did discover Binet's formula before Binet), and involves the golden ratio Phi. The mathematical reasoning behind this (see link) is a bit involved, but doable:
While it is an approximate formula, Fibonacci numbers are integers -- so, once you achieve a high enough precision (depends on n), you can just approximate the number from Binet's formula to the closest integer.
Precision however depends on constants, so you basically have two versions, one with float numbers and one with double precision numbers, with the second also running in constant time, but slightly slower. For large n you will need an arbitrary precision number library, and those have processing times that do depend on the numbers involved; as observed by #MattTimmermans, you'll then probably end up with a O(log^2 n) algorithm. This should happen for large enough values of n that you'd be stuck with a large-number library no matter what (but I'd need to test this to be sure).
Otherwise, the Binet formula is mainly made up of two exponentiations and one division (the three sums and divisions by 2 are probably negligible), while the recursive formula mainly employs function calls and the iterative formula uses a loop. While the first formula is O(1), and the other two are O(n), the actual times are more like a, b n + c and d n + e, with values for a, b, c, d and e that depend on the hardware, compiler, implementation etc. . With a modern CPU it is very likely that a is not too larger than b or d, which means that the O(1) formula should be faster for almost every n. But most implementations of the iterative algorithm start with
if (n < 2) {
return n;
}
which is very likely to be faster for n = 0 and n = 1. I feel confident that Binet's formula is faster for any n beyond the single digits.
Instead of thinking about the recursive method, think of building the sequence from the bottom up, starting at 1+1.
You can also use a matrix m like this:
1 1
1 0
and calculate power n of it. then output m^n[0,0].
Ques:
https://www.codechef.com/ISCC2018/problems/T24
Code which I referred:
https://www.codechef.com/viewsolution/19340066
In this question we were supposed to find sum of a geometric progression.
while using summation of a Geometric Progression formula in cpp, why is there need to find modulo multiplicative inverse of K-1 ?Why cannot we directly divide by K-1?
Geomteric progression:
k+(kk)+(kk*k)+.....till n terms (where k is some integer)
Formula: (first term *(Cd raised to power n - 1))/(Cd-1).
Cd refers to common difference of Geomteric progression.
Dividing by K-1 would give the correct answer if high precision integers are used.
The problem is that K to the power of N is going to be very large. K can be up to a billion, and N up to a billion, so K to the power of N could be a 9 billion digit number.
However, only the result modulo a prime is requested so high precision integers can be avoided by doing all calculations modulo this prime.
There exists a binary GCD algorithm for finding the greatest common divisor of a number. In general, the GCD can be extended to the XGCD, which can help find a multiplicative inverse in a field.
I am working with binary numbers that represent a polynomial. For example, the bitstring 1101 represents x^3 + x^2 + 1. I need to compute the modular inverse of a random polynomial modulo x^p - 1 for some large known prime p. However, I need to do it in constant time (meaning that the runtime should not depend on the number I am inverting). I know how to make the binary GCD constant time and I know how to implement the XGCD for polynomials in order to compute multiplicative inverses. What I don't know is if there exists a binary GCD equivalent (with corresponding XGCD) for (binary) polynomials?
Yes there is. The "binary" GCD works in any ring where the smallest prime exists. For integers it is 2, hence the name binary. For polynomials, it is x. The algorithm follows the same idea: subtract polynomials to eliminate a free term in one of higher degree, factor out the highest possible power of x, and keep going until the result of subtraction becomes zero.
I thought of the following problem recently, and I'm quite surprised that there doesn't seem to be anybody who asked this question yet:
Given a string, how many distinct permutations of it exist, modulo ?
I know the formula where is the length of the string, and are the count of each character (considering an alphabet of size ). So, the string toffee would have different permutations.
But this doesn't quite work anymore when can be really large (say ), since computing would go out of the range of long long int, and using BigIntegers would be too slow. Is there any way to compute this in, say, or time?
If I preprocessed the factorials from to , and my "strings" came in the form of an array of length where each element contained the count of each letter, would it be possible to compute it in or time?
Would appreciate any help on this :)
The trick is to note that p = 10^9 + 7 is a prime number. Therefore, we can use multiplicative inverses and Fermat's little theorem to turn the divisions in your formula into multiplications by the inverses:
n! / (a1!*...*ak!) =
n! * a1!^(p - 2) * ... * ak!^(p - 2) (mod p)
Which will be your formula mod p with no divisions and an easy implementation (just use modular exponentiation by squaring).
Complexity will be O(k log p + n), since we have O(k) multiplications, and for each one, an O(log p) exponentiation, and we must also compute n! and the factorial of each count.
This is easier to implement than cancelling out factors in the fraction.
The number of distinct permutations of a string is always an integer, despite being the result of a division. That's because the factors of the denominator essentially "knock out" some of the factors of the numerator. So you can eliminate the division as a post-factorial operation, instead dividing out the particular factors of the factorial which you've matched up with factors of the denominator.
Once you've got the division removed, you're just left with modular multiplication, which is simple.
Yes .. a solution exists. You can read about Modular multiplicative inverse algorithm. This
As the answer is with modulo 1000000007(which is a prime also), you can try Fermat's little theorem to solve this problem. If modulo number is mod Complexity is O(N + K * log(mod)).
If N isn't gigantic (that is, it's small enough to sift it using something like Sieve of Eratosthenes), you can calculate the prime factorisation of N! with a modified version of the sieve.
Then you can use the prime factorisation to calculate the division, cancelling out the factors present on both sides of the division.
Though this doesn't take into account the fact that you want the result modulo a prime number (where better solutions exist), it's probably useful to know in the general case.
I am using the FFT to evaluate a polynomial at certain points so that it can be represented using value representation. (representation as a number of points equal to its degree)
However to multiply two polynomials of degree d, I need to evaluate both at 2d + 1 points. However using the FFT for evaluation (multiplying by the dth roots of unity) only evaluates the polynomial at d points. Therefore how can the FFT be used for evaluation for polynomial evaluation if it only evaluates a polynomial at d points? (as opposed to 2d + 1)
You get to choose which n-th roots of -1 you evaluate at. If you need 2d-1 points (as I suspect you do) just use the (2d-1)-th roots of -1. In fact, you would normally use the 2^k-th roots of -1, where 2^k is the first power of 2 >= 2d-1, because it is much easier to get fast FFT for powers of 2. The complexity is still O(d log d) because the definition of O allows for constant factors.