UVA - 10375

Choose and divide
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 4053   Accepted: 1318

Description

The binomial coefficient C(m,n) is defined as

  1. m!

  2. C(m,n) = --------

  3. n!(m-n)!

Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s).

Input

Input consists of a sequence of lines. Each line contains four non-negative integer numbers giving values for p, q, r, and s, respectively, separated by a single space. All the numbers will be smaller than 10,000 with p>=q and r>=s.

Output

For each line of input, print a single line containing a real number with 5 digits of precision in the fraction, giving the number as described above. You may assume the result is not greater than 100,000,000.

Sample Input

  1. 10 5 14 9
  2. 93 45 84 59
  3. 145 95 143 92
  4. 995 487 996 488
  5. 2000 1000 1999 999
  6. 9998 4999 9996 4998

Sample Output

  1. 0.12587
  2. 505606.46055
  3. 1.28223
  4. 0.48996
  5. 2.00000
  6. 3.99960

Source


唯一分解定理

太诡异了,uva AC

poj一直TLE,连白书的标解都WA

  1. //
  2. // main.cpp
  3. // poj2613
  4. //
  5. // Created by Candy on 10/20/16.
  6. // Copyright ? 2016 Candy. All rights reserved.
  7. //
  8.  
  9. #include <iostream>
  10. #include <cstdio>
  11. #include <cstring>
  12. #include <algorithm>
  13. #include <cmath>
  14. using namespace std;
  15. const int N=1e4+;
  16. int p,q,r,s;
  17. int prime[N],cnt=,e[N],vis[N];
  18. void era(int n){
  19. int m=sqrt(n)+;
  20. for(int i=;i<=m;i++) if(!vis[i])
  21. for(int j=i*i;j<=n;j+=i) vis[j]=;
  22. for(int i=;i<=n;i++) if(!vis[i]) prime[++cnt]=i;
  23. }
  24. inline void mul(int x,int d){//x^d
  25. for(int i=;i<=cnt&&x!=;i++)
  26. while(x%prime[i]==){
  27. x/=prime[i];
  28. e[i]+=d;
  29. }
  30. }
  31. void fac(int x,int d){//printf("%d %d\n",x,d);
  32. for(int i=;i<=x;i++) mul(i,d);
  33. }
  34. inline int fastPow(int a,int b){
  35. int ans=;
  36. for(;b;b>>=,a*=a)
  37. if(b&) ans*=a;
  38. return ans;
  39. }
  40. int main(int argc, const char * argv[]){
  41. era();//cout<<"p";
  42. while(scanf("%d%d%d%d",&p,&q,&r,&s)!=EOF){
  43. memset(e,,sizeof(e));
  44. fac(p,);
  45. fac(q,-);
  46. fac(p-q,-);
  47. fac(r,-);
  48. fac(s,);
  49. fac(r-s,);
  50. double ans=;
  51. for(int i=;i<=cnt;i++){
  52. if(e[i]>) ans*=(double)fastPow(prime[i],e[i]);
  53. else if(e[i]<) ans/=(double)fastPow(prime[i],-e[i]);
  54. // ans*=pow(prime[i],e[i]);
  55. }
  56. printf("%.5f\n",ans);
  57. }
  58.  
  59. return ;
  60. }

