#HDU1930#And Now, a Remainder from Our Sponsor(中国剩余定理)

来源:互联网 发布:银川古装摄影推荐知乎 编辑:程序博客网 时间:2024/05/24 06:14

And Now, a Remainder from Our Sponsor

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 950    Accepted Submission(s): 422


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

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.

Output
For each test case write the decoded message. You should not print any trailing blanks.

Sample Input
2634 81 65 43 1062038 1043103 1473907 22794503 15135731 16114011320 31 53 39 5184133 14080210 7090922
 
Sample Output
THE CAT IN THE HATTHE END

题意:

给出4个模数,每一个大数字都是同一个6位数的4个余数组成的(分别mod那4个模数),

然后6位数中每两个数字对应一个字符(A~Z从01编号到26,空格为27),

求原来那句话。


经典的中国剩余定理的裸题,题中只有4个数甚至可以强行手打表。

Code:

StatusAcceptedMemory1688kBLength1297LangG++Submitted2017-07-07 19:25:58SharedRemoteRunId21021148

#include<iostream>#include<cstdio>using namespace std;int N;int Mod[5], s[5], ans[5];bool vis[5];void Gcd(int a, int b, int & d, int & x, int & y){if(! b){d = a, x = 1, y = 0;}else {Gcd(b, a % b, d, y, x);y -= x * (a / b);}}void trans(){int c, sum = 0;int d, x, y;for(int i = 1; i <= 4; ++ i){c = 1;vis[i] = 1;for(int j = 1; j <= 4; ++ j)if(! vis[j])vis[j] = 1, c *= Mod[j];Gcd(c, Mod[i], d, x, y);x *= c;sum += x * s[i];vis[1] = vis[2] = vis[3] = vis[4] = 0;}sum %= Mod[0];while(sum < 272727 && sum % 1000000 < 10101)sum += Mod[0];while(sum > 272727)sum -= Mod[0];int tot = 3;while(tot){ans[tot] = 0;ans[tot] = sum % 100;sum /= 100;-- tot;}}void print(int p){int tot = 3;while(p == N && ans[tot] == 27)-- tot;for(int i = 1; i <= tot; ++ i)if(ans[i] != 27)printf("%c", 'A' + ans[i] - 1);else putchar(32);}int main(){int T;scanf("%d", &T);while(T --){scanf("%d", &N);Mod[0] = 1;for(int i = 1; i <= 4; ++ i)scanf("%d", & Mod[i]), Mod[0] *= Mod[i];for(int i = 1; i <= N; ++ i){scanf("%d", &s[0]);s[4] = s[0] % 100;s[3] = s[0] / 100 % 100;s[2] = s[0] / 10000 % 100;s[1] = s[0] / 1000000;trans();print(i);}putchar(10);}return 0;}









阅读全文
0 0
原创粉丝点击