链接

题意:整数每位上的进制并不固定,例如321个位为2进制,十位为十进制,百位为八进制。换算为十进制数为((3 * 10) + 2) * 2 + 1 = 65。给定整数A和B,在同一进制规则下,A-B的最大值。

题解:本题重点就是怎么理解X进制和大数组整数的存储。最小位在数组的第一位,最高位在最后一位,方便计算。

代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#define PI 3.14159
using namespace std;

typedef pair<int, int> PII;
typedef long long LL;
const int MAX_INT = 0x3f3f3f3f;
const int N = 1e5 + 15;
const int mod = 1e9 + 7;

LL a[N];
LL b[N];
LL p[N];

void solve()
{
    int t;
    cin >> t;
    int n1, n2;
    cin >> n1;
    for (int i = n1; i >= 1; i--)
        cin >> a[i];//把最高位放在末尾 便于计算 例如 00000001
                                                 // 000001
    cin >> n2;
    for (int i = n2; i >= 1; i--)
        cin >> b[i];

    for (int i = 1; i <= n1; i++)//从最小位开始计算进制 
    {
        p[i] = max(max(a[i], b[i]) + 1, (LL)2);//进制的算法
    }
    p[0] = 1;//第一个一定是1

    LL sum = 0;
    for (int i = n1; i >= 1; i--)//从最高位开始遍历
    {
        sum = (sum + a[i] - b[i]) * p[i - 1] % mod;//带公式
    }
    cout << sum << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int T = 1;
    while (T--)
    {
        solve();
    }
}