POJ 1023 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 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.至于后面的折半...移位...我是真的迷,等那天弄明白了再来更新吧

4.参考: http://blog.csdn.net/zsc09_leaf/article/details/6292041

http://blog.csdn.net/hqd_acm/article/details/6212599

代码如下:

#include <iostream>

using namespace std;

int main()
{
    int test;
    __int64 len, goal;
    char str[1000];
    char ans[1000];
    cin >> test;
    while (test--)
    {
        cin >> len >> str >> goal;
        ans[len] = ‘\0‘;
        for (int i = len - 1; i >= 0; i--)
        {
            if (goal % 2 == 1 || goal % 2 == -1)
            {
                ans[i] = ‘1‘;
                if (str[i] == ‘p‘) goal = (goal - 1) / 2;
                else goal = (goal + 1) / 2;
            }
            else
            {
                ans[i] = ‘0‘;
                goal /= 2;
            }
        }
        if (!goal) cout << ans << endl;
        else cout << "Impossible" << endl;
    }
    return 0;
}
时间: 2024-08-29 13:56:07

POJ 1023 The Fun Number System的相关文章

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

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 the number system. First few numbers in the number system are: 3, 4, 33, 34, 43, 44, 333, 334, 343, 344, 433, 434, 443, 444, 3333, 3334, 3343, 3344, 343

uva 11651 - Krypton Number System(矩阵快速幂)

题目链接:uva 11651 - Krypton Number System 题目大意:给定进制base,和分数score,求在base进制下,有多少个数的值为score,要求不能有连续相同的数字以及前导0.计算一个数的值即为相邻两位数的平方差和. 解题思路:因为score很大,所以直接dp肯定超时,但是即使对于base=6的情况,每次新添一个数score最大增加25(0-5),所以用dp[i][j]预处理出base平方以内的总数,然后用矩阵快速幂计算. #include <cstdio> #

The Stern-Brocot Number System(树的左边还是右边)

The Stern-Brocot Number System Input: standard input Output: standard output The Stern-Brocot tree is a beautiful way for constructing the set of all nonnegative fractions m / n where m and n are relatively prime. The idea is to start with two fracti

UVA 11651 - Krypton Number System(DP+矩阵快速幂)

UVA 11651 - Krypton Number System 题目链接 题意:给一个进制base,一个分数score求该进制下,有多少数满足一下条件: 1.没有连续数字 2.没有前导零 3.分数为score,分数的计算方式为相邻数字的平方差的和 思路:先从dp入手,dp[i][j]表示组成i,最后一个数字为j的种数,然后进行状态转移,推出前面一步能构成的状态,也就是到dp[(b - 1) * (b - 1)][x]. 然后可以发现后面的状态,都可以由前面这些状态统一转移出来,这样就可以利用

Moduli number system

A number system with moduli is de?ned by a vector of k moduli, [m1,m2, ···,mk]. The moduli must be pairwise co-prime, which means that, for any pair of moduli, the only common factor is 1. In such a system each number n is represented by a string "-x

POJ #2448 A New Operating System

Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 1165   Accepted: 110 Case Time Limit: 5000MS Description May is a lovely girl. Due to her filial piety, she wants to give a present on her mother's birthday. Because both her parents are t

POJ 2104:K-th Number(整体二分)

http://poj.org/problem?id=2104 题意:给出n个数和m个询问求区间第K小. 思路:以前用主席树做过,这次学整体二分来做.整体二分在yr大佬的指点下,终于大概懂了点了.对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了. 在这题二分可行的答案,根据这个答案,把询问操作丢在左右两个队列里面分别递归继续按这样处理.注释里写的很详细. 1 #include <iostream> 2 #include <cstdlib> 3 #

【POJ 2104】K-th Number

Description You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array