The Fun Number System
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10829 | Accepted: 3674 |
Description
In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight of the most significant bit (i.e., in position k-1), is -2^(k-1), and the weight of a bit in any position i (0 ≤ i < k-1) is 2^i. For example, a 3 bit number 101 is -2^2 + 0 + 2^0 = -3. A negatively weighted bit is called a negabit (such as the most significant bit in a 2's complement number), and a positively weighted bit is called a posibit.
A Fun number system is a positional binary number system, where each bit can be either a negabit, or a posibit. For example consider a 3-bit fun number system Fun3, where bits in positions 0, and 2 are posibits, and the bit in position 1 is a negabit. (110)Fun3 is evaluated as 2^2-2^1 + 0 = 3. Now you are going to have fun with the Fun number systems! You are given the description of a k-bit Fun number system Funk, and an integer N (possibly negative. You should determine the k bits of a representation of N in Funk, or report that it is not possible to represent the given N in the given Funk. For example, a representation of -1 in the Fun3 number system (defined above), is 011 (evaluated as 0 - 2^1 + 2^0), and
representing 6 in Fun3 is impossible.
A Fun number system is a positional binary number system, where each bit can be either a negabit, or a posibit. For example consider a 3-bit fun number system Fun3, where bits in positions 0, and 2 are posibits, and the bit in position 1 is a negabit. (110)Fun3 is evaluated as 2^2-2^1 + 0 = 3. Now you are going to have fun with the Fun number systems! You are given the description of a k-bit Fun number system Funk, and an integer N (possibly negative. You should determine the k bits of a representation of N in Funk, or report that it is not possible to represent the given N in the given Funk. For example, a representation of -1 in the Fun3 number system (defined above), is 011 (evaluated as 0 - 2^1 + 2^0), and
representing 6 in Fun3 is impossible.
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by the input data for each test case. Each test case is given in three consecutive lines. In the first line there is a positive integer k (1 ≤ k ≤ 64). In the second line of a test data there is a string of length k, composed only of letters n, and p, describing the Fun number system for that test data, where each n (p) indicates that the bit in that position is a negabit (posibit).
The third line of each test data contains an integer N (-2^63 ≤ N < 2^63), the number to be represented in the Funk number
system by your program.
The third line of each test data contains an integer N (-2^63 ≤ N < 2^63), the number to be represented in the Funk number
system by your program.
Output
For each test data, you should print one line containing either a k-bit string representing the given number N in the Funk number system, or the word Impossible, when it is impossible to represent the given number.
Sample Input
2 3 pnp 6 4 ppnn 10
Sample Output
Impossible 1110
大致题意:
我们知道正常的二进制,第k位的位权是2的k-1次方。这里给出一个奇特的计数系统。给定k个字母,每个字母为p或者n。从右往左数,第k位若是p,则位权为2的k-1次方,若是n,则位权为-2的k-1次方。问题是,给出一组pn序列,求在这样的计数系统下,能否表示给定的十进制数字n。若能,输出该表示。否则,输出Impossible。
大体思路:
整体上就是模拟二进制的表示。
先讨论n为正数的情况。
1)把给定的数字转化为2进制。
2)把二进制从低位到高位依次转化为特殊进制。转化规则如下:
若该位上的数字为0,那么结果中该位也是0。
若该位是1,且位权为p,则结果中的该位也是1。
若该位是1,且位权为n,则这个1应看做从前一位借位后减去本位上的1得到的。所以这种情况的处理方法是,结果中的该位填1,原二进制的下一位加1。
以上步骤中的二进制表示和结果可以同时在一个数组中表示出来。
若n为负数,处理方法不变。只需要把对p、n的操作互换就可以了。
解题过程:
一开始没有初始化数组,导致处理进位时混乱了。
#include<cstdio>
#include<cstring>
char A[70];
int In[70],Ans[70];
int t,k,inn;
long long N;
char x;
bool po;
int main()
{
//freopen("in.txt","r",stdin);
int i;
scanf("%d",&t);//样例数
while(t--)
{
memset(In,0,sizeof(In));//初始化数组
scanf("%d",&k);//位数
for(i=k-1;i>=0;i--)
scanf(" %c",&A[i]);//输入p/n
scanf("%I64d",&N);//输入待表示的数字
if(N<0) N=-N,po=0;//判断正负
else po=1;
inn=0;
while(N)//转化为正常的二进制
In[inn++]=N%2,N/=2;
if(po==1) for(i=0;i<inn;i++)//处理正数
{
if(In[i]>=2){//若不符合二级制产生进位动作
if(i==inn-1) inn++;
In[i]-=2,++In[i+1],--i;//这里的i--的意思是continue继续循环后仍处理当前位
continue;
}
if(In[i]==0) Ans[i]=0;
else if(A[i]=='p') Ans[i]=1;
else if(A[i]=='n') {
Ans[i]=1,++In[i+1];
if(i==inn-1) inn++;
}
}
else for(i=0;i<inn;i++)//处理负数的情况
{
if(In[i]>=2){
if(i==inn-1) inn++;
In[i]-=2,++In[i+1],--i;
continue;
}
if(In[i]==0) Ans[i]=0;
else if(A[i]=='n') Ans[i]=1;//此处的操作与正数相反
else if(A[i]=='p'){
Ans[i]=1,++In[i+1];
if(i==inn-1) inn++;
}
}
if(inn>k) printf("Impossible\n");
else {
for(i=inn;i<k;i++) Ans[i]=0;
for(i=k-1;i>=0;i--)
printf("%d",Ans[i]);
printf("\n");
}
}
return 0;
}