And Now, a Remainder from Our Sponsor
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 380 Accepted Submission(s): 153
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
Recommend
威士忌
这题典型的中国剩余定理题,一遍样例过了,交上去PE了,要注意最后如果是空格的就不输出。
#include<iostream>
#include<cstdlib>
#include<stdio.h>
#define ll __int64
using namespace std;
int mm[5];
int uu[55];
int yy[55][5];
int n;
int res[55];
char cc[4];
ll x,y,ti;
void zhuanhuan()
{
for(int i=1;i<=n;i++)
for(int j=4;j>=1;j--)
{
yy[i][j]=uu[i]%100;
uu[i]/=100;
}
}
void change()
{
for(int i=1;i<=n;i++)
{
int d=0;
for(int j=1;j<=3;j++)
{
int oo=res[i]%100;
res[i]/=100;
if(oo==27)
{
cc[d++]=' ';
}
else
{
oo--;
cc[d++]=char(oo+'A');
}
}
for(int k=d-1;k>=0;k--)
{
if(i==n)
{
if(cc[k]==' ')
break;
else
putchar(cc[k]);
}
else
putchar(cc[k]);
}
}
printf("\n");
}
ll egcd(ll a,ll b)
{
if(b==0){x=1;y=0;return a;}
else{ll e=egcd(b,a%b);ti=x;x=y;y=ti-a/b*y;return e;}
}
void solve()
{
ll d,c,t;
for(int i=1;i<=n;i++)
{
int m1=mm[1];
int r1=yy[i][1];
for(int j=2;j<=4;j++)
{
d=egcd(m1,mm[j]);
c=yy[i][j]-r1;
t=mm[j]/d;
x=(c/d*x%t+t)%t;
r1=m1*x+r1;
m1=m1*mm[j]/d;
}
res[i]=r1;
}
/*for(int i=1;i<=n;i++)
cout<<res[i]<<" ";
cout<<endl;*/
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=1;i<=4;i++)
scanf("%d",&mm[i]);
for(int i=1;i<=n;i++)
scanf("%d",&uu[i]);
zhuanhuan();
solve();
change();
}
}