⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lzz_px.txt

📁 密码大家Shoup写的数论算法c语言实现
💻 TXT
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************\MODULE: zz_pXSUMMARY:The class zz_pX implements polynomial arithmetic modulo p.Polynomial arithmetic is implemented using a combination of classicalroutines, Karatsuba, and FFT.\**************************************************************************/#include "zz_p.h"#include "vec_zz_p.h"class zz_pX {public:   zz_pX(); // initial value 0   zz_pX(const zz_pX& a); // copy   zz_pX& operator=(const zz_pX& a); // assignment   zz_pX& operator=(zz_p a);    zz_pX& operator=(long a);    ~zz_pX(); // destructor   zz_pX(long i, zz_p c); // initialize to X^i*c   zz_pX(long i, long c);    };/**************************************************************************\                                  Comparison\**************************************************************************/long operator==(const zz_pX& a, const zz_pX& b);long operator!=(const zz_pX& a, const zz_pX& b);long IsZero(const zz_pX& a); // test for 0long IsOne(const zz_pX& a); // test for 1// PROMOTIONS: operators ==, != promote {long, zz_p} to zz_pX on (a, b)/**************************************************************************\                                   Addition\**************************************************************************/// operator notation:zz_pX operator+(const zz_pX& a, const zz_pX& b);zz_pX operator-(const zz_pX& a, const zz_pX& b);zz_pX operator-(const zz_pX& a); // unary -zz_pX& operator+=(zz_pX& x, const zz_pX& a);zz_pX& operator+=(zz_pX& x, zz_p a);zz_pX& operator+=(zz_pX& x, long a);zz_pX& operator-=(zz_pX& x, const zz_pX& a);zz_pX& operator-=(zz_pX& x, zz_p a);zz_pX& operator-=(zz_pX& x, long a);zz_pX& operator++(zz_pX& x);  // prefixvoid operator++(zz_pX& x, int);  // postfixzz_pX& operator--(zz_pX& x);  // prefixvoid operator--(zz_pX& x, int);  // postfix// procedural versions:void add(zz_pX& x, const zz_pX& a, const zz_pX& b); // x = a + bvoid sub(zz_pX& x, const zz_pX& a, const zz_pX& b); // x = a - bvoid negate(zz_pX& x, const zz_pX& a); // x = -a// PROMOTIONS: binary +, - and procedures add, sub promote {long, zz_p}// to zz_pX on (a, b)./**************************************************************************\                               Multiplication\**************************************************************************/// operator notation:zz_pX operator*(const zz_pX& a, const zz_pX& b);zz_pX& operator*=(zz_pX& x, const zz_pX& a);zz_pX& operator*=(zz_pX& x, zz_p a);zz_pX& operator*=(zz_pX& x, long a);// procedural versions:void mul(zz_pX& x, const zz_pX& a, const zz_pX& b); // x = a * bvoid sqr(zz_pX& x, const zz_pX& a); // x = a^2zz_pX sqr(const zz_pX& a);// PROMOTIONS: operator * and procedure mul promote {long, zz_p} to zz_pX// on (a, b).void power(zz_pX& x, const zz_pX& a, long e);  // x = a^e (e >= 0)zz_pX power(const zz_pX& a, long e);/**************************************************************************\                               Shift OperationsLeftShift by n means multiplication by X^nRightShift by n means division by X^nA negative shift amount reverses the direction of the shift.\**************************************************************************/// operator notation:zz_pX operator<<(const zz_pX& a, long n);zz_pX operator>>(const zz_pX& a, long n);zz_pX& operator<<=(zz_pX& x, long n);zz_pX& operator>>=(zz_pX& x, long n);// procedural versions:void LeftShift(zz_pX& x, const zz_pX& a, long n); zz_pX LeftShift(const zz_pX& a, long n);void RightShift(zz_pX& x, const zz_pX& a, long n); zz_pX RightShift(const zz_pX& a, long n); /**************************************************************************\                                  Division\**************************************************************************/// operator notation:zz_pX operator/(const zz_pX& a, const zz_pX& b);zz_pX operator%(const zz_pX& a, const zz_pX& b);zz_pX& operator/=(zz_pX& x, const zz_pX& a);zz_pX& operator/=(zz_pX& x, zz_p a);zz_pX& operator/=(zz_pX& x, long a);zz_pX& operator%=(zz_pX& x, const zz_pX& b);// procedural versions:void DivRem(zz_pX& q, zz_pX& r, const zz_pX& a, const zz_pX& b);// q = a/b, r = a%bvoid div(zz_pX& q, const zz_pX& a, const zz_pX& b);// q = a/bvoid rem(zz_pX& r, const zz_pX& a, const zz_pX& b);// r = a%blong divide(zz_pX& q, const zz_pX& a, const zz_pX& b);// if b | a, sets q = a/b and returns 1; otherwise returns 0long divide(const zz_pX& a, const zz_pX& b);// if b | a, sets q = a/b and returns 1; otherwise returns 0// PROMOTIONS: operator / and procedure div promote {long, zz_p} to zz_pX// on (a, b)./**************************************************************************\                                   GCD'sThese routines are intended for use when p is prime.\**************************************************************************/void GCD(zz_pX& x, const zz_pX& a, const zz_pX& b);zz_pX GCD(const zz_pX& a, const zz_pX& b); // x = GCD(a, b),  x is always monic (or zero if a==b==0).void XGCD(zz_pX& d, zz_pX& s, zz_pX& t, const zz_pX& a, const zz_pX& b);// d = gcd(a,b), a s + b t = d // NOTE: A classical algorithm is used, switching over to a// "half-GCD" algorithm for large degree/**************************************************************************\                                  Input/OutputI/O format:   [a_0 a_1 ... a_n],represents the polynomial a_0 + a_1*X + ... + a_n*X^n.On output, all coefficients will be integers between 0 and p-1, amda_n not zero (the zero polynomial is [ ]).  On input, the coefficientsare arbitrary integers which are reduced modulo p, and leading zerosstripped.\**************************************************************************/istream& operator>>(istream& s, zz_pX& x);ostream& operator<<(ostream& s, const zz_pX& a);/**************************************************************************\                              Some utility routines\**************************************************************************/long deg(const zz_pX& a);  // return deg(a); deg(0) == -1.zz_p coeff(const zz_pX& a, long i);// returns the coefficient of X^i, or zero if i not in rangezz_p LeadCoeff(const zz_pX& a);// returns leading term of a, or zero if a == 0zz_p ConstTerm(const zz_pX& a);// returns constant term of a, or zero if a == 0void SetCoeff(zz_pX& x, long i, zz_p a);void SetCoeff(zz_pX& x, long i, long a);// makes coefficient of X^i equal to a; error is raised if i < 0void SetCoeff(zz_pX& x, long i);// makes coefficient of X^i equal to 1;  error is raised if i < 0void SetX(zz_pX& x); // x is set to the monomial Xlong IsX(const zz_pX& a); // test if x = Xvoid diff(zz_pX& x, const zz_pX& a);zz_pX diff(const zz_pX& a); // x = derivative of avoid MakeMonic(zz_pX& x); // if x != 0 makes x into its monic associate; LeadCoeff(x) must be// invertible in this case.void reverse(zz_pX& x, const zz_pX& a, long hi);zz_pX reverse(const zz_pX& a, long hi);void reverse(zz_pX& x, const zz_pX& a);zz_pX reverse(const zz_pX& a);// x = reverse of a[0]..a[hi] (hi >= -1);// hi defaults to deg(a) in second versionvoid VectorCopy(vec_zz_p& x, const zz_pX& a, long n);vec_zz_p VectorCopy(const zz_pX& a, long n);// x = copy of coefficient vector of a of length exactly n.// input is truncated or padded with zeroes as appropriate./**************************************************************************\                             Random Polynomials\**************************************************************************/void random(zz_pX& x, long n);zz_pX random_zz_pX(long n);// x = random polynomial of degree < n /**************************************************************************\                    Polynomial Evaluation and related problems\**************************************************************************/void BuildFromRoots(zz_pX& x, const vec_zz_p& a);zz_pX BuildFromRoots(const vec_zz_p& a);// computes the polynomial (X-a[0]) ... (X-a[n-1]), where n =// a.length()void eval(zz_p& b, const zz_pX& f, zz_p a);zz_p eval(const zz_pX& f, zz_p a);// b = f(a)void eval(vec_zz_p& b, const zz_pX& f, const vec_zz_p& a);vec_zz_p eval(const zz_pX& f, const vec_zz_p& a);//  b.SetLength(a.length());  b[i] = f(a[i]) for 0 <= i < a.length()void interpolate(zz_pX& f, const vec_zz_p& a, const vec_zz_p& b);zz_pX interpolate(const vec_zz_p& a, const vec_zz_p& b);// interpolates the polynomial f satisfying f(a[i]) = b[i].  p should// be prime./**************************************************************************\                       Arithmetic mod X^nIt is required that n >= 0, otherwise an error is raised.\**************************************************************************/void trunc(zz_pX& x, const zz_pX& a, long n); // x = a % X^nzz_pX trunc(const zz_pX& a, long n); void MulTrunc(zz_pX& x, const zz_pX& a, const zz_pX& b, long n);zz_pX MulTrunc(const zz_pX& a, const zz_pX& b, long n);// x = a * b % X^nvoid SqrTrunc(zz_pX& x, const zz_pX& a, long n);zz_pX SqrTrunc(const zz_pX& a, long n);// x = a^2 % X^nvoid InvTrunc(zz_pX& x, const zz_pX& a, long n);zz_pX InvTrunc(const zz_pX& a, long n);// computes x = a^{-1} % X^n.  Must have ConstTerm(a) invertible./**************************************************************************\                Modular Arithmetic (without pre-conditioning)Arithmetic mod f.All inputs and outputs are polynomials of degree less than deg(f), anddeg(f) > 0.NOTE: if you want to do many computations with a fixed f, use thezz_pXModulus data structure and associated routines below for betterperformance.\**************************************************************************/void MulMod(zz_pX& x, const zz_pX& a, const zz_pX& b, const zz_pX& f);zz_pX MulMod(const zz_pX& a, const zz_pX& b, const zz_pX& f);// x = (a * b) % fvoid SqrMod(zz_pX& x, const zz_pX& a, const zz_pX& f);zz_pX SqrMod(const zz_pX& a, const zz_pX& f);// x = a^2 % fvoid MulByXMod(zz_pX& x, const zz_pX& a, const zz_pX& f);zz_pX MulByXMod(const zz_pX& a, const zz_pX& f);// x = (a * X) mod fvoid InvMod(zz_pX& x, const zz_pX& a, const zz_pX& f);zz_pX InvMod(const zz_pX& a, const zz_pX& f);// x = a^{-1} % f, error is a is not invertiblelong InvModStatus(zz_pX& x, const zz_pX& a, const zz_pX& f);// if (a, f) = 1, returns 0 and sets x = a^{-1} % f; otherwise,// returns 1 and sets x = (a, f)// for modular exponentiation, see below/**************************************************************************\                     Modular Arithmetic with Pre-ConditioningIf you need to do a lot of arithmetic modulo a fixed f, buildzz_pXModulus F for f.  This pre-computes information about f thatspeeds up subsequent computations. Required: deg(f) > 0 and LeadCoeff(f)invertible.As an example, the following routine computes the product modulo f of a vectorof polynomials.#include "zz_pX.h"void product(zz_pX& x, const vec_zz_pX& v, const zz_pX& f){   zz_pXModulus F(f);   zz_pX res;   res = 1;   long i;   for (i = 0; i < v.length(); i++)      MulMod(res, res, v[i], F);    x = res;}Note that automatic conversions are provided so that a zz_pX canbe used wherever a zz_pXModulus is required, and a zz_pXModuluscan be used wherever a zz_pX is required.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -