我在C ++中的代码
long long N=1000000000000LL;
long long a = N;
mpz_class v;
mpz_mul(v, a, a);
cout<<v<<endl; //I want this to show 1000000000002000000000001
long long U=((sqrt(4*N+v)-1)/4); //not sure how to do this in GMP at all
cout << U << endl; //should show 250000000000
这是一个片段,显示了我想要做的操作。但是我对GMP没有足够的经验来解决这个问题,而且文档对我来说还不清楚。我如何纠正这一切?
答案 0 :(得分:2)
mpz_class有no constructor from long long(它只能达到无符号长整数),所以你必须使用一个中间字符串:
#include <gmpxx.h>
#include <iostream>
#include <string>
int main()
{
long long N = 1000000000000LL;
mpz_class a(std::to_string(N).c_str());
mpz_class v = a*a;
std::cout << v << '\n'; // shows 1000000000000000000000000
std::cout << (a+1) * (a+1) << '\n'; // THIS shows 1000000000002000000000001
mpz_class U = ((sqrt(4*a+v)-1)/4);
std::cout << U << '\n'; // shows 250000000000
}