这是我的代码。我正在尝试将两个非常大的数字相乘 (最大数字可以是100000),并且当前结果为模数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;
}
但是当我编译它时,我得到了错误:
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;
这里A和B可以分别如下:
74543987529435983745230948023948 6498573497543987543985743989120393097595572309482304
修改
/*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;
}
错误消息为:
/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'
答案 0 :(得分:0)
模糊过载错误消息表示%
和mpz_class
之间存在多个long long int
实现。我希望编译器找到多种方法将long long int
提升为mpz_class
,以便它最终可以在%
的两个实例之间调用mpz_class
。
解决此问题的最简单方法是更改参数的类型,使它们匹配,而编译器不必更改任何内容。
尝试将模数声明为mpz_class
:
mpz_class rem(1000000007);
答案 1 :(得分:0)
第一个代码更好地修复错误,我认为rem的定义存在错误; 请尝试>
#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;
}
错误:'operator%'的模糊重载(操作数类型为'mpz_class {aka __gmp_expr&lt; __ mpz_struct [1],__ mpz_struct [1]&gt;}'和'long long int')
此错误已被占用,因为没有运算符:mpz_class operator% (mpz_class a, long long d)
如果你想长时间使用rem,你应该定义这个操作符。