hdu 1930 中国剩余定理(互质模板题)
Posted vainglory
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 1930 中国剩余定理(互质模板题)相关的知识,希望对你有一定的参考价值。
And Now, a Remainder from Our Sponsor
Time Limit : 5000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 23 Accepted Submission(s) : 13
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.
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
2007 East Central North America
#include<bits/stdc++.h> using namespace std; const int N=100; int m[5],aa[N],a[N][10],ans[N]; char s[1000];int cnt; int exgcd(int a, int b, int &x, int &y){ int d = a; if(b != 0){ d = exgcd(b, a%b, y, x); y -= (a / b) * x; } else{ x = 1; y = 0; } return d; } int China(int n) { int M = 1,ans = 0; for(int i = 1; i <=4; i++) M *= m[i]; for(int i=1; i<=4; i++){ int x,y,Mi = M / m[i]; exgcd(Mi, m[i], x, y); ans = (ans + Mi * x * a[n][i]) % M; } if(ans < 0) ans += M; return ans; } void change(int n){ for(int i=1;i<=n;i++){ for(int j=4;j>=1;j--){ a[i][j]=aa[i]%100; aa[i]/=100; } } } void solve(int n){ for(int i=1;i<=n;i++) ans[i]=China(i); //for(int i=1;i<=n;i++) cout<<ans[i]<<endl; for(int i=1;i<=n;i++){ for(int j=3;j>=1;j--){ int tmp=ans[i]%100; if(tmp==27) s[cnt+j]=‘ ‘; else s[cnt+j]=‘A‘-1+tmp; ans[i]/=100; } cnt+=3; } while(s[cnt]==‘ ‘) cnt--; for(int i=1;i<=cnt;i++) printf("%c",s[i]); printf(" "); } int main(){ int t,n;scanf("%d",&t); while(t--){ scanf("%d",&n);cnt=0; for(int i=1;i<=4;i++) scanf("%d",&m[i]); for(int i=1;i<=n;i++) scanf("%d",&aa[i]); change(n); solve(n); } return 0; }
以上是关于hdu 1930 中国剩余定理(互质模板题)的主要内容,如果未能解决你的问题,请参考以下文章