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. 『零行代码』解决键盘遮挡问题(iOS)

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

  2. angularjs-ngModel 控制页面的宽度

    js NiDialog.open({ windowClass: '', size:'elements', backdrop: 'static', keyboard: false, templateUr ...

  3. org.springframework.beans.factory.BeanCreationException: 求教育!

    2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...

  4. PL/SQL Developer远程连接Oracle数据库

    首先打开电脑,到pl/sql安装的指定目录[D:\app\DZL\product\11.2.0\dbhome_1\NETWORK\ADMIN]找到[tnsnames.ora]     打开[tnsna ...

  5. animationWithKeyPath键值对

    animationWithKeyPath键值对的方式来改变动画 <Jacky Shin:可以从这个网址查到哪些可以做为动画效果, 打开xcode帮助,搜索animatable propertie ...

  6. 在万网虚拟主机上部署MVC5

    参考 要想部署mvc,需要把一些mvc用到的全局程序集改为本地部署,通过N次试验,终于搞定. 特写个备忘录,免得以后忘了. 首先更改web.config,在里面加上 <system.web> ...

  7. 【POJ2761】【fhq treap】A Simple Problem with Integers

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  8. H5相关

    对于容器元素,尤其在做移动端产品时候,我们很自然会让其居中定位: .container { position: absolute; left: %; top: %; transform: transl ...

  9. [jQuery编程挑战]004 针对选择框词典式排序

    <!DOCTYPE html> <html lang="zh"> <head> <meta charset="utf-8&quo ...

  10. Redis 中的事务

    Redis支持简单的事务 Redis与mysql事务的对比 Mysql Redis 开启 start transaction muitl 语句 普通sql 普通命令 失败 rollback 回滚 di ...