HDU 1930 CRT

Posted ( m Lweleth)

tags:

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

也是很模板的一道题,给出一些数,分割,模数固定是4个互质的。

 

/** @Date    : 2017-09-16 23:54:51
  * @FileName: HDU 1930 CRT.cpp
  * @Platform: Windows
  * @Author  : Lweleth ([email protected]ail.com)
  * @Link    : https://github.com/
  * @Version : $Id$
  */
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std;

const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8;

LL exgcd(LL a, LL b, LL &x, LL &y)
{
	LL d = a;
	if(b == 0)
		x = 1, y = 0;
	else 
	{
		d = exgcd(b, a % b, y, x);
		y -= (a / b) * x;
	}
	return d;
}

//非互质
LL crtCD(LL &rem, LL &mod, LL newRem, LL newMod)
{
	LL c = newRem - rem;// c= r2 -r1 
	LL g = __gcd(mod, newMod);
	if(c % g!=0)
		return 0;
	LL x, y;
	exgcd(mod, newMod, x, y);
	LL t = newMod / g;// b/d
	LL minx = (c / g * x % t + t) % t;//x = x‘ * (c/g) mod (b/d)
	rem = minx * mod + rem;// newrem = mod * x + rem,
	mod = mod / g * newMod; //newmod = lcm(mod, newMod)
	return 1;
} 

LL CRT(LL n, LL rem[], LL mod[])
{
	LL M = 1,x,y;
	for(int i = 0; i < n; i++)
		M *= mod[i];
	LL res = 0;
	for(int i = 0; i < n; i++)
	{
		LL t = M / mod[i];
		exgcd(t, mod[i], x, y);
		res = (res + t * rem[i] * x) % M;
	}

	return (res % M + M) % M;
}

int main()
{
	int T;
	cin >> T;
	while(T--)
	{
		int n;
		cin >> n;
		LL mod[5];
		LL rem[5];
		for(int i = 0; i < 4; i++) scanf("%lld", mod + i);
		for(int i = 0; i < n; i++)
		{
			LL x;
			scanf("%lld", &x);
			rem[0] = x / 1000000;
			rem[1] = x / 10000 % 100;
			rem[2] = x / 100 % 100;
			rem[3] = x % 100;
			/*cout << endl;
			for(int j = 0; j < 4; j++)
				cout << rem[j] << " ";
			cout << endl;*/
			LL nre = CRT(4, rem, mod);
			LL a = nre / 10000;
			LL b = nre / 100 % 100;
			LL c = nre % 100;
			//printf("%d %d %d~\n",a, b, c);
			//cout << nre << " ";
			if(i == n - 1)
			{
				printf("%c", a==27?‘ ‘:(a+‘A‘-1));
				if(b != 27)
					printf("%c", (b+‘A‘-1));
				if(c != 27)
					printf("%c", (c+‘A‘-1));
			}
			else 
				printf("%c%c%c", a==27?‘ ‘:(a+‘A‘-1), b==27?‘ ‘:(b+‘A‘-1), c==27?‘ ‘:(c+‘A‘-1));
		}
		printf("\n");
	}
    return 0;
}

以上是关于HDU 1930 CRT的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1573 CRT

HDU 5768 Lucky7(CRT+容斥原理)

hdu 5446 lucas+crt+按位乘

HDU 5446 Unknown Treasure(Lucas定理+CRT)

HDU 5446 lucas CRT

HDU 4521 小明系列问题——小明序列 (线段树维护DP)