设为首页 - 加入收藏 PHP编程网 - PHP站长网 (http://www.52php.cn)- 电商,百科,编程,业界,移动互联,5G,云计算,站长网!
热搜: 娱乐 服务 专业 百度
当前位置: 首页 > 大数据 > 正文

简化版大数乘法

发布时间:2021-01-23 22:43 所属栏目:[大数据] 来源:网络整理
导读: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

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

#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int n,m;
void solve()
{
    int s[50];
    memset(s,0,sizeof s);
    s[0]=1;
    int up=1;
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<up;j++)//现将每一位的结果算出来,在进行进位,可避免将进位后的新高位又算一遍
        {
           s[j]*=n;
          // cout<<"s[j]"<<s[j];
        }
        for(int j=0;j<up;j++)
        {
            if(s[j]>9)
            {
               s[j+1]+=s[j]/10;
               s[j]%=10;
               if(j==up-1)
                up++;
            }
        }
        // cout<<"****up="<<up<<endl;
    }

    for(int i=up-1;i>=0;i--)
        printf("%d",s[i]);
    puts("");
}
int main()
{
    while(scanf("%d%d",&n,&m)&&(n||m))
    {
        if(n==10)
        {
            printf("1");
            for(int i=0;i<m;i++)
                printf("0");
            puts("");
            continue;
        }
        solve();
    }
    return 0;
}

【免责声明】本站内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

推荐文章
热点阅读