zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    c# 自定义位数生成激活码
    接口interface和抽象类型abstract
    winform自动升级方案
    泛型介绍
    泛型约束形式
    登录状态保持Session/Cookie
    EFCore 2.0引用标量函数
    .net生成条形码
    通用手机号、身份证号等隐藏显示方法
    .net core api Post请求
  • 原文地址:https://www.cnblogs.com/Yumesenya/p/7553937.html
Copyright © 2011-2022 走看看