Cut the Cake(大数相乘)

 
MMM got a big big big cake, and invited all her M friends to eat the cake together. Surprisingly one of her friends HZ took some (N) strawberries which MMM likes very much to decorate the cake (of course they also eat strawberries, not just for decoration). HZ is in charge of the decoration, and he thinks that it's not a big deal that he put the strawberries on the cake randomly one by one. After that, MMM would cut the cake into M pieces of sector with equal size and shape (the last one came to the party will have no cake to eat), and choose one piece first. MMM wants to know the probability that she can get all N strawberries, can you help her? As the cake is so big, all strawberries on it could be treat as points.
 

 

Input
First line is the integer T, which means there are T cases.
For each case, two integers M, N indicate the number of her friends and the number of strawberry.
(2 < M, N <= 20, T <= 400)
 

 

Output
As the probability could be very small, you should output the probability in the form of a fraction in lowest terms. For each case, output the probability in a single line. Please see the sample for more details.
 

 

Sample Input
2
3 3
3 4
 

 

Sample Output
1/3
4/27
 
主要是大数的乘法并约分;
 
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 const int MAX = 200;
 5 
 6 char ans[2*MAX],mul[MAX];
 7 
 8 int gcd(int a, int b)
 9 {
10     if(b == 0)
11         return a;
12     return gcd(b,a%b);
13 }
14 
15 void multiply(char*a,char*b,char*c)
16 {//正着乘,从最高位开始;
17     int *s;
18     int i,j;
19     int ca = strlen(a);
20     int cb = strlen(b);
21     s = (int*)malloc(sizeof(int)*(ca+cb));
22     for(i = 0; i < ca+cb; i++)
23         s[i] = 0;
24 
25     for(i = 0; i < ca; i++)
26     {
27         for(j = 0; j < cb; j++)
28         {
29             s[i+j+1] += (a[i]-'0')*(b[j]-'0');//i+j+1是为了防止最高位进位出现错误
30         }
31     }
32 
33     for(i = ca+cb-1; i >= 0; i--)
34     {
35         if(s[i] >= 10)
36         {
37             s[i-1] += s[i]/10;
38             s[i] %= 10;
39         }
40     }
41 
42     i=0;
43     while (s[i]==0)
44         i++;//去除前导0
45     for (j=0; i<ca+cb; i++,j++)
46         c[j]=s[i]+'0';
47     c[j]= 0;//将结果存储到字符数组
48     free(s);
49 }
50 int main()
51 {
52     int test,i;
53     scanf("%d",&test);
54 
55     while(test--)
56     {
57         int M,N;
58         scanf("%d %d",&M,&N);
59 
60         memset(ans,0,sizeof(ans));
61         memset(mul,0,sizeof(mul));
62         ans[0] = '1';
63         ans[1] = '\0';
64 
65         int flag = 1;
66         int n = N;
67         for(i = 1; i <= N-1; i++)
68         {
69             int m = M;
70             if(flag == 1)
71             {
72                 int g = gcd(n,m);
73                 if(g == 1)
74                 {
75                     flag = 0;
76                 }
77                 else
78                 {
79                     n/=g;
80                     m/=g;
81                 }
82             }
83             if(m >= 10)
84             {
85                 mul[0] = m/10+'0';
86                 mul[1] = m%10+'0';
87                 mul[2] = '\0';
88             }
89             else
90             {
91                 mul[0] = m+'0';
92                 mul[1] = '\0';
93             }
94             multiply(ans,mul,ans);
95         }
96         printf("%d/%s\n",n,ans);
97     }
98     return 0;
99 }
View Code

 

 

转载于:https://www.cnblogs.com/LK1994/p/3344689.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值