您的位置:首页 > 其它

一些关于中国剩余定理的数论题(POJ 2891/HDU 3579/HDU 1573/HDU 1930)

2013-07-30 03:01 671 查看
2891 -- Strange Way to Express Integers

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>

using namespace std;

typedef long long LL;
inline LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a;}
inline LL lcm(LL a, LL b) { return a / gcd(a, b) * b;}
void gcd(LL a, LL b, LL &d, LL &x, LL &y) {
if (b) { gcd(b, a % b, d, y, x); y -= a / b * x;}
else d = a, x = 1, y = 0;
}

LL A[6], R[6];

bool check(LL a) {
for (int i = 0; i < 3; i++) {
if (a % 100 > 27 || a % 100 <= 0) return false;
a /= 100;
}
return true;
}

LL cal() {
LL d, x, y, a = A[0], r = R[0];
for (int i = 1; i < 4; i++) {
LL dr = R[i] - r;
gcd(a, A[i], d, x, y);
if (dr % d) {
puts("WTF??");
while (1) ;
}
LL tmp = a * x;
//a = lcm(a, A[i]);
a *= A[i];
tmp %= a;
tmp *= dr / d;
tmp += r;
r = (tmp % a + a) % a;
//cout << a << '~' << r << endl;
}
while (!check(r)) r += a;
//cout << r << ' ' << a << endl;
return r;
}

LL cal(LL x) {
for (int i = 0; i < 4; i++) R[i] = x % 100, x /= 100;
//for (int i = 0; i < 4; i++) cout << A[i] << ' ' << R[i] << endl;
return cal();
}

const LL ep[3] = { 10000, 100, 1};
char out[111111];

int main() {
//freopen("in", "r", stdin);
int T, n, p;
LL tmp, x;
cin >> T;
while (T-- && cin >> n) {
p = 0;
for (int i = 0; i < 4; i++) cin >> A[i];
reverse(A, A + 4);
for (int i = 0; i < n; i++) {
cin >> tmp;
tmp = cal(tmp);
for (int i = 0; i < 3; i++) {
x = tmp / ep[i] % 100;
out[p++] = x == 27 ? ' ' : (x - 1 + 'A');
}
}
out[p] = 0;
while (p > 0 && out[p - 1] == ' ') out[--p] = 0;
puts(out);
}
return 0;
}


View Code

——written by Lyon
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: