码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1930 CRT

时间:2017-09-19 21:14:07      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:efi   new   [1]   inf   back   pair   size   can   scan   

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

 

/** @Date    : 2017-09-16 23:54:51
  * @FileName: HDU 1930 CRT.cpp
  * @Platform: Windows
  * @Author  : Lweleth (SoungEarlf@gmail.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

标签:efi   new   [1]   inf   back   pair   size   can   scan   

原文地址:http://www.cnblogs.com/Yumesenya/p/7553937.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!