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. eclipse 常见问题及解决

    1. Target runtime Apache Tomcat v6.0 is not defined.错误解决方法 原文:http://blog.csdn.net/xw13106209/articl ...

  2. 『零行代码』解决键盘遮挡问题(iOS)

    关注仓库,及时获得更新:iOS-Source-Code-Analyze https://github.com/draveness/iOS-Source-Code-Analyze Follow: Dra ...

  3. iOS UI控件继承关系图

    闲来无事,把UI控件的继承关系图整理下来,供自己和大家使用.

  4. 2进制,16进制,BCD,ascii,序列化对象相互转换

    public final static char[] BToA = "0123456789abcdef".toCharArray() ; 1.16进制字符串转为字节数组 /** * ...

  5. c# 操作.config中AppSettings配置节

    ConfigurationSettings.AppSettings[key].ToString(); 这种方式很眼熟吧? 不过这种方式基本过时了,虽然还能用. 微软建议采用ConfigurationM ...

  6. Socket 学习

    Socket一般应用模式(服务器端和客户端) 服务器端Socket(至少有两个) ->一个负责接收客户端连接请求(但不负责和客户端通信) ->没成功接收到一个客户端的连接便在服务端生成一个 ...

  7. Get file name without extension.

    Ref:How to get file name without the extension? Normally,there are two ways to implements this:use t ...

  8. VsSharp:一个VS扩展开发框架(上)

    上篇:设计 一.引子 自2008年起开发SSMS插件SqlSharp(er)的过程中,有一天发现多数代码都大同小异,就像这样. Commands2 commands = (Commands2)_app ...

  9. vim备注

    ① 用户path生效 在~/.bashrc中修改path,在~/.profile中source bashrc ② secureCRT着色方案 底色RGB:43 43 43 前景色RGB:221 221 ...

  10. 解决UITableView中Cell重用机制导致内容出错的方法总结

    UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件.上面主要是一个个的 UITableViewCell,可以让UITableViewCell响应一些点 ...