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. 可以直接拿来用的15个jQuery代码片段

    jQuery里提供了许多创建交互式网站的方法,在开发Web项目时,开发人员应该好好利用jQuery代码,它们不仅能给网站带来各种动画.特效,还会提高网站的用户体验. 本文收集了15段非常实用的jQue ...

  2. Usaco 2010 Dec Gold Exercise(奶牛健美操)

    /*codevs 3279 二分+dfs贪心检验 堆版本 re一个 爆栈了*/ #include<cstdio> #include<queue> #include<cst ...

  3. insert当 sql语句里面有变量 为字符类型的时候 要3个单引号

    set @InsertStr='INSERT INTO [dbo].[T_SchoolPercentMonth]([SchoolID],[MonthOfYear],[PercentNum]) VALU ...

  4. ASP.NET Boilerplate Castle容器无缝添加日志功能

    以添加log4net日志框架为例进行讲解 1.通常log4net的配置参数放在单独的配置文件中,但也可以写在web.config中,这里在我们的web项目中添加log4net.config应用配置文件 ...

  5. HTML5 文件域+FileReader 分段读取文件(五)

    一.默认FileReader会分段读取File对象,这是分段大小不一定,并且一般会很大 HTML: <div class="container"> <!--文本文 ...

  6. 简单讲解iOS应用开发中的MD5加密的相关使用<转>

    这篇文章主要介绍了iOS应用开发中的MD5加密的相关使用,示例代码基于传统的Objective-C,需要的朋友可以参考下 一.简单说明 1.说明 在开发应用的时候,数据的安全性至关重要,而仅仅用POS ...

  7. 【转】iOS开发6:UIActionSheet与UIAlertView

    原文: http://my.oschina.net/plumsoft/blog/42763 iOS程序中的Action Sheet就像Windows中的 “确定-取消”对话框一样,用于强制用户进行选择 ...

  8. C#快速导入海量XML数据至SQL Server数据库

    #region 将Xml中的数据读到Dataset中,然后用SqlBulkCopy类把数据copy到目的表中using (XmlTextReader xmlReader = new XmlTextRe ...

  9. 新一代的代码编辑神器Sublime Text 3(使用指南)

    首先附上官网下载链接:http://www.sublimetext.com/3 接下来是安装sublime最强大的插件功能:Package Control 一.简单的安装方法 使用Ctrl+`快捷键或 ...

  10. js获取url中的参数对象、js生成带参数的url

    // 获取url中的参数,并返回一个对象 $.getRequestData = function() { var url = location.search; //获取url中"?" ...