中国剩余定理 (非互质情况)
来源:互联网 发布:超星尔雅刷课软件 编辑:程序博客网 时间:2024/06/05 10:24
在前边的时候介绍了普通剩余定理的模板,但在很多时候是这些数并不互质,那次是有应该咋整呢,下边我们就介绍一下不互质情况下的中国剩余定理
首先不互质的情况就是两个和成一个的过程
模板;
#include <iostream>
#include <cstdio>
using namespace std;
int a[10],b[10];
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
void exgcd(int a,int b,int &x,int &y)
{
if(b==0)
{
x=1;
y=0;
return ;
}
int x1,y1;
exgcd(b,a%b,x1,y1);
x=y1;
y=x1-(a/b)*y1;
}
int main()
{
int n,m,x,y;
cin>>n;
for(int h=1;h<=n;h++)
{
scanf("%d",&m);
for(int i=1;i<=m;i++)
scanf("%d",&a[i]);
for(int j=1;j<=m;j++)
scanf("%d",&b[j]);
int m1=b[1],n1=a[1];
int flag=1;
for(int i=2;i<=m;i++)
{
int m2=b[i],n2=a[i];
int d=gcd(n1,n2);
if((m2-m1)%d!=0)
{
flag=0;
break;
}
exgcd(n1,n2,x,y);
int t=n2/d;
x=x*((m2-m1)/d);
x=(x%t+t)%t;
m1=m1+x*n1;
n1=(n1*n2)/d;
}
if(m1==0)
{
m1=1;
for(int i=1;i<=m;i++)
m1=m1*a[i]/gcd(m1,a[i]);
}
printf("Case %d: ",h);
if(flag==0)
printf("-1\n");
else
printf("%d\n",m1);
}
return 0;
}
- 中国剩余定理 (非互质情况)
- hdu 1573(中国剩余定理非互质情况)
- 中国剩余定理--不互质情况
- 中国剩余定理模板(1) 两两互质情况
- 中国剩余定理模板(2)出现不互质情况
- HDU 1573 中国剩余定理(有不互质情况)
- hdu3575(中国剩余定理,注意特殊情况处理)
- 中国剩余定理模板(互质的情况)
- 中国剩余定理(互质与不互质的情况)
- 中国剩余定理(不互质的情况)-HDU3579
- hdu 3579(中国剩余定理非互质)
- 中国剩余定理(互质+非互质)
- HDU3579中国剩余定理(不互质的情况)
- 中国剩余定理(转)
- 中国剩余定理(韩信点兵)
- poj2891(中国剩余定理)
- POJ_1006_Biorhythms(中国剩余定理)
- 中国剩余定理(续)
- git上传github和码云的步骤
- java 中的构造代码块和构造函数以及静态代码块
- 程序调试记录
- 对于张量展开矩阵的理解
- 线程 thread_once
- 中国剩余定理 (非互质情况)
- 读ByteBuf有感
- 存储过程和函数的区别
- 洛谷 1417
- Anaconda的使用
- c#在DataGridView中插入一列
- 学习资源整理分享
- Struts2的Action类详解(深入表单登录并验证)
- android fastboot模式下载以及出现的问题