Uncle Jack POJ - 3199
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
题意描述:杰克叔叔想把他一些可收藏的CD送给他的侄子,要求计算在侄子中分发CD的不同方式的数量。
解题思路:这一题是一道求大数幂的问题,不过我的代码实现当指数为0和n为10的时候得到的答案是错误的,所以我把这两种情况分开来描述。
AC代码:
#include<stdio.h>
#include<string.h>
#define max 105
char str[max];
int b[max+10],z[max*max+10];
int s[max*max+10];
void mi(int b[],int z[],int k)
{
int i,j;
memset(s,0,sizeof(s));
for(i=0;i<k;i++)
for(j=0;j<max*max;j++)
s[i+j]+=b[i]*z[j];
for(i=0;i<max*max;i++)
{
if(s[i]>=10)
s[i+1]+=s[i]/10;
z[i]=s[i]%10;
}
}
int main()
{
int i,j,k,n,m;
while(~scanf("%s%d",str,&n))
{
memset(z,0,sizeof(z));
k=strlen(str);
if(k==1&&str[0]=='0'&&n==0)
break;
j=0;
m=0;
if(k==2&&str[0]=='1'&&str[1]=='0')
{
printf("1");
for(i=1;i<=n;i++)
printf("0");
printf("\n");
continue;
}
if(n==0)
{
printf("1\n");
continue;
}
for(i=0;i<k;i++)
{
b[j++]=str[i]-'0';
z[m++]=str[i]-'0';
}
n--;
while(n)
{
mi(b,z,k);
n--;
}
for(i=max*max-1;i>=0;i--)
{
if(z[i]==0)
continue;
else
break;
}
memset(str,0,sizeof(str));
for(;i>=0;i--)
printf("%d",z[i]);
printf("\n");
}
return 0;
}