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. Python中sys.argv[]的用法

    类似bash中的$0, $1, $2... sys.argv[0]:文件名 sys.argv[1]:第一个参数 sys.argv[2]:第二个参数 import sys print "The ...

  2. Log4j(1.2.17) - hello world

    1. Maven 依赖 <dependencies> <dependency> <groupId>log4j</groupId> <artifac ...

  3. Android性能优化之ViewStub

    http://www.cnblogs.com/lwbqqyumidi/p/4047108.html ViewStub是Android布局优化中一个很不错的标签/控件,直接继承自View.虽然Andro ...

  4. java中json转xml

    参考:http://heipark.iteye.com/blog/1394844 需要json-lib-2.1-jdk15.jar和xom-1.2.5.jar,maven pom.xml如下: xml ...

  5. EntityFramework在root目录web.config中的配置设置

    未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序.请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序.有关详 ...

  6. 拦截器getmodel方法什么时候被调用(没搞懂有什么鸟用,自己搭的项目中用到了这个)

    拦截器是Struts2最强大的特性之一,它是一种可以让用户在Action执行之前和Result执行之后进行一些功能处理的机制.Struts2 的预定义拦截器 modelDriven 如果action实 ...

  7. 查找被锁对象的名称、sid,锁定的类型-1123

    select lk.sid,lk_obj.object_id,obj.object_name,DECODE(LK.LMODE,0,'None',1,'Null',2,'Row-S (SS)',3,'R ...

  8. Plsql工具单步调试 存储过程或是 函数(oracle数据库)-留着自己用的

    <案例1> 原地址: http://jingyan.baidu.com/article/3a2f7c2e144d2826aed61167.html 调试过程对找到一个存过的bug或错误是非 ...

  9. 关于@property()的那些属性及ARC简介【nonatomic,atomic,assign,retain,strong,weak,copy。】

    @property()常用的属性有:nonatomic,atomic,assign,retain,strong,weak,copy. 其中atomic和nonatomic用来决定编译器生成的gette ...

  10. C#入门经典(第五版)学习笔记(二)

    ---------------函数---------------参数数组:可指定一个特定的参数,必须是最后一个参数,可使用个数不定的参数调用函数,用params关键字定义它们 例如: static i ...