当前位置:网站首页>模线性方程组(中国剩余定理+通用解法)
模线性方程组(中国剩余定理+通用解法)
2022-06-11 07:23:00 【CaptainHarryChen】
求解
中国剩余定理
若m1,m2,m3,...,mn m 1 , m 2 , m 3 , . . . , m n 两两互质,则可用中国剩余定理
设M=∏ni=1mi M = ∏ i = 1 n m i ,Mi=M/mi M i = M / m i ,ti t i 为Mi M i 在(mod mi) ( m o d m i ) 意义下的逆元,那么通解为
解释一下:
因为 tiMi≡1 (mod mi) t i M i ≡ 1 ( m o d m i ) ,而 Mj≡0 (mod mi) (j≠i) M j ≡ 0 ( m o d m i ) ( j ≠ i ) ,所以整个求和式子中,模 mi m i 有用的就只剩下 aitiMi≡ai (mod mi) a i t i M i ≡ a i ( m o d m i ) ,对每一个 mi m i 都如此,满足所有条件。
通用解法
不要求m1,m2,m3,...,mn m 1 , m 2 , m 3 , . . . , m n 两两互质的解法
首先考虑只有两个方程
则
用exgcd求不定方程,将 k1 k 1 , k2 k 2 解出,并带入原方程,得到一个特解 x x
可知这两个方程的通解 为
即
我们已经成功把两个方程合为一个,只需要继续把这个方程与方程组其他方程一一合并,就求出解了
模板题
HDU1573,求解的个数
#include<cstdio>
const int MAXM=13;
long long gcd(long long a,long long b,long long &x,long long &y)
{
if(b==0)
{
x=1;y=0;
return a;
}
long long xx,yy,d=gcd(b,a%b,xx,yy);
x=yy;
y=xx-a/b*yy;
return d;
}
long long a[MAXM],b[MAXM];
int main()
{
int T,M;
long long N,A,B,d,l,x,y,p,q,r;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%d",&N,&M);
for(int i=1;i<=M;i++)
scanf("%I64d",a+i);
for(int i=1;i<=M;i++)
scanf("%I64d",b+i);
A=a[1];B=b[1];
bool flag=true;
for(int i=2;i<=M;i++)
{
p=A;q=a[i];r=b[i]-B;
d=gcd(p,q,x,y);
if(r%d)
{flag=false;break;}
p/=d;q/=d;r/=d;
x=x*r;
x=(x%q+q)%q;
l=A/d*a[i];
B=x*A+B;
B=(B%l+l)%l;
A=l;
if(B>N)break;
}
if(flag&&B<=N)
printf("%I64d\n",(N-B)/A+(B!=0));
else
puts("0");
}
return 0;
}边栏推荐
- R language Parallel Computing practice tutorial
- May 30-June 5, 2022 AI industry weekly (issue 100): three years
- 【Oracle 数据库】奶妈式教程day03 排序查询
- P5431 [template] multiplicative inverse 2
- Android和iOS逆向分析/安全检测/渗透测试框架
- 【CF#223 (Div. 2)】A. Sereja and Dima
- C language inherits memory management mechanism (unfinished)
- MS office level II wrong question record [4]
- 学 SQL 必须了解的10个高级概念
- [analysis of STL source code] summary note (4): behind the scenes hero allocator
猜你喜欢

webserver

Leetcode-104. Maximum Depth of Binary Tree

Create a form whose client area is 800 pixels by 600 pixels

JVM学习记录(七)——类加载过程与双亲委派模型

2022 low voltage electrician operation certificate test question simulation test platform operation
![P5431 [template] multiplicative inverse 2](/img/63/1cb95a55c9ce9b92d6d55381d0215b.jpg)
P5431 [template] multiplicative inverse 2

QT table display data

Biological sequence intelligent analysis platform blog (1)
![[STL source code analysis] summary note (2): overview of containers](/img/66/60fba564ae6020dfb503c7fdf78529.jpg)
[STL source code analysis] summary note (2): overview of containers

Gobang interface of mobile console (C language)
随机推荐
多线程复习总结之解析Volatile关键字
CMAP of Matplotlib
[analysis of STL source code] summary note (4): behind the scenes hero allocator
[compilation principle] 05- syntax guided semantic computing -- Semantic Computing Based on translation mode
学 SQL 必须了解的10个高级概念
【CodeForces1019E】Raining season(边分治+斜率优化)
[Oracle database] mammy tutorial day02 use of database management tool sqlplus
Outer margin collapse
Building a full-featured NAS server with raspberry pie (06): built-in file synchronization tool for penetration
Leetcode-647. Palindromic Substrings
【AtCoder2376】Black and White Tree(博弈)
nosqlzoo刷题-1
MFC debugger OutputDebugString override
QT interface nested movement based on qscrollarea
Mybags puls will report an error invalid bound statement (not found) when writing an SQL statement in the XML file:
[STL source code analysis] summary notes (5): a good helper for understanding iterators --list
P3811 [template] multiplicative inverse
软件测试周刊(第75期):唯有平视,才能看见真实的自己。
Modular notes
JVM Learning record (7) - - class Loading Process and parent delegation Model