參考http://blog.sina.com.cn/s/blog_8716ef750100wxxb.html
注意:要表示的數字的類型要用long long,在輸入時用"%lld"
//Source Code
//Problem: 1023 Memory: N/A Time: N/A
//Language: C Result: Wrong Answer
#include<stdio.h>
#include<memory.h>
#define LEN 65
struct Funk
{
int k;
char flag[LEN];//存儲p-n串
int used[LEN];//跟着記錄哪些基數被使用
};
struct Funk funk;
int main()
{
int testNum;
int i,j;
long long input;//需要表示的數字
scanf("%d", &testNum);
while(testNum--)
{
memset(&funk,0,sizeof(struct Funk));
j=0;
scanf("%d", &funk.k);
scanf("%s", funk.flag);
scanf("%d", &input);
for(i=funk.k-1; i>=0; i--)
{
if(input%2 != 0)//input是奇數
{
if(funk.flag[i] == 'p')
input = (input-1);
else if(funk.flag[i] == 'n')
input = (input+1);
input /= 2;
funk.used[j++] = 1;
}
else
{
input /= 2;
funk.used[j++] = 0;
}
}
if(input == 0)
{
for(i=funk.k-1; i>=0; i--)
{
printf("%d", funk.used[i]);
}
}
else
printf("Impossible");
printf("\n");
}
return 0;
}