And Now, a Remainder from Our Sponsor
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 669 Accepted Submission(s): 288
Problem Description
IBM has decided that all messages sent to and from teams competing in the ACM programming contest should be encoded. They have decided that instead of sending the letters of a message, they will transmit their remainders relative to some secret keys which are four, two-digit integers that are pairwise relatively prime. For example, consider the message "THE CAT IN THE HAT". The letters of this message are first converted into numeric equivalents, where A=01, B=02, ..., Z=26 and a blank=27. Each group of 3 letters is then combined to create a 6 digit number. (If the last group does not contain 3 letters it is padded on the right with blanks and then transformed into a 6 digit number.) For example
THE CAT IN THE HAT → 200805 270301 202709 142720 080527 080120
Each six-digit integer is then encoded by replacing it with the remainders modulo the secret keys as follows: Each remainder should be padded with leading 0’s, if necessary, to make it two digits long. After this, the remainders are concatenated together and then any leading 0’s are removed. For example, if the secret keys are 34, 81, 65, and 43, then the first integer 200805 would have remainders 1, 6, 20 and 38. Following the rules above, these combine to get the encoding 1062038. The entire sample message above would be encoded as
1062038 1043103 1473907 22794503 15135731 16114011
THE CAT IN THE HAT → 200805 270301 202709 142720 080527 080120
Each six-digit integer is then encoded by replacing it with the remainders modulo the secret keys as follows: Each remainder should be padded with leading 0’s, if necessary, to make it two digits long. After this, the remainders are concatenated together and then any leading 0’s are removed. For example, if the secret keys are 34, 81, 65, and 43, then the first integer 200805 would have remainders 1, 6, 20 and 38. Following the rules above, these combine to get the encoding 1062038. The entire sample message above would be encoded as
1062038 1043103 1473907 22794503 15135731 16114011
Input
The input consists of multiple test cases. The first line of input consists of a single positive integer n indicating the number of test cases. The next 2n lines of the input consist of the test cases. The first line of each test case contains a positive integer (< 50) giving the number of groups in the encoded message. The second line of each test case consists of the four keys followed by the encoded message.
Each message group is separated with a space.
Each message group is separated with a space.
Output
For each test case write the decoded message. You should not print any trailing blanks.
Sample Input
2 6 34 81 65 43 1062038 1043103 1473907 22794503 15135731 16114011 3 20 31 53 39 5184133 14080210 7090922
Sample Output
THE CAT IN THE HAT THE END
Source
很奇怪的一题啊,思路比较简单,就是非互质的中国剩余定理,还有就是在输出方面也是很奇怪,密码解出来可能最后会出现空格,但是最后的空格要特判消除(MD什么意思……明明是你让我解出来的……)。套中国剩余定理的模板课轻松解决。
#include <cstring>
#include <cstdio>
const int N = 2005 ;
int key[N], num, n ;
int str[N] ;
int res[N] ;
int r[N] ;
void input ()
{
int i ;
scanf ("%d", &n) ;
memset (res, 0, sizeof (res)) ;
memset (str, 0, sizeof (str)) ;
memset (r, 0, sizeof (r)) ;
memset (key, 0, sizeof (key)) ;
num = 0 ;
for (i = 0; i < 4; i ++) // key
scanf ("%d", &key[i]) ;
for (i = 0; i < n; i ++) // the set of remainder
scanf ("%d", &str[i]) ;
}
void exGcd (int a, int b, int &d, int &x, int &y) // Extended_Euclid
{
if (b == 0)
{
d = a ;
x = 1 ;
y = 0 ;
return ;
}
exGcd (b, a%b, d, x, y) ;
int tmp = x ;
x = y ;
y = tmp - (a/b)*y ;
}
void getNum ()
{
int i, j ;
int a, b, c, d, x, y ;
for (i = 0; i < n; i ++)
{
r[3] = str[i] % 100 ; //transform the set of remainder into the
r[2] = (str[i]%10000)/100 ; //single remainder of the key
r[1] = (str[i]%1000000)/ 10000 ;
r[0] = str[i]/1000000;
int ta = key[0] ;
int tr = r[0] ;
//mission: x = r1 (mod a1), x = r2 (mod a2), ..., find x ;
for (j = 1; j < 4; j ++) //a1, a2, ... are key[i], r1, r2, ... are r[i]
{ //find the str[i]'s value
a = ta, b = key[j] ;
c = r[j] - tr ;
exGcd (a, b, d, x, y) ;
int t = b/d ;
x = (x*(c/d)%t + t) % t ;
tr = ta*x + tr ;
ta = ta * (key[j]/d) ;
}
res[i] = tr ;
}
}
void output ()
{
int i ;
int a, b, c ;
char destr [10005] ;
int len = 0 ;
for (i = 0; i < n; i ++)
{
a = res[i] / 10000 ;
b = (res[i] % 10000) / 100 ;
c = res[i] % 100 ;
if (a != 27)
destr[len++] = 'A' + a - 1 ;
else destr[len++] = ' ' ;
if (b != 27)
destr[len++] = 'A' + b - 1 ;
else destr[len ++] = ' ' ;
if (c != 27)
destr[len++] = 'A' + c - 1 ;
else destr[len ++] = ' ' ;
}
while (destr[len-1] == ' ') //ignore the blank in the end of text.
len -- ;
for (i = 0; i < len; i ++)
printf ("%c", destr[i]) ;
printf ("\n") ;
}
int main ()
{
int tcase ;
scanf ("%d", &tcase) ;
while (tcase --)
{
input () ;
getNum () ;
output () ;
}
return 0 ;
}