prob: http://www.codechef.com/problems/ARRANGE
the same code below works fine and outputs what is expected at http://www.compileonline.com
however, it gives “runtime error” on both ideone, and codechef. dont know what is the problem
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
int a[65536][16], flag;
for(int i=0; i<16; i++) a[0][i]=0;
for(int i=1; i<65536; i++) {
flag=0;
for(int j=15; j>=0; j--) {
if(a[i-1][j]==0 && flag==0) {
a[i][j]=1;
flag=1;
}
else if(a[i-1][j]==1 && flag==0) {
a[i][j]=0;
}
else if(flag==1) {
a[i][j]=a[i-1][j];
}
}
}
int t, k, b[65536][16], cnt, d;
char s[65536], c[65536];
scanf("%d", &t);
while(t--) {
scanf("%d%s", &k, s);
d=pow(2, k);
for(int i=0; i<d; i++) {
for(int j=15, m=0; m<k; m++, j--) {
b[i][m]=a[i][j];
}
}
for(int i=0; i<d; i++) {
for(int j=0; j<d; j++) {
cnt=0;
for(int m=0; m<k; m++) {
if(b[i][m]==a[j][16-k+m]) cnt++;
}
if(cnt==k) {
c[i]=s[j];
break;
}
}
}
printf("%s\n", c);
}
return 0;
}