Sicily 1735 Encryption (模拟)

链接:http://soj.me/show_problem.php?pid=1735&cid=

Description
Let me introduce an easy method of encryption to you.
Suppose there’re N bytes (1 byte = 8 bits) data that are to be encrypted and we want to encrypt them in groups of M bytes, while for the last group we may need to add some zeros to make up M bytes. Then we find out an M * M matrix (Aij) m*m. Now for each group, suppose M bytes are X1, X2, …, Xm, and we will generate the encrypted data Y1, Y2, …, Ym from the following equations :
        Yi = (X1 * Ai1) + (X2 * Ai2) + … + (Xm * Aim)
         i = 1 , 2 , 3 , … , m .
Input
There are multiple cases.
For each case, there are two numbers N and M (1<=N<=100, 2<=M<=10) in the first line. In the second line are N numbers to be encrypted (between 0 and 255). While in the following M lines, each line contains M numbers and the jth number in the ith line of these M lines is Aij (0 <= Aij <= 255). Numbers in the same line are separated by spaces.
Input will be ended by end of file.
Output
For each case, output one line of the encrypted data. If the encrypted data contains K numbers, use K-1 spaces to separate them.
Sample Input
 Copy sample input to clipboard
4 2
1 2 3 4
0 1
1 0
1 2
1
3 1
0 1
10 10
100 100 100 100 100 100 100 100 100 100
100 100 100 100 100 100 100 100 100 100
0 1 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 1 0 0 0 0 0
0 0 0 0 0 1 0 0 0 0
0 0 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 1 0 0
0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 0 1
Sample Output
2 1 4 3
3 0
100000 100 100 100 100 100 100 100 100 100

分析: 这道题是一个矩阵加密算法的实现,所谓矩阵加密就是把要加密的数据作为一个向量,乘以一个矩阵,得到的就是被加密的内容,如果需要解密,就乘以一个逆矩阵而还原,这道题直接按题意做加密的处理就可以了;

题目中给出了求Y的公式,而且数据范围也不大,直接根据公式代入就得啦;


代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <string>
#define MAXN 120
#define RST(N)memset(N, 0, sizeof(N))
using namespace std;

int ans, n, m;
int data[MAXN], x[11], a[11][11];

void Init()
{
    RST(data);
    for(int i=1; i<=n; i++) cin >> data[i];
    for(int i=1; i<=m; i++) {
        for(int j=1; j<=m; j++) {
            cin >> a[i][j];
        }
    }
    return ;
}

void solve()
{
    for(int i=1; i<=m; i++) {
        ans = 0;
        for(int j=1; j<=m; j++) ans += a[i][j]*x[j];  //根据公式代入;
        cout << ans;
        if(i < m) cout << " ";
    }
}

int main()
{
    while(cin >> n >> m) {
        for(int i=1; i<=n; i+=m) {
            if(i > 1) cout << " ";
            for(int j=0; j<m; j++) x[j+1] = data[i+j];  //计算x;
            solve();
        }
        cout << endl;
    }
    return 0;
}


你可能感兴趣的:(模拟)