Cattle-off practice match 48 A · small problem w of a + b (the greedy, structure, binary)

Cattle-off practice match 48 A · small problem w of a + b

Links: https://ac.nowcoder.com/acm/contest/923/A Source: Cattle-off network

Time limit: C / C ++ 1 second, 2 seconds languages other
space restrictions: C / C ++ 32768K, other languages 65536k
Special Judge, the IO 64bit the Format: LLD%

Title Description

You must have done major oj different versions of the above A + B problem, if we turn to you c, please give me a set of output and b, such that a + b and c is equal to it?

This also is a simple question.

We assume that a language shaping bit 32 is defined as an int, which is stored in language-negative integer in two's-complement form, the sign bit of 32 bits, the first 31 bits of the value. E.g. -1 is stored as "1,111,111,111,111,111 1,111,111,111,111,111", - 8 were stored as "1,111,111,111,111,111 1,111,111,111,111,000", in particular, plastic 32 can represent the largest negative number -2147483648 It is stored as "1,000,000,000,000,000 0,000,000,000,000,000."

The computer doing the addition operation is actually performed complement addition operation in the calculation of the process if the number of overflows to 33 does not exist, then the overflow bit is not.

You are given a 32 negative plastic c. I.e. c∈ [-2147483648, -1] c \ in [-2147483648, -1] c∈ [-2147483648, -1].

Please give me a positive two plastic 32 a, b, that is a, b∈ [1,2147483647] a, b \ in [1,2147483647] a, b∈ [1,2147483647]. Such that a + b = c.

If there is no such a and b, please outputs a string "No solution". Otherwise, the output of any two n-shaping a, b satisfy a + b = c. Separated by a space between two integers.

Enter a description:

仅一行一个32位负整形c,(−2147483648⩽c⩽−1)(-2147483648\leqslant c\leqslant -1)(−2147483648⩽c⩽−1)

Output Description:

如果存在两个32位正整形a,b使得a+b=c成立,则输出这两个正整形。反之请输出一个字符串"No solution"。(不含引号)

Example 1

Entry

copy

-182

Export

copy

2147483647 2147483467

Explanation

a=2147483647="0111 1111 1111 1111 1111 1111 1111 1111"b=2147483467="0111 1111 1111 1111 1111 1111 0100 1011"c=-182="1111 1111 1111 1111 1111 1111 0100 1010"a+b=c

Remarks:


如存在多解,你可以输出任意解。你可以使用c语言的int类型来验证构造答案的正确性。

Ideas:

We know exactly x binary information and \ ((- 1 * x) -1 \) of each opposite.

We first obtain information of each bit of binary x,

Then a configuration and b,

Just use a flag to see if certain markers need to carry two low adding 1 before they can get the current one can be.

-1 because all binary 1, it is not possible by the two is greater than the sum equal to the number of configuration 1, the output has no solution.

See details Code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {a %= MOD; if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}

inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
string s1;
void PRINF2(ll num, int k)
{
    for (int i = k; i >= 0; i--)
    {
        if ( (bool)(num & (1ll << i)))
        {
            s1.push_back('0');
        } else
        {
            s1.push_back('1');
        }
    }
}
void PRINF1(ll num, int k)
{
    for (int i = k; i >= 0; i--)
    {
        cout << (bool)(num & (1ll << i));
    }
    cout << endl;
}
int main()
{
    //freopen("D:\\code\\text\\input.txt","r",stdin);
    //freopen("D:\\code\\text\\output.txt","w",stdout);
    ll x;
    cin >> x;
    if (x == -1)
    {
        return 0 * puts("No solution");
    }
    x *= -1;
    x--;
    PRINF2(x, 31);
    ll a = 0ll;
    ll b = 0ll;
    bool flag = 1;
    // cout << s1 << endl;
    for (int i = 1; i <= 32; ++i)
    {
        if (s1[i - 1] == '1')
        {
            if (flag)
            {
                a += 1ll << (31 - i);
                b += 1ll << (31 - i);
            } else
            {
                a += 1ll << (31 - (i - 1));
            }
        } else
        {
            flag = 0;
        }
    }
    cout << a << " " << b << endl;
    // PRINF1(a, 31);
    // PRINF1(b, 31);
    // cout << (int)((int)(a) + (int)(b)) << endl;
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}




Guess you like

Origin www.cnblogs.com/qieqiemin/p/11789950.html