0

我的 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 的经验不够丰富,无法将其搞定,而且我不清楚文档。我该如何纠正这一切?

4

1 回答 1

2

mpz_class没有来自 long long 的构造函数(它只会上升到 unsigned 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
}
于 2012-11-27T04:05:38.283 回答