2021SC@SDUSC
GF2上环/域的扩张(GF2E)
单变元模线性方程
已知a,b,n,求x,使得ax≡b(mod n)。
令d = gcd(a,n),先使用扩展欧几里得求 ax+ny=d 的解。如果 b 不能整除 d 则无解,否则 mod n意义下的解有 d 个,可以通过对某个解不断地加 n/d 得到。
复杂度:O(logn)
输入:a,b,n 三个整数
输出:所有[0,n)中满足 ax≡b(mod n) 的解。
代码
int gcd(int a, int b, int &x, int &y)
{
if(b == 0)
{
x = 1; y = 0;
return a;
}
else
{
int r = gcd(b, a % b, y, x);
y -= x * (a / b);
return r;
}
}
vector<long long> line_mod_equation(long long a, long long b, long long n)
{
long long x, y;
long long d = gcd(a, n, x, y);
vector<long long> ans;
ans.clear();
if(b % d == 0)
{
x %= n; x += n; x %= n;
ans.push_back(x * (b / d) % (n / d));
for(long long i = 1; i < d; ++i)
ans.push_back((ans[0] + i * n / d) % n);
}
return ans;
}
广义表描述 m元多项式
一个一元多项式可以用一个长度为 n,每个数据元素有两个数据项(系数项和指数项)的线性表来表示。
对于 m 元多项式:
一个 m 元多项式的每一项,至多有 m 个变元。
用线性表来表示,每个数据元素需要 m+1 个数据项,以存储 1 个系数值和 m 个指数值。
若每个数据元素都用 m+1 个数据项来表示,会造成存储空间的浪费;
按实际大小来给每个数据元素分配空间,那么每个节点的大小是变化的,因此采用链式结构来表示 m元多项式
代码
typedef struct MPNode
{
ElemType tag; // 区分原子结点0 还是表结点1
int exp; // 指数域
union
{
float coef; // 系数域
struct MPNode * hp; // 表结点的表头指针
}
struct MPNode *tp; // 相当于线性链表的next,指向下一个元素节点
} *MPList; // m 元多项式广义表类型
• 用图形表示上面的多项式
在每一层上增设一个表头结点并利用 exp域 指向该层的变元,用一个一维数组存储一个多项式中的所有变元,exp域中存对应边缘的下标即可。
头指针 p 的 exp 域中是该多项式中所有变元的个数。
单变元模线性方程算法
已知a,b,n,求x,使得ax=b(mod n).
令d=gcd(a,n),如果d|b,则存在d个解。否则无解。
用扩展欧几里得算法求出 ax+ny=d 的一组解(x0,y0).x0即为ax=b(mod n)的一个解。d个解满足x=x0+in/d,i取值范围是[0,d-1].
推导过程
由同余方程的定义,可知ax-ny=b.
将b拆分为gcd(a,n)t.
若gcd(a,n)不能整除b,因为ax0+ny0=gcd(a,n).等式左边有因子gcd(a,n),而等式右边没有,所以此时是无解的。
设x0,y0满足ax0+ny0=gcd(a,n).
等式两边同乘以t,得
a(x0t)-n(-y0t)=gcd(a,n)t.
即x0t为ax=b(mod n)的一个解。
代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll ex_gcd(ll a,ll b,ll &x,ll &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
else
{
ll r=ex_gcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-y*a/b;
return r;
}
}
vector<ll> cm(ll a,ll b,ll n)
{
ll x,y;
vector<ll> ans;
ans.clear();
ll d=ex_gcd(a,n,x,y);
if(b%d==0)
{
ans.push_back(x*(b/d)%n);
for(ll i=1; i<d; i++)
ans.push_back((ans[0]+i*n/d)%n);
return ans;
}
}
int main()
{
ll a,b,n;
while(cin>>a>>b>>n)
{
vector<ll> ans=cm(a,b,n);
for(ll i=0;i<ans.size();i++)
cout<<ans[i]<<endl;
}
return 0;
}
代码:
class GF2E {
public:
GF2E(); // initial value 0
GF2E(const GF2E& a); // copy constructor
explicit GF2E(GF2 a); // promotion constructor
explicit GF2E(long a); // promotion constructor
GF2E& operator=(const GF2E& a); // assignment
GF2E& operator=(GF2 a); // assignment
GF2E& operator=(long a); // assignment
~GF2E(); // destructor
GF2E(GF2E&& a);
// move constructor (C++11 only)
// declared noexcept unless NTL_EXCEPTIONS flag is set
#ifndef NTL_DISABLE_MOVE_ASSIGN
GF2E& operator=(GF2E&& a);
// move assignment (C++11 only)
// declared noexcept unless NTL_EXCEPTIONS flag is set
#endif
static void init(const GF2X& P);
// GF2E::init(P) initializes the current modulus to P;
// required: deg(P) >= 1.
static const GF2XModulus& modulus();
// GF2E::modulus() yields read-only reference to the current modulus
static long degree();
// GF2E::degree() returns deg(P)
// typedefs to aid generic programming
typedef GF2X rep_type;
typedef GF2EContext context_type;
typedef GF2EBak bak_type;
typedef GF2EPush push_type;
typedef GF2EX poly_type;
};