P1249 最大乘积

题目传送门

#include <bits/stdc++.h>

using namespace std;
typedef long long LL;
const int N = 1e5 + 10;

int n, num;
int ans;
vector<int> c;

int a[N], al;
int b[N], bl;
void mul(int a[], int &al, int b[], int bl) {
    int c[N] = {0}, cl = al + bl;
    for (int i = 1; i <= al; i++)
        for (int j = 1; j <= bl; j++)
            c[i + j - 1] += a[i] * b[j];
    int t = 0;
    for (int i = 1; i <= al + bl; i++) {
        t += c[i];
        c[i] = t % 10;
        t /= 10;
    }
    memcpy(a, c, sizeof c);
    al = cl;
    //前导0
    while (al > 1 && a[al] == 0) al--;
}

int main() {
    scanf("%d", &n);
    //无脑的增大序列
    for (num = 2; ans + num <= n; num++) {
        ans += num;
        c.push_back(num);
    }
    //余数
    int r = n - ans;
    //后面的人,每人一个,如果一轮没有分完,就继续再来一轮
    //举栗子:13
    // 2 3 4 余数 4
    // 最终分配结果 3 4 6
    while (r) {
        for (int i = c.size() - 1; i >= 0 && r; i--) {
            c[i] += 1;
            r--;
        }
    }
    //输出C
    for (int i = 0; i < c.size(); i++) printf("%d ", c[i]);
    printf("\n");

    //高精度乘法
    a[++al] = 1;
    for (int i = 0; i < c.size(); i++) {
        int x = c[i];
        for (bl = 0; x; x /= 10) b[++bl] = x % 10;
        mul(a, al, b, bl);
    }

    //输出
    for (int i = al; i; i--) printf("%d", a[i]);
    return 0;
}
posted @ 2021-11-22 15:53  糖豆爸爸  阅读(72)  评论(0编辑  收藏  举报
Live2D