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.

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.

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

Source

Tehran 2002, First Iran Nationwide Internet Programming Contest
 
    题目大致意思:恩......给你一个串,只由n和p组成,再给你一个数字,让你求一个二进制序列使得这个二进制序列按照那个串的规则变换后得到给定的数字。
    其中如果碰到p,那就是 二进制序列的第i个数 * 2^i   否则就是 二进制数序列的第i个数 * -2^i
    分析:
    1.我也不知道怎么做,看别人的......
    2.不过我知道一点:排除二进制数最后一个数不看,前面的二进制数按照串的规则最后得到的必然是偶数,如果最后一个是二进制数是0,那么肯定是偶数了,否则就是奇数;也就是说,如果给定的数字是偶数或者奇数,我们至少能够100%确定最后一个二进制数是0还是1
    3.至于后面的折半...移位...我是真的迷,等那天弄明白了再来更新吧
 
代码如下:
#include <iostream>

using namespace std;

int main()
{
int test;
__int64 len, goal;
char str[];
char ans[];
cin >> test;
while (test--)
{
cin >> len >> str >> goal;
ans[len] = '\0';
for (int i = len - ; i >= ; i--)
{
if (goal % == || goal % == -)
{
ans[i] = '';
if (str[i] == 'p') goal = (goal - ) / ;
else goal = (goal + ) / ;
}
else
{
ans[i] = '';
goal /= ;
}
}
if (!goal) cout << ans << endl;
else cout << "Impossible" << endl;
}
return ;
}

最新文章

  1. iOS常用第三方库
  2. Event Logging
  3. MSSQL 如何实现 MySQL 的 limit 查询方式 (转)
  4. javascript每日一练(十)——运动二:缓冲运动
  5. 【Demo 0008】标签控制器
  6. docker安装使用
  7. Optional乱用Empty之No value present
  8. 关于CSS的外边距合并问题
  9. DML数据操作语言之常用函数
  10. 免费下载获取Odoo中文实施 应用 指南 手册
  11. JPQL的关联查询
  12. python 协程库gevent学习--gevent源码学习(二)
  13. 微信网页JS分享,微信二次分享无缩略图问题
  14. iOS控制器生命周期
  15. mvc area出现“找到多个与名为“Home”的控制器匹配的类型”错误的解决方法
  16. FFmpeg命令详解
  17. Python -- 网络编程 -- Socket简单网络通信
  18. 【maven】Maven打包后为何文件大小改变了
  19. mysql中timestamp类型的应用
  20. oracle 常用的系统表查询

热门文章

  1. C#编写影院售票系统(A project with a higher amount of gold )(2:相关代码)
  2. &lt;script&gt;的用法
  3. nodejs+websocket实时聊天系统
  4. uwsgi wsgi nginx centos7.2部署flask
  5. 单KEY业务,数据库水平切分架构实践
  6. Go_认识golang
  7. Windows核心编程&amp;内存管理
  8. CURL模拟post请求上传文件
  9. 【转】iptables 命令介绍
  10. matlab判断文件或文件夹是否存在