组合数学
注意列号中的字母是无序的,可以想到求组合数C(26,1) C(26,2) ... C(26,26)
! ! ! 行列有数据0的情况,R ,n1 ,R, n2 和 C, n1, C, n2 为非合法数据,需要判断
// TYVJ 1725-IXCEL表格
#include <iostream>
#include <algorithm>
using namespace std;
int sum[30], zuhe[30][30];
int n, m, n1, n2;
int pos[7] = {0,26,676,17576,456976,11881376,308915776};
char x1, x2;
void create()
{
int i, j;
for(i = 0; i != 27; ++i)
zuhe[i][0] = zuhe[i][i] = 1;
for(i = 1; i != 27; ++i)
for(j = 1; j != i; ++j)
zuhe[i][j] = zuhe[i - 1][j] + zuhe[i - 1][j - 1];
}
string calr(int n1)
{
int i, len = 0;
int temp_first_letter;
string str = "";
for(i = 1; i < 8; ++i)
sum[i] = sum[i - 1] + pos[i];
for(i = 1; i < 8; ++i)
if(sum[i - 1] < n1 && n1 <= sum[i]) {
len = i;
break;
}
n1 -= sum[i - 1];
for(i = len - 1; i >= 1; --i) {
temp_first_letter = 0;
while(n1 - pos[i] > 0) {
n1 -= pos[i];
++temp_first_letter;
}
str += char('A' + temp_first_letter);
}
str += char('A' + n1 - 1);
return str;
}
string calc(int n2) {
int i, len = 0;
int temp_first_letter = -1;
string str = "";
for(i = 1; i < 28; ++i)
sum[i] = sum[i - 1] + zuhe[26][i];
for(i = 1; i < 28; ++i)
if(sum[i - 1] < n2 && n2 <= sum[i]) {
len = i;
break;
}
n2 -= sum[i - 1];
for(i = len - 1; i >= 1; --i) {
++temp_first_letter;
while(zuhe[25 - temp_first_letter][i] < n2) {
n2 -= zuhe[25 - temp_first_letter][i];
++temp_first_letter;
}
str += char('A' + temp_first_letter);
}
str += char('A' + temp_first_letter + n2);
return str;
}
void run()
{
int i;
create();
while(cin>>m>>n) {
for(i = 0; i != n; ++i) {
cin>>x1>>n1>>x2>>n2;
bool sign = false;
if((x1 == 'R' && x2 == 'R' || x1 == 'C' && x2 == 'C') || n1 <= 0 || n2 <= 0)
sign = true;
else {
if(x1 != 'R')
swap(n1, n2);
if(n1 > m || n2 > 67108863 || n2 > m)
sign = true;
}
if(sign)
cout<<"-1"<<endl;
else
cout<<calr(n1)<<' '<<calc(n2)<<endl;
}
}
}
int main()
{
run();
return 0;
}
/*
123 3
R 26 C 27
R 27 C 26
R 124 C 10
*/