• HDU 1930 CRT


    也是很模板的一道题,给出一些数,分割,模数固定是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~
    ",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("
    ");
    	}
        return 0;
    }
    
  • 相关阅读:
    微服务之集成(四)
    后缀数组
    HDU-6514 Monitor(二维前缀和+差分)
    前缀和、二维前缀和与差分
    2019年icpc上海网络赛 B Light bulbs (分块、差分)
    树状数组
    2019ICPC 上海网络赛 L. Digit sum(二维树状数组+区间求和)
    [USACO09MAR]向右看齐Look Up(单调栈、在线处理)
    Pay Back(模拟)
    [USACO09MAR]Moon哞哞叫Moon Mooing(模拟)
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/7553937.html
Copyright © 2020-2023  润新知