UVA11115 POJ3199 Uncle Jack大数

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA11115 POJ3199 Uncle Jack大数相关的知识,希望对你有一定的参考价值。

Dear Uncle Jack is willing to give away some of his collectable CDs to his nephews. Among the titles you can find very rare albums of Hard Rock, Classical Music, Reggae and much more; each title is considered to be unique. Last week he was listening to one of his favorite songs, Nobody’s fool, and realized that it would be prudent to be aware of the many ways he can give away the CDs among some of his nephews.
    So far he has not made up his mind about the total amount of CDs and the number of nephews. Indeed, a given nephew may receive no CDs at all.
    Please help dear Uncle Jack, given the total number of CDs and the number of nephews, to calculate the number of different ways to distribute the CDs among the nephews.
Input
The input consists of several test cases. Each test case is given in a single line of the input by, space separated, integers N (1 ≤ N ≤ 10) and D (0 ≤ D ≤ 25), corresponding to the number of nephews and the number of CDs respectively. The end of the test cases is indicated with N = D = 0.
Output
The output consists of several lines, one per test case, following the order given by the input. Each line has the number of all possible ways to distribute D CDs among N nephews.
Sample Input
1 20
3 10
0 0
Sample Output
1
59049

问题链接UVA11115 POJ3199 Uncle Jack
问题简述:输入N(1 ≤ N ≤ 10) 和 D (0 ≤ D ≤ 25),计算ND
问题分析:大数计算问题。可以用字符数组进行模拟计算(这里用2维数组,也可以用1维数组);也可以用128位整数进行计算,因为计算结果≤ 1025,而128位整数可以表示36位10进制数。连乘不是好方法,也许要用快速幂来计算。
程序说明:(略)
参考链接:(略)
题记:(略)

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

/* UVA11115 POJ3199 Uncle Jack */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

const int D = 25 + 1;
int a[D + 1][D + 1];

int main()
{
    int n, d;
    while (~scanf("%d%d", &n, &d) && (n || d)) {
        memset(a, 0, sizeof a);
        a[0][0] = 1;
        for (int i = 1; i <= d; i++) {
            for (int j = 0; j <= D; j++)
                a[i][j] = a[i - 1][j] * n;
            for (int j = 0; j <= D; j++)
                if (a[i][j] >= 10) {
                    a[i][j + 1] += a[i][j] / 10;
                    a[i][j] %= 10;
                }
        }

        int k = D;
        while(a[d][k] == 0) k--;
        while (k >= 0) putchar(a[d][k--] + '0');
        putchar('\\n');
    }

    return 0;
}

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

/* UVA11115 Uncle Jack */

#include <bits/stdc++.h>

using namespace std;

typedef __int128 INT128;

void printi128(INT128 n)
{
    if (n < 0) {putchar('-'); printi128(-n);}
    else if (n < 10) {putchar(n % 10 + '0');}
    else {printi128(n / 10); putchar(n % 10 + '0');}
}

int main()
{
    int n, d;
    while (~scanf("%d%d", &n, &d) && (n || d)) {
        INT128 ans = 1;

        while (d--) ans *= n;

        printi128(ans);
        putchar('\\n');
    }

    return 0;
}

以上是关于UVA11115 POJ3199 Uncle Jack大数的主要内容,如果未能解决你的问题,请参考以下文章

POJ 2063 investment (完全背包)

UVA 322 ships (POJ 1138)

2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

2019年7月做题记录

POJ 1015 / UVA 323 Jury Compromise(01背包,打印路径)

UVA10998 POJ2857 Flipping ColorsDFS