UVA11532 Simple Adjacency Maximization位运算

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11532 Simple Adjacency Maximization位运算相关的知识,希望对你有一定的参考价值。

Find the smallest integer N that has both of the following properties:

  1. The binary representation of N has exactly P 1’s & exactly Q 0’s. (Leading Zeroes are allowed).
  2. The number of 1’s adjacent to one or more 0 in the binary representation is maximized.

Input
The first line of the input file contains a single integer C, the number of test cases in the input file. Each of the next C lines contains two non-negative integers P & Q (1 ≤ P + Q ≤ 50).
Output
For each test case a print the value of N, as explained in the statement, in a line by itself.
Sample Input
3
4 3
1 1
3 2
Sample Output
45
1
13

问题链接UVA11532 Simple Adjacency Maximization
问题简述:(略)
问题分析:简单题,用位运算来解决,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11532 Simple Adjacency Maximization */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int t, p, q;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &p, &q);

        long long ans = 0, digit = 0;
        while (p >= 2 && q >= 1) {
            ans |= (1LL << digit) | (1LL << (digit + 2));
            digit += 3;
            p -= 2, q -= 1;
        }
        if (p == 1 && q >= 1)
            ans |= 1LL << digit, digit += 2, p--, q--;
        while (p) ans = (ans << 1) | 1, digit++, p--;

        printf("%lld\\n", ans);
    }

    return 0;
}

以上是关于UVA11532 Simple Adjacency Maximization位运算的主要内容,如果未能解决你的问题,请参考以下文章

UVA10994 Simple Addition前缀和

UVA11565 Simple Equations数学+暴力

UVA12253 简单加密法 Simple Encryption

Adjacency matrix 传球问题

python graph_adjacency_matrix_print

boost::adjacency_list 如何在 remove_edge 之后保留有效的边描述符