A1023 Have Fun with Numbers

摘要
Title: A1023 Have Fun with Numbers
Tag: 高精度
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

A1023 Have Fun with Numbers

  • 题意

    Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
    Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.

  • 思路

    如果用Python3,则可以用Counter直接模拟即可
    如果用C++

    • int 是 [2109,2109][-2*10^9 , 2*10^9]
    • long long 是 [91018,91018][-9*10^{18} , 9*10^{18}]
    • 这个题有20位,会超,所以要开高精度
    • 高精度需要从低位开始处理,所以字符串放进vector时要倒着放,输出时也要倒序输出

    高精加

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    vector<int> add(vector<int> &A, vector<int> &B) // 加上引用可以提高效率,防止函数将vector全拷贝过来
    {
    if (A.size() < B.size()) return add(B, A); // 调整A的size最大

    vector<int> C;
    int t = 0;
    for (int i = 0; i < A.size(); i ++ )
    {
    t += A[i];
    if (i < B.size()) t += B[i];
    C.push_back(t % 10);
    t /= 10;
    }

    if (t) C.push_back(t);
    return C;
    }

    高精乘

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    vector<int> mul(vector<int> &A, int b)
    {
    vector<int> C;

    int t = 0;
    for (int i = 0; i < A.size(); i ++ )
    {
    t += A[i] * b;
    C.push_back(t % 10);
    t /= 10;
    }

    if(t) C.push_back(t);

    while (C.size() > 1 && C.back() == 0) C.pop_back(); // 去掉前导0
    return C;
    }
  • 代码

    python3

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    '''
    Author: NEFU AB-IN
    Date: 2023-01-06 10:02:20
    FilePath: \GPLT\A1023\A1023.py
    LastEditTime: 2023-01-06 10:39:00
    '''
    from collections import Counter

    n = input()
    n2 = str(int(n) * 2)

    d1 = Counter(n)
    d2 = Counter(n2)

    if d1 == d2:
    print("Yes")
    else:
    print("No")

    print(n2)

    C++

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    /*
    * @Author: NEFU AB-IN
    * @Date: 2023-01-06 18:00:41
    * @FilePath: \GPLT\A1023\A1023.cpp
    * @LastEditTime: 2023-01-06 18:09:24
    */
    #include <bits/stdc++.h>
    using namespace std;
    #define N n + 100
    #define int long long
    #define SZ(X) ((int)(X).size())
    #define IOS \
    ios::sync_with_stdio(false); \
    cin.tie(nullptr); \
    cout.tie(nullptr)
    #define DEBUG(X) cout << #X << ": " << X << endl
    typedef pair<int, int> PII;

    // #undef N
    // const int N = 1e5 + 10;

    // #undef int

    vector<int> add(vector<int> A, vector<int> B)
    {
    vector<int> C;
    int t = 0;

    for (int i = 0; i < SZ(A); i++)
    {
    int s = A[i] + B[i] + t;
    C.push_back(s % 10);
    t = s / 10;
    }

    if (t)
    C.push_back(t);
    return C;
    }

    signed main()
    {
    IOS;
    string s;
    cin >> s;

    vector<int> a;
    for (int i = SZ(s) - 1; i >= 0; --i)
    a.push_back(s[i] - '0');

    vector<int> b = add(a, a);
    vector<int> c = b;
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());

    cout << (a == b ? "Yes" : "No") << '\n';
    for (int i = SZ(c) - 1; i >= 0; --i)
    cout << c[i];
    return 0;
    }
使用搜索:谷歌必应百度