Problem 1014 - Uncle Jack

/*

Problem 1014 - Uncle Jack
Time Limit: 1000MS   Memory Limit: 65536KB   Difficulty:
Total Submit: 161  Accepted: 55  Special Judge: No
Description
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

*/

简单大数。题意很简单,由乘法原理知本题就是要计算N^D,最大为10^25,而long long 最大可存储9223372036854775807,不到10^19,所以考虑大数,但是这个大数比较简单,所以写起来不用那么麻烦

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    int ans[28];
    int a,b,i,j,t,s;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        for(i=0;i<28;i++) ans[i]=0;    //初始化ans数组
        if(a==10)    //10的n次方,直接写就行,节省点时间
        {
            printf("1");
            for(i=1;i<=b;i++)    //1后面b个0……
                printf("0");
            printf("\n");
            continue;
        }
        s=1;
        ans[27]=1;
        if(b==0)    //特殊值
        {
            if(a==0) break;
            else {printf("1\n");continue;}
        }
        for(i=1;i<=b;i++)
        {
            t=0;
            for(j=27;j>=0;j--)
            {
                s=ans[j]*a+t;  //乘以a再加上上位的进位
                ans[j]=s%10;  
                t=s/10;
            }
        }
        for(i=0;ans[i]==0;i++);  //跳过前导0
        for(;i<28;i++) printf("%d",ans[i]);
        printf("\n");
    }
    return 0;
}

posted @ 2013-08-07 21:23  hjf007  阅读(162)  评论(0编辑  收藏  举报