3

This is my code. I am trying to multiply two very large numbers (maximum digits can be 100000) and the present the result as modulo of 10^9 + 7

#include <bits/stdc++.h>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;

long long int rem = 1000000007;

int main() {

    mpz_class A, B, res;

    cin>>A>>B;

    res = 1;
    for(int i=1; i<=B; i++)
        res = res*A;

    res = res % rem;
    cout<<res<<"\n";

    return 0;
}

But I get this error when I compile it:

error: ambiguous overload for ‘operator%’ (operand types are ‘mpz_class {aka __gmp_expr<__mpz_struct [1], __mpz_struct [1]>}’ and ‘long long int’)
     res = res % rem;

Here A and B can be as large as below respectively:

74543987529435983745230948023948 6498573497543987543985743989120393097595572309482304

EDIT:

/*Header files same as prev*/
mpz_class rem (1000000007);

int main() {

    mpz_class A, B, res;
    cin>>A>>B;
    res = A ^ B;     // Using "^" to denote exponentiation since we have used
    res = res % rem; // "+" and "%" instead of mpz_sum and mpz_mod functions
    cout<<res<<"\n";

return 0;
}

The ERROR message is:

/ccqd5nye.o: In function `__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::~__gmp_expr()':
/usr/include/gmpxx.h:1523: undefined reference to `__gmpz_clear'
/ccqd5nye.o: In function `__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr()':
/usr/include/gmpxx.h:1488: undefined reference to `__gmpz_init'
/usr/include/gmpxx.h:1488: undefined reference to `__gmpz_init'
/usr/include/gmpxx.h:1488: undefined reference to `__gmpz_init'
/ccqd5nye.o: In function `std::istream& operator>><__mpz_struct [1]>(std::istream&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]>&)':
/usr/include/gmpxx.h:2039: undefined reference to `operator>>(std::istream&, __mpz_struct*)'
/usr/include/gmpxx.h:2039: undefined reference to `operator>>(std::istream&, __mpz_struct*)'
/ccqd5nye.o: In function `__gmp_binary_xor::eval(__mpz_struct*, __mpz_struct const*, __mpz_struct const*)':
/usr/include/gmpxx.h:846: undefined reference to `__gmpz_xor'
/ccqd5nye.o: In function `__gmp_binary_modulus::eval(__mpz_struct*, __mpz_struct const*, __mpz_struct const*)':
/usr/include/gmpxx.h:763: undefined reference to `__gmpz_tdiv_r'
/ccqd5nye.o: In function `std::ostream& operator<< <__mpz_struct [1], __mpz_struct [1]>(std::ostream&, __gmp_expr<__mpz_struct [1], __mpz_struct [1]> const&)':
/usr/include/gmpxx.h:2033: undefined reference to `operator<<(std::ostream&, __mpz_struct const*)'
/ccqd5nye.o: In function `__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::~__gmp_expr()':
/usr/include/gmpxx.h:1523: undefined reference to `__gmpz_clear'
/usr/include/gmpxx.h:1523: undefined reference to `__gmpz_clear'
/usr/include/gmpxx.h:1523: undefined reference to `__gmpz_clear'
/usr/include/gmpxx.h:1523: undefined reference to `__gmpz_clear'
/usr/include/gmpxx.h:1523: undefined reference to `__gmpz_clear'
/ccqd5nye.o:/usr/include/gmpxx.h:1523: more undefined references to `__gmpz_clear' follow
/ccqd5nye.o: In function `__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::init_ui(unsigned long)':
/usr/include/gmpxx.h:1465: undefined reference to `__gmpz_init_set_ui'
Vatsal Aggarwal
  • 564
  • 2
  • 5
  • 18

2 Answers2

0

The ambiguous overload error message means that there are multiple implementations for % between mpz_class and long long int. I expect the compiler has found multiple ways to promote long long int to mpz_class, so that it can eventually call % between two instances of mpz_class.

The simplest way to resolve this is to change the types of the arguments so they match without the compiler having to change anything.

Try declaring the modulus as mpz_class:

mpz_class rem(1000000007);
Conrad Parker
  • 834
  • 10
  • 23
  • Hey! It would be great if you could add a bit of explanation to the answer. While this seems to have helped the asker, it has been flagged for being a code-only answer. – ZeMoon Aug 25 '17 at 10:56
  • @ZeMoon It just assigns "rem" the data type of C++ GMP library so that when the arithmetic operations are performed they are performed on similar data types, since GMP is defined for very very large numbers, it operates differently and hence type matching is important.....but this is my inference. – Vatsal Aggarwal Aug 25 '17 at 12:24
  • As I said in my comment, GMP does not handle long long. The possibilities that g++ found are converting the long long to long, unsigned long, float, double, etc (all are bad). – Marc Glisse Aug 29 '17 at 18:35
0

First code is better to fix the error, I supposed that there is error in definition of rem; Please try it>

#include <bits/stdc++.h>
#include <gmp.h>
#include <gmpxx.h>
using namespace std;

mpz_class rem = 1000000007;

int main() {

    mpz_class A, B, res;

    cin>>A>>B;

    res = 1;
    for(int i=1; i<=B; i++)
        res = res*A;

    res = res % rem;
    cout<<res<<"\n";

    return 0;
}

error: ambiguous overload for ‘operator%’ (operand types are ‘mpz_class {aka __gmp_expr<__mpz_struct [1], __mpz_struct [1]>}’ and ‘long long int’) This error occupied because there is no operator this: mpz_class operator% (mpz_class a, long long d)

If you wanna to use long long for rem, you should define this operator.

R.Mirana
  • 68
  • 9