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
 
 
题目大意:两个矩阵“相乘”,输出相乘后的矩阵。这里给了一个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;
}

  

 
 

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

  1. HDU 1262 寻找素数对 模拟题

    题目描述:输入一个偶数,判断这个偶数可以由哪两个差值最小的素数相加,输出这两个素数. 题目分析:模拟题,注意的是为了提高效率,在逐个进行判断时,只要从2判断到n/2就可以了,并且最好用打表法判断素数. ...

  2. HDU 2093 考试排名 模拟题

    解题报告: 题目描述:写一个程序给一个编程考试C++实时提交系统排名,给你的数据是题目的总数,每次错误提交罚的时间分,每位用户的姓名,然后是输入用户每题的完成情况,有一下几种情况,第一,输入只有一个正 ...

  3. HDU 2521 反素数 模拟题

    解题报告:水题,直接附上代码,只是觉得这题的作者是不是吃饱了饭撑的,反素数的概念跟这题一点关系都没有. #include<cstdio> int judge1(int k) { ; ;i& ...

  4. HDU 1256 画8 模拟题

    解题报告:这题我觉得题目有一个没有交代清楚的地方就是关于横线的字符的宽度的问题,题目并没有说,事实上题目要求的是在保证下面的圈高度不小于上面的圈的高度的情况下,横线的宽度就是等于下面的圈的高度. #i ...

  5. HDU 4022 Bombing STL 模拟题

    人工模拟.. #include<stdio.h> #include<iostream> #include<algorithm> #include<vector ...

  6. hdu 4515 年月份模拟题

    小Q系列故事——世界上最遥远的距离 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  7. HDU 3787 A+B 模拟题

    解题报告:就是输入两个用逗号隔开的数字,求出这两个数字的和,并且用正常的方式输出来.直接写一个函数将一个包含逗号的数字转换成十进制的数返回就行了.这里推荐一个函数atoi(),参数是char*型的,然 ...

  8. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  9. HDU 4452 Running Rabbits (模拟题)

    题意: 有两只兔子,一只在左上角,一只在右上角,两只兔子有自己的移动速度(每小时),和初始移动方向. 现在有3种可能让他们转向:撞墙:移动过程中撞墙,掉头走未完成的路. 相碰: 两只兔子在K点整(即处 ...

随机推荐

  1. Python中logging模块的基本用法

    在 PyCon 2018 上,Mario Corchero 介绍了在开发过程中如何更方便轻松地记录日志的流程. 整个演讲的内容包括: 为什么日志记录非常重要 日志记录的流程是怎样的 怎样来进行日志记录 ...

  2. Ubuntu安装SHH服务

    1.打开"终端窗口",输入"sudo apt-get update"-->回车-->"输入当前登录用户的管理员密码"--> ...

  3. C# 添加vertical 属性上下边框消失问题

    点击这里的曲别针就好了.... 自定义控件主题..... #学习地址: http://www.cnblogs.com/anding/p/4993655.html

  4. python爬取抖音APP视频教程

    本文讲述爬取抖音APP视频数据(本文未完,后面还有很多地方优化总结) 公众号回复:抖音 即可获取源码 1.APP抓包教程,需要用到fiddler fiddler配置和使用查看>>王者荣耀盒 ...

  5. Error: Duplicate key name 'PCS_STATS_IDX' (state=42000,code=1061) ----Hive schematool -initSchema -dbType mysql

    schematool -initSchema -dbType mysqlMetastore connection URL: jdbc:mysql://localhost/metastore_db?cr ...

  6. my.宝石 --- --- ZC 收集

    1.         DT PT 头盔 太阳石 物理伤害+8     √     月亮石 物理防御+12     √ 武器 太阳石 物理伤害+8   √     舍利子 法术伤害+6         ...

  7. python django bootstrap_导入 201901

    参考 http://www.liujiangblog.com/course/django/124 AdminLTE-2.4.5 http://www.liujiangblog.com/course/d ...

  8. 转 from __future__ import unicode_literals

    转自 https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/0013868200230 ...

  9. Spring MVC自定义错误页面

    在web.xml中添加: <error-page(其他属性404...省略咯)> <location>/error</location> </error-pa ...

  10. webstorm中.vue报错(es6语法报错)-转

    1.webstorm中es6语法报错,解决方法: 打开 Settings => Languages & Frameworks => Javascript把 Javascript L ...