Result: Accepted
Time: 21ms
Memory: 1716kB
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main(){
int i,b[9],c,d,j=0,e=1,w=0,y,T;
cin>>T;
while(T--){
string a,s="",q,f="";
j=w=0;
e=1;
cin>>a;
cin>>c>>d;
for(i=0;i<a.size();i++){
if(isdigit(a[i])){
b[i]=a[i]-'0';
}
else
b[i]=a[i]-'a'+10;
}
while(d>0){
s+=('0'+d%2);
d/=2;
}
for(i=0;i<c;i++){
if(i>=s.size())
s+="0";
}
reverse(s.begin(),s.end());
for(i=1;i<=c;i++){
q+=s[i-1];
if(i%4==0){
b[j]=(q[0]-'0')*8+(q[1]-'0')*4+(q[2]-'0')*2+(q[3]-'0')*1;
q="";
j++;
}
}
y=b[j];
while(y>0){
f+=('0'+y%2);
y/=2;
}
while(f.size()<4){
f+='0';
}
reverse(f.begin(),f.end());
for(i=q.size()-1;i>=0;i--){
f[i]=q[i];
}
b[j]=(f[3]-'0')*1+(f[2]-'0')*2+(f[1]-'0')*4+(f[0]-'0')*8;
for(i=0;i<8;i++){
if(b[i]>=10)
printf("%c",'a'+b[i]-10);
else
printf("%d",b[i]);
}
printf("\n");
}}