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
 
主要是大数的乘法并约分;
 
 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
const int MAX = ; char ans[*MAX],mul[MAX]; int gcd(int a, int b)
{
if(b == )
return a;
return gcd(b,a%b);
} void multiply(char*a,char*b,char*c)
{//正着乘,从最高位开始;
int *s;
int i,j;
int ca = strlen(a);
int cb = strlen(b);
s = (int*)malloc(sizeof(int)*(ca+cb));
for(i = ; i < ca+cb; i++)
s[i] = ; for(i = ; i < ca; i++)
{
for(j = ; j < cb; j++)
{
s[i+j+] += (a[i]-'')*(b[j]-'');//i+j+1是为了防止最高位进位出现错误
}
} for(i = ca+cb-; i >= ; i--)
{
if(s[i] >= )
{
s[i-] += s[i]/;
s[i] %= ;
}
} i=;
while (s[i]==)
i++;//去除前导0
for (j=; i<ca+cb; i++,j++)
c[j]=s[i]+'';
c[j]= ;//将结果存储到字符数组
free(s);
}
int main()
{
int test,i;
scanf("%d",&test); while(test--)
{
int M,N;
scanf("%d %d",&M,&N); memset(ans,,sizeof(ans));
memset(mul,,sizeof(mul));
ans[] = '';
ans[] = '\0'; int flag = ;
int n = N;
for(i = ; i <= N-; i++)
{
int m = M;
if(flag == )
{
int g = gcd(n,m);
if(g == )
{
flag = ;
}
else
{
n/=g;
m/=g;
}
}
if(m >= )
{
mul[] = m/+'';
mul[] = m%+'';
mul[] = '\0';
}
else
{
mul[] = m+'';
mul[] = '\0';
}
multiply(ans,mul,ans);
}
printf("%d/%s\n",n,ans);
}
return ;
}

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 存储过程项目小结

    1. false :0  true 1 切记 官方文档:http://dev.mysql.com/doc/refman/5.0/en/numeric-type-overview.html BOOL,  ...

  2. ios中从相册:相机中获取图片信息

    ios中从相册/相机中获取图片信息 从相册中获取图片的信息 UIImagePickerController *imgPickView = [[UIImagePickerController alloc ...

  3. Android 布局

    转自:http://www.cnblogs.com/chiao/archive/2011/08/24/2152435.html Android布局是应用界面开发的重要一环,在Android中,共有五种 ...

  4. iOS原生CIFilter创建二维码

    iOS原生CIFilter创建二维码 2016-05-31 未来C iOS原生CIFilter创建二维码 关于二维码生成,网上也是有很多,很早以前的第三方库大多数都是通过C++写,也是有的如zxing ...

  5. VS2015 Cordova Ionic移动开发(二)

    一.创建空白Cordova应用 打开VS,选择[新建项目],选择其它语言JavaScript或者TypeScript,语言的话就按个人喜好,喜欢JS就用JS,喜欢TS就用TS,推荐使用TS书写,代码结 ...

  6. oracle服务器端-登陆

    由于的的操作系统是windows server版本,所以想装服务器端的server版本,一般的oracle都有'scott'用户,但是貌似服务器端的没有该用户,我用以下方式登陆: sqlplus / ...

  7. 输出内容(document.write)

    document.write() 直接在页面中输出内容 第一种 直接输出 document.write("I Love Javascript !") //输出内容为:I Love ...

  8. centos 6.x 安装redis

    1.yum 安装 yum install redis 如果提示找不到包的话  可以yum install epel-release   先安装epel第三方库 2.源码安装 https://redis ...

  9. sublime text 几种常用插件

    1.docblockr  //文档注释 使用 /**  +tab 在函数前就可以   2.SublimeLinter 代码校验插件,支持多种语言,这个是主插件,如果想检测特定的文件需要单独下载   3 ...

  10. HDOJ 2036

    错误代码: #include<stdio.h>#include<math.h>int main(){ int x[102],y[102]; int i,n; float s,a ...