And Now, a Remainder from Our Sponsor (中国剩余定理互质情况)

And Now, a Remainder from Our Sponsor

 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

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

题意:

这是个非常有意思的破解密码的题目,给你已经加密的密文,用数字表示的,让你破译密文

题目告诉了具体生成的密码过程:

1)现将字母对应成2位数字,然后每三个字符合成一个6位数(当然了有前导零的计算时会去掉)

2)给了四个两位的数字,让每个六位数取模这四个数字得到四个值,四个两位数(可能有前导零)然后四个拼成一个数(最终会去掉前导零)

最终题目就给你四个取模的两位数,和每个取模后的结果合成的数

让我们破译密码

分析:

我们要破译密码肯定是要倒着求解,求出原来的数字是什么,然后对应回字母即可,这个过程很明显就是裸的中国剩余定理,并且题目告诉我们取模的数互质,因此就是直接套用一般情况的模板。

就是分解的过程恶心点,其他还倒好说,注意最后不能有空格

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
ll a[10],m[10];
ll c[60];
char mark[30]="AABCDEFGHIJKLMNOPQRSTUVWXYZ ";
char s[200];
ll n;
ll ex_gcd(ll a,ll b,ll &x,ll &y){
    if(!b){
        x = 1;
        y = 0;
        return a;
    }
    ll g = ex_gcd(b,a%b,y,x);
    y -= a / b * x;
    return g;
}
ll CRT(){
    ll i,d,R,y,M,lcm = 1,sum = 0;
    for(int i = 0; i < 4; i++){
        lcm *= m[i];
    }
    for(int i = 0; i < 4; i++){
        M = lcm / m[i];
        d = ex_gcd(M,m[i],R,y);
        sum = (sum + R * M * a[i] % lcm) % lcm;
    }
    return (sum % lcm + lcm) % lcm;
}

int main(){
    ll tmp,T;
    scanf("%lld",&T);
    while(T--){
        scanf("%lld",&n);
        for(int i = 0; i < 4; i++){
            scanf("%lld",&m[i]);
        }
        for(int i = 0; i < n; i++){
            scanf("%lld",&tmp);
            for(int j = 3; j >= 0; j--){//将给定数字分成四个数即四个取模后的数
                a[j] = tmp % 100;
                tmp /= 100;
            }
            ll x = CRT();//利用中国剩余定理求出一个加密的六位数
            c[i] = x;
        }
        int len = 0;
        for(int i = 0; i < n; i++){
            vector<int>q;
            tmp = c[i];
            while(tmp){
                q.push_back(tmp%100);
                tmp /= 100;
            }//将得到的六位数两个两个分开
            while(!q.empty()){
                tmp = q.back();
                q.pop_back();
                s[len++] = mark[tmp];//然后一一对应到字母
            }
        }
        while(s[len-1] == ' ') len--;//除去末尾空格
        for(int i = 0; i < len; i++){//输出
            putchar(s[i]);
        }
        puts("");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值