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 = ;
  5.  
  6. char ans[*MAX],mul[MAX];
  7.  
  8. int gcd(int a, int b)
  9. {
  10. if(b == )
  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 = ; i < ca+cb; i++)
  23. s[i] = ;
  24.  
  25. for(i = ; i < ca; i++)
  26. {
  27. for(j = ; j < cb; j++)
  28. {
  29. s[i+j+] += (a[i]-'')*(b[j]-'');//i+j+1是为了防止最高位进位出现错误
  30. }
  31. }
  32.  
  33. for(i = ca+cb-; i >= ; i--)
  34. {
  35. if(s[i] >= )
  36. {
  37. s[i-] += s[i]/;
  38. s[i] %= ;
  39. }
  40. }
  41.  
  42. i=;
  43. while (s[i]==)
  44. i++;//去除前导0
  45. for (j=; i<ca+cb; i++,j++)
  46. c[j]=s[i]+'';
  47. c[j]= ;//将结果存储到字符数组
  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,,sizeof(ans));
  61. memset(mul,,sizeof(mul));
  62. ans[] = '';
  63. ans[] = '\0';
  64.  
  65. int flag = ;
  66. int n = N;
  67. for(i = ; i <= N-; i++)
  68. {
  69. int m = M;
  70. if(flag == )
  71. {
  72. int g = gcd(n,m);
  73. if(g == )
  74. {
  75. flag = ;
  76. }
  77. else
  78. {
  79. n/=g;
  80. m/=g;
  81. }
  82. }
  83. if(m >= )
  84. {
  85. mul[] = m/+'';
  86. mul[] = m%+'';
  87. mul[] = '\0';
  88. }
  89. else
  90. {
  91. mul[] = m+'';
  92. mul[] = '\0';
  93. }
  94. multiply(ans,mul,ans);
  95. }
  96. printf("%d/%s\n",n,ans);
  97. }
  98. return ;
  99. }

Cut the Cake(大数相乘)的更多相关文章

  1. HDU 4762 Cut the Cake (2013长春网络赛1004题,公式题)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  2. POJ 2389 Bull Math(水~Java -大数相乘)

    题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...

  3. HDU 4762 Cut the Cake(公式)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. 大数相乘算法C++版

    #include <iostream> #include <cstring> using namespace std; #define null 0 #define MAXN ...

  5. java版大数相乘

    在搞ACM的时候遇到大数相乘的问题,在网上找了一下,看到了一个c++版本的 http://blog.csdn.net/jianzhibeihang/article/details/4948267 用j ...

  6. Linux C/C++ 编程练手 --- 大数相加和大数相乘

    最近写了一个大数相乘和相加的程序,结果看起来是对的.不过期间的效率可能不是最好的,有些地方也是临时为了解决问题而直接写出来的. 可以大概说一下相乘和相加的解决思路(当然,大数操作基本就是两个字符串的操 ...

  7. Karatsuba乘法--实现大数相乘

    Karatsuba乘法 Karatsuba乘法是一种快速乘法.此算法在1960年由Anatolii Alexeevitch Karatsuba 提出,并于1962年得以发表.此算法主要用于两个大数相乘 ...

  8. HDU 4762 Cut the Cake(高精度)

    Cut the Cake Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. leetcode 43 Multiply Strings 大数相乘

    感觉是大数相乘算法里面最能够描述.模拟演算过程的思路 class Solution { public String multiply(String num1, String num2) { if(nu ...

随机推荐

  1. mysql查看端口

    在你的my.ini(Windows)或my.cfg(Linux) 中就有啊. 或者如果已经连入MySQL可以直接 SQL code ? 1 2 3 4 5 6 7 8 9 mysql> show ...

  2. 基于GPUImage的实时美颜滤镜

    1.背景 前段时间由于项目需求,做了一个基于GPUImage的实时美颜滤镜.现在各种各样的直播.视频App层出不穷,美颜滤镜的需求也越来越多.为了回馈开源,现在我把它放到了GitHub https:/ ...

  3. [转]C#中yield用法

    yield 关键字向编译器指示它所在的方法是迭代器块.编译器生成一个类来实现迭代器块中表示的行为.在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值.这是一个返回值, ...

  4. List转xml

    1. List<Model> list = new List<Model>(); Model zj = new Model(); zj.id = ; zj.name = &qu ...

  5. 重要性!important

    我们在做网页代码的时,有些特殊的情况需要为某些样式设置具有最高权值,怎么办?这时候我们可以使用!important来解决. 如下代码: p{color:red!important;} p{color: ...

  6. [转]Delphi I/O Errors

    The following are the Windows API (and former DOS) IO errors, which are also the IO errors often ret ...

  7. jq 图片裁剪

    1.html <div class="jcropbox" style="display: none"> <img src="&quo ...

  8. 利用CMake自己创建OpenCV静态链接库

    1.准备工作: 1)完成Visual Studio2012安装: 2)下载并解压CMake3.5.0: 3)下载并解压OpenCV2.4.12: 4)下载并解压TBB44_20160128oss. 2 ...

  9. 完整的 dataType=text/plain jquery ajax 登录验证

    Html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <m ...

  10. SGU 106.Index of super-prime

    时间限制:0.25s 空间限制:4M 题目大意:                 在从下标1开始素数表里,下标为素数的素数,称为超级素数(Super-prime),给出一个n(n<=10000) ...