求解一道PKU上的一道题,急需理论支持

来源:百度知道 编辑:UC知道 时间:2024/06/01 20:31:34
The Fun Number System
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2532 Accepted: 702

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 s

没太看明白你是什么意思
不过这样的代码AC了

#include<stdio.h>

__int64 m;
int n,a[100],b[100];

int main(){
char x[100];
int tt,test,i,j;
scanf("%d",&test);
for (tt=1;tt<=test;tt++) {
scanf("%d",&n);
if (n>0) scanf("%s",x);
for (i=n-1;i>=0;i--) {
if (x[i]=='p') a[n-1-i]=1;
else a[n-1-i]=-1;
}
scanf("%I64d",&m);
for (i=0;i<n;i++)
if (m%2!=0) {
m=(m-a[i])/2;
b[i]=1;
}
else {
b[i]=0;
m=m/2;
}
if (m!=0) printf("Impossible\n");
else {
j=n-1;
for (i=j;i>=0;i--)
printf("%d",b[i]);
printf("\n");
}
}
return 0;
}