【数论】 HDOJ 1930 && ZOJ 2945 And Now, a Remainder from Our Sponsor

中国剩余定理的简单应用。。。

#include <iostream>  
#include <sstream>  
#include <algorithm>  
#include <vector>  
#include <queue>  
#include <stack>  
#include <map>  
#include <set>  
#include <bitset>  
#include <cstdio>  
#include <cstring>  
#include <cstdlib>  
#include <cmath>  
#include <climits>  
#define maxn 105
#define eps 1e-6 
#define mod 10007 
#define INF 99999999  
#define lowbit(x) (x&(-x))  
//#define lson o<<1, L, mid  
//#define rson o<<1 | 1, mid+1, R  
//typedef long long LL;
typedef int LL;
using namespace std;

int a[maxn], b[maxn], n, p[1000005];
void extend_gcd(int a, int b, int &d, int &x, int &y)
{
    if(b == 0) { d = a, x = 1, y = 0; }
    else { extend_gcd(b, a%b, d, y, x), y -= x*(a/b); }
}
void extend_chinese_reminder(int &a1, int &b1)
{
	int x, y, g, tmp, i, a2, b2;
	for(i = 1; i < n; i++) {
		a2 = a[i], b2 = b[i];
		extend_gcd(a1, a2, g, x, y);
		tmp = a2/g;
		x = x*(b2-b1)/g;
		x = (x%tmp+tmp)%tmp;
		b1 = a1*x+b1;
		a1 = (a1*a2)/g;
		b1 = (b1%a1+a1)%a1;
	}
}
int main(void)
{
	int _, m, tmp, i, j, a1, b1, bb;
	scanf("%d", &_);
	while(_--) {
		n = 4, bb = 0;
		scanf("%d", &m);
		for(i = 0; i < n; i++) scanf("%d", &a[i]);
		for(i = 0; i < m; i++) {
			scanf("%d", &tmp);
			for(j = 0; j < n; j++)
				b[3-j] = tmp%100, tmp/=100;
			a1 = a[0], b1 = b[0];
			extend_chinese_reminder(a1, b1);
			for(j = 0; j < n-1; j++)
				b[2-j] = b1%100, b1/=100;
			for(j = 0; j < n-1; j++)
				p[bb++] = b[j];
		}
		bb--;
		for(j = bb; j >= 0; j--) if(p[j]!=27) break;
		for(i = 0; i <= j; i++)
			if(p[i] == 27) printf(" ");
			else printf("%c", p[i]+'A'-1);
		printf("\n");
	}
	return 0;
}


你可能感兴趣的:(数论,ZOJ,HDU)