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 ;
}

POJ 1023 The Fun Number System的更多相关文章

  1. POJ1023 The Fun Number System

    题目来源:http://poj.org/problem?id=1023 题目大意: 有一种有趣的数字系统.类似于我们熟知的二进制,区别是每一位的权重有正有负.(低位至高位编号0->k,第i位的权 ...

  2. Find n‘th number in a number system with only 3 and 4

    这是在看geeksforgeeks时看到的一道题,挺不错的,题目是 Given a number system with only 3 and 4. Find the nth number in th ...

  3. Moduli number system

    A number system with moduli is defined by a vector of k moduli, [m1,m2, ···,mk]. The moduli must be p ...

  4. F - The Fun Number System(第二季水)

    Description In a k bit 2's complement number, where the bits are indexed from 0 to k-1, the weight o ...

  5. The Stern-Brocot Number System(排序二进制)

    The Stern-Brocot Number System Input: standard input Output: standard output The Stern-Brocot tree i ...

  6. 为什么实数系里不存在最小正数?(Why the smallest positive real number doesn't exist in the real number system ?)

    We define the smallest positive real number as the number which is explicitly greater than zero and ...

  7. POJ #2448 A New Operating System

    Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 1165   Accepted: 110 Case Time Limit: ...

  8. 【POJ】2104 K-th Number(区间k大+主席树)

    http://poj.org/problem?id=2104 裸题不说.主席树水过. #include <cstdio> #include <iostream> #includ ...

  9. poj 3696 The Luckiest Number

    The Luckiest Number 题目大意:给你一个int范围内的正整数n,求这样的最小的x,使得:连续的x个8可以被n整除. 注释:如果无解输出0.poj多组数据,第i组数据前面加上Case ...

随机推荐

  1. SSL和SSH有什么区别

    SSL 是一种安全协议,它为网络(例如因特网)的通信提供私密性.SSL 使应用程序在通信时不用担心被窃听和篡改. SSL 实际上 是共同工作的两个协议:"SSL 记录协议"(SSL ...

  2. putty怎么用?如何使用Putty远程管理Linux主机

    Putty是一个免费的Windows 32平台下用于telnet.rlogin和ssh客户端的远程客户端工具,可以通过PUTTY快速的实现SSH连接linux等主机,下面小编就给大家演示一下如何使用P ...

  3. JavaScript之BST

    自己尝试用js实现了数据结构的二叉查找树. // node function Node(data) { this.data = data; this.lc = null; this.rc = null ...

  4. [sklearn]官方例程-Imputing missing values before building an estimator 随机填充缺失值

    官方链接:http://scikit-learn.org/dev/auto_examples/plot_missing_values.html#sphx-glr-auto-examples-plot- ...

  5. PHP正则式PCRE

    PHP正则式PCRE的总结差不多就下边这些了.参考 PCRE与perl的差异 .   锚(^.$.\A.\Z/\z):^.$在多行模式下是非紧固的,在单行模式下是紧固的:而\A.\Z / \z在任何模 ...

  6. android 基础03 -- Intent

    Android 中的 Intent 是将要执行的操作的一种抽象的描述,是一个用于Android 各个组件之间传递消息的对象. Intent 的基本用法 Intent 基本的使用方法主要有三种: 启动一 ...

  7. vue学习笔记(三)——目录结构介绍

    1.初始目录结构如下: 2.目录结构介绍 目录/文件 说明 build 最终发布的代码存放位置. config 配置目录,包括端口号等.我们初学可以使用默认的. node_modules npm 加载 ...

  8. [one day one question] Vue数组变更不能触发刷新

    问题描述:Vue数组变更不能触发刷新,特别是数组的每个元素都是对象的时候,对象中某个属性的值发生变化,根本无法触发Vue的dom刷新,这怎么破? 解决方案:this.$set(array, index ...

  9. MySQL 水平拆分(读书笔记整理)

    转:http://blog.csdn.net/mchdba/article/details/46278687 1,水平拆分的介绍 一般来说,简单的水平切分主要是将某个访问极其平凡的表再按照某个字段的某 ...

  10. arduino扩展IO与M74HC595B芯片的使用,挪车电话提示牌的设计

    2018-01-0915:39:24 视频连接 首先arduino中shiftOUT()函数的定义与说明! shiftOut()描述将一个数据的一个字节一位一位的移出.从最高有效位(最左边)或最低有效 ...