扩展中国剩余定理

Posted thusloop

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了扩展中国剩余定理相关的知识,希望对你有一定的参考价值。

#include<bits/stdc++.h>
#define int long long
#define LD long double
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf=2e18+100;
const int maxn=1e5+10;
int a[maxn],b[maxn];
int n;
int mul(int a,int b,int p)
{
	int x=(LD)a*b/p;//#define LD long double
	return ((a*b-x*p)%p+p)%p;//#define int long long
}
int exgcd(int a,int b,int &x,int &y)
{
	if(b==0)
	{
		x=1;
		y=0;
		return a;
	}
	int gcd=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return gcd;
}
int exc()
{
	int prea=a[1],preb=b[1];
	for(int i=2;i<=n;i++)
	{
		int x,y;
		int d=exgcd(preb,b[i],x,y);
		int t=b[i]/d;//如果 (a[i]-prea)%d!=0则无解 
		x*=(a[i]-prea)/d;
		x=(x%t+t)%t;
		int temp=preb/d*b[i];
		prea=(preb*x+prea)%temp;
		//prea=(mul(preb,x,temp)+prea)%temp;
		preb=temp;
		//prea=prea%preb;
	}
	return prea;
}
signed main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>b[i]>>a[i];
	}
	cout<<exc()<<"\\n";
}
//  __int128 
/*
#include<bits/stdc++.h>
#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
const int inf=2e18+100;
const int maxn=1e5+10;
int a[maxn],b[maxn];
int n;
void print(__int128 x)
{
    if (x>9) print(x/10);
    putchar('0'+x%10);
}
__int128 exgcd(__int128 a,__int128 b,__int128 &x,__int128 &y)
{
	if(b==0)
	{
		x=1;
		y=0;
		return a;
	}
	__int128 gcd=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return gcd;
}
__int128 exc()
{
	__int128 prea=a[1];
	__int128 preb=b[1];
	for(int i=2;i<=n;i++)
	{
		__int128 x,y;
		__int128 d=exgcd(preb,b[i],x,y);
		__int128 t=b[i]/d;
		x*=(a[i]-prea)/d;
		x=(x%t+t)%t;
		__int128_t temp=preb/d*b[i];
		prea=((preb%temp*x)%temp+prea)%temp;
		preb=temp;
		prea=prea%preb;
	}
	return prea;
}
signed main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>b[i]>>a[i];
	}
	print(exc());
}

*/
*/

以上是关于扩展中国剩余定理的主要内容,如果未能解决你的问题,请参考以下文章

中国剩余定理与扩展中国剩余定理

中国剩余定理&&扩展中国剩余定理

中国剩余定理(CRT) & 扩展中国剩余定理(ExCRT)总结

欧几里得(辗转相除gcd)扩欧(exgcd)中国剩余定理(crt)扩展中国剩余定理(excrt)简要介绍

Acwing-204-表达整数的奇怪方式(扩展中国剩余定理)

中国剩余定理讲解