跳到主要內容

uva 10705

換成二進位 負的加回來就好

麻煩的是overflow問題

//============================================================================
// Name : The Fun Number System.cpp
// Date : 2013/5/1 上午11:35:24
// Author : GCA
//============================================================================
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
#ifdef ONLINE_JUDGE
#define ll "%lld"
#else
#define ll "%I64d"
#endif
typedef unsigned int uint;
typedef long long Int;
typedef unsigned long long Uint;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 10011
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
Int n;
int bitsize;
char str[10000];
char ans[10000];
bool solve(){
Int Max=0,Min=0;
int minus=0;
Int b;
for(int i=bitsize-1;i>=0;i--){
b=(Int)(1)<<(bitsize-i-1);
if(str[i]=='p')Max+=b;
else Min+=b;
}
if(Max<0)Max=LLONG_MAX;
Min=-Min;
if(Min>0)Min=LLONG_MIN;
if(n>Max)return false;
else if(n<Min)return false;
if(n<0){
minus=1;
n=-n;
}
// printf("%I64d %I64d %I64d %d\n",Max,Min,n,minus);
// printf("%d %d %I64d\n",Max,Min,n);
// for(int i=bitsize-1;i>=0;i--){
// printf("%I64d",(n>>i)&1);
// }Pln();
for(int i=0;i<bitsize;i++){
Int tmp=(Int)(1)<<i;
if(str[bitsize-i-1]=='n'&&((n>>i)&1)==1&&minus==0){
n+=tmp;
n+=tmp;

}else if(str[bitsize-i-1]=='p'&&((n>>i)&1)==1&&minus==1){
n+=tmp;
n+=tmp;
}
// printf("%d>> ",i);
// for(int j=bitsize-1;j>=0;j--){
// printf("%I64d",(n>>j)&1);
// }printf(" %I64d %I64d %c",n,tmp,str[bitsize-i-1]);Pln();
}
for(int i=bitsize-1;i>=0;i--){
printf("%lld",(n>>i)&1);
}Pln();
return true;
}
int main() {
int test;
ios_base::sync_with_stdio(0);
scanf("%d",&test);
while(test--){
scanf("%d %s %lld",&bitsize,str,&n);
if(!solve())printf("Impossible\n");
}

















}

留言