UVA - 10375 Choose and divide[唯一分解定理]的更多相关文章

  1. 【暑假】[数学]UVa 10375 Choose and divide

    UVa 10375 Choose and divide 题目: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19601 思路 ...

  2. uva10375 Choose and Divide(唯一分解定理)

    uva10375 Choose and Divide(唯一分解定理) 题意: 已知C(m,n)=m! / (n!*(m-n!)),输入整数p,q,r,s(p>=q,r>=s,p,q,r,s ...

  3. UVA 10375 Choose and divide【唯一分解定理】

    题意:求C(p,q)/C(r,s),4个数均小于10000,答案不大于10^8 思路:根据答案的范围猜测,不需要使用高精度.根据唯一分解定理,每一个数都可以分解成若干素数相乘.先求出10000以内的所 ...

  4. UVA 10375 Choose and divide(大数的表示)

    紫上给得比较奇怪,其实没有必要用唯一分解定理.我觉得这道题用唯一分解只是为了表示大数. 但是分解得到的幂,累乘的时候如果顺序很奇怪也可能溢出.其实直接边乘边除就好了.因为答案保证不会溢出, 设定一个精 ...

  5. UVa 10375 - Choose and divide(唯一分解定理)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. UVA 10375 Choose and divide

    n! 分解素因子 快速幂 ei=[N/pi^1]+ [N/pi^2]+ …… + [N/pi^n]  其中[]为取整 ei 为数 N!中pi 因子的个数: #include <iostream& ...

  7. Uva 10375 选择与除法 唯一分解定理

    题目链接:https://vjudge.net/contest/156903#problem/E 题意:已知 求:C(p,q)/C(r,s) 其中p,q,r,s都是10^4,硬算是肯定超数据类型的. ...

  8. UVA.10791 Minimum Sum LCM (唯一分解定理)

    UVA.10791 Minimum Sum LCM (唯一分解定理) 题意分析 也是利用唯一分解定理,但是要注意,分解的时候要循环(sqrt(num+1))次,并要对最后的num结果进行判断. 代码总 ...

  9. Choose and divide(唯一分解定理)

    首先说一下什么是唯一分解定理 唯一分解定理:任何一个大于1的自然数N,如果N不是质数,那么N可以分解成有限个素数的乘积:例:N=(p1^a1)*(p2^a2)*(p3^a3)......其中p1< ...

随机推荐

  1. java函数

    函数的封装没有定规,只要遵循语法,函数如何封装按照需求来做 函数四要素:函数名,输入,加工,输出(返回). 一.函数调用 1.函数名(变量列表); 没有返回值. 2.数据类型 变量名=函数名(变量列表 ...

  2. SSH(Struts2+Spring+Hibernate)框架搭建流程<注解的方式创建Bean>

    此篇讲的是MyEclipse9工具提供的支持搭建自加包有代码也是相同:用户登录与注册的例子,表字段只有name,password. SSH,xml方式搭建文章链接地址:http://www.cnblo ...

  3. 对hashmap,hashset,hashtable的理解

    1.首先先理一下java的集合关系,Collection和Map接口是所有集合接口的根结点,其他集合都直接或者间接的实现了他们中的一个:collection下有:list(元素可重复)和set(不可重 ...

  4. MySQL中CURRENT_TIMESTAMP(转)

    1. MySQL 获得当前时间戳函数:current_timestamp, current_timestamp()  代码如下   mysql> select current_timestamp ...

  5. 关于C#消息调度(作业日志)

    在Windows定时作业中,其实有多种关于作业调度形式,比如Windows Services 和 Windows Form 都可以做到,各有各的好处.现在来介绍下使用插件的形式进行定时作业. 1.用q ...

  6. iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  7. iOS之UICollectionView详解

    UICollectionView是一种类似于UITableView但又比UITableView功能更强大.更灵活的视图,这是源于它将UICollectionView对cell的布局交给了UIColle ...

  8. 设置 TabBarItem 选中时的图片及文字颜色

    TabBarController 是在 ios 开发过程中使用较为频繁的一个 Controller,但是在使用过程中经常会遇到一些问题,例如本文所要解决的,如何修改 TabBar 选中时文字及图片的颜 ...

  9. iOS 本地推送通知

    1.什么是本地推送通知 不需要联网的情况下,应用程序经由系统发出的通知 2.本地推送的使用场景 定时提醒,如玩游戏.记账.闹钟.备忘录等 3.实现本地推送通知的步骤 创建本地推送通知的对象UILoca ...

  10. org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

    org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not a ...