HDU 4364——Matrix operation——————【模拟题】

Matrix operation

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 907    Accepted Submission(s): 384

Problem Description

  M_0 is interested in cryptography. Recently he learned the Advanced Encryption Standard (AES). AES operates on a 4×4 column-major order matrix of bytes and he found it that there is a step called the MixColumns step in the AES.
  In the MixColumns step, there is a state matrix, the four bytes of each column of the state are combined using an invertible linear transformation. The MixColumns function takes four bytes as input and outputs four bytes, where each input byte affects all four output bytes. Together with ShiftRows, MixColumns provides diffusion in the cipher.
  During this operation, each column is multiplied by the known matrix that for the 128 bit key is:

  The addition operation is defined as: xor.
  The multiplication operation is defined as: 
  multiplication by 1 means no change
  multiplication by 2 means shifting to the left
  multiplication by 3 means shifting to the left and then performing xor with the initial unshifted value. 
  Notice:After each shifting, a conditional xor with 0x1B should be performed if the shifted value is larger than 0xFF.

Input

There are several cases.
The first line is an integer T (T <= 20000), indicating the test cases. 
Then is the state matrix, each case followed by four lines, each line contains four bytes, separated by spaces.

Output

For each case, output the new matrix of the state matrix after the MixColumns step. The output data for two different test cases should be separated by an empty line.

Sample Input

1

00 01 02 03

04 05 06 07

08 09 0A 0B

0C 0D 0E 0F

Sample Output

08 09 0A 0B

1C 1D 1E 1F

00 01 02 03

14 15 16 17

Author

FZU

Source

2012 Multi-University Training Contest 7

题目大意:两个矩阵“相乘”,输出相乘后的矩阵。这里给了一个key矩阵,表示秘钥矩阵。下面给出t组数据,每组都是4*4的矩阵,用key矩阵乘以matrix得到结果,矩阵不改变,只是用矩阵中的值进行操作,这里加法用Xor代替,如果key矩阵中是1,那么就不对matrix中的值操作,如果是2,就让matrix中的值左移一位,如果是3,就让matrix中的值左移一位,并且与没有左移的值进行异或。左移可能会超出8位能表示的最大值,所以如果结果大于0xFF,就跟0x1B异或,然后对256取余。结果是大写字母,所以输出时应该是%02X输出,不能用%02x输出。

#include<bits/stdc++.h>
using namespace std;
int key[4][4]={2,3,1,1,1,2,3,1,1,1,2,3,3,1,1,2};
int ini[4][4],ans[4][4];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                scanf("%x",&ini[i][j]);
            }
        }
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                int a,tmp=0;
                for(int k=0;k<4;k++){
                    if(key[i][k]==1){
                        a=ini[k][j];
                    }else if(key[i][k]==2){
                            a=ini[k][j];
                            a<<=1;
                            if(a>0xFF){
                                a^=0x1B;
                                a%=(0xFF+1);
                            }
                    }else if(key[i][k]==3){
                            a=ini[k][j];
                            a<<=1;
                            if(a>0xFF){
                                a^=0x1B;
                                a%=(0xFF+1);
                            }
                            a^=ini[k][j];
                    }
                    tmp^=a;
                }
                ans[i][j]=tmp;
            }
        }
        for(int i=0;i<4;i++){
            for(int j=0;j<4;j++){
                printf("%02x%c",ans[i][j],j==3?‘\n‘:‘ ‘);
            }
        }
        if(t)
            puts("");
    }
    return 0;
}

  

时间: 2024-07-31 21:12:02

HDU 4364——Matrix operation——————【模拟题】的相关文章

HDU 4022 Bombing STL 模拟题

手动模拟.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define N 10100 #define inf 1000000010 map<

hdu 5671 Matrix(BC——思维题)

题目链接:acm.hdu.edu.cn/showproblem.php?pid=5671 Matrix Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 891    Accepted Submission(s): 371 Problem Description There is a matrix M that has n rows

hdu 4119 Isabella&#39;s Message 模拟题

Isabella's Message Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4119 Description Isabella and Steve are very good friends, and they often write letters to each other. They exchange funny experiences, talk ab

HDU多校联合赛(1007 Magical Forest)模拟题

题目: Problem Description There is a forest can be seen as N * M grid. In this forest, there is some magical fruits, These fruits can provide a lot of energy, Each fruit has its location(Xi, Yi) and the energy can be provided Ci. However, the forest wi

HDU 4772 Zhuge Liang&#39;s Password (简单模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4772 题面: Zhuge Liang's Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1404    Accepted Submission(s): 926 Problem Description In the anc

HDU 4028 The time of a day STL 模拟题

暴力出奇迹.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector> #include<cmath> #include<queue> #include<set> #include<map> using namespace std; #define ll __int64 #define N 42 ll n,m,ans;

hdu 5641 King&#39;s Phone(暴力模拟题)

Problem Description In a military parade, the King sees lots of new things, including an Andriod Phone. He becomes interested in the pattern lock screen. The pattern interface is a 3×3 square lattice, the three points in the first line are labeled as

HDU 4930 Fighting the Landlords(扯淡模拟题)

Fighting the Landlords 大意: 斗地主....   分别给出两把手牌,肯定都合法.每张牌大小顺序是Y (i.e. colored Joker) > X (i.e. Black & White Joker) > 2 > A (Ace) > K (King) > Q (Queen) > J (Jack) > T (10) > 9 > 8 > 7 > 6 > 5 > 4 > 3. 给你8种组合:1.

HDU 4925 Apple Tree(模拟题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4925 解题报告:给你n*m的土地,现在对每一块土地有两种操作,最多只能在每块土地上进行两种操作,第一种是种苹果树操作,第二种是施肥操作,种苹果树操作可以使得该块地 长出一个苹果,施肥操作可以使得与这块土地相邻的土地的苹果产量变为原来的两倍,问可以得到的最多的苹果数量是多少? 例如一个4*4的土地,用1表示在该土地上做第一种操作,0表示在该土地上做第二种操作,可以得到最多苹果的操作如下: 0 1 0