UVa10375:选择与除法(唯一分解定理)
The binomial coefficient C(m,n) is defined as

Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s).
这是二次项系数,现在给出p,q,r,s,计算C(p,q)除以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.
输入:包含多组数据,pqrs均小于10000的非负整数。
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.
输出:每行输出除法的结果保留小数点后5位结果,结果保证不超过一亿。
Sample Input
10 5 14 9
93 45 84 59
145 95 143 92
995 487 996 488
2000 1000 1999 999
9998 4999 9996 4998
Sample Output
0.12587
505606.46055
1.28223
0.48996
2.00000
3.99960
思路:指数级的乘法结果太大,运用一些约分的方法防爆。
方法一:
唯一分解定理以前介绍过,大意就是每个数都可以唯一地分解为一堆质数的幂的乘积,如90 = 2 * 3² * 5.其中2和5是一次幂,3是二次幂。
这样我们回到题目数据,显然很多时候我们把分子分母都分解以后会发现能约掉很多质数的幂,这样的话可以采取这样一种策略:用一个数组记录每个质数的幂指数,如果是分子的话就加,是分母的话就减,免去了反复的无用乘除法。当把分子分母们都处理完以后,约分后的分子分母就是答案的分数形式了,此时再求出结果即可。
先把10000以内的素数都记录下来。
然后对每个数据来说,p、s、r-s是分子,q、p-q、r是分母。
接着把这六个数分别代入修改函数,修改素数幂指数数组的值,对于此数组,以90为例,应为every_count={1,2,1,0,0,0,……}。
详见代码,220ms:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std; int p, q, r, s, primes_size/*10000以内素数个数*/;
int all_primes[];//10000以内素数的按顺序具体记录
int every_count[];//每个素数幂指数的记录数组
bool is_composite[];//判定是否为合数 void modify_count(int a, int d)
{
for (int i = ; a > && i < primes_size; i++)
while (a % all_primes[i] == )
{
every_count[i] += d;//1与-1的传入使得修改写起来简便
a /= all_primes[i];
}
} void add_factorial(int n, int d)
{
for (int i = ; i <= n; i++)//n!
modify_count(i, d);//修改every_count数组
} void get_primes()
{
for (int i = ; i < ; i++)
if (!is_composite[i])
{
for (int j = i*i; j < ; j += i)
is_composite[j] = true;
all_primes[primes_size++] = i;
}
} int main()
{
get_primes();//埃氏筛素数 while (cin >> p >> q >> r >> s)
{
memset(every_count, , sizeof(every_count)); //进行6次分解,1与-1的用途见函数内部
add_factorial(p, );
add_factorial(p-q, -);
add_factorial(q, -); add_factorial(r, -);
add_factorial(r-s, );
add_factorial(s, ); //最后利用素数数组一次性求得答案
double ans = 1.0;
for (int i = ; i < primes_size; i++)
ans *= pow(all_primes[i], every_count[i]); cout << fixed << setprecision() << ans << endl;
} return ;
}
方法二:
也是约分的方式,数学知识降维打击。想必大家都知道上边下边的感叹号是可以约下去一堆的。这里有个知识是,如果你把m!约下去n!,剩下的是n+1~m,数字个数与1~m-n+1相等。也就是可以进行一一对应的double结果乘除。同理约下去(m-n)!也是一样的。
见代码即懂,0ms:
#include <cstdio> int main()
{
int p, q, r, s;
while (~scanf("%d%d%d%d", &p, &q, &r, &s))
{
double ans = 1.0;
for (int i = , j = p-q+, x = , y = r-s+; i <= q || x <= s;)
{
if (i <= q) ans *= (double)(j++)/(i++); if (x <= s) ans *= (double)(x++)/(y++);
}
printf("%.5lf\n", ans);
}
}
最后,虽然方法二严重打击了方法一的信心与自尊心,不过方法一的思想还是很不错的,值得学习。
UVa10375:选择与除法(唯一分解定理)的更多相关文章
- Uva 10375 选择与除法 唯一分解定理
题目链接:https://vjudge.net/contest/156903#problem/E 题意:已知 求:C(p,q)/C(r,s) 其中p,q,r,s都是10^4,硬算是肯定超数据类型的. ...
- 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 ...
- UVA10375 选择与除法 Choose and divide 题解
题目链接: https://www.luogu.org/problemnew/show/UVA10375 分析: 这道题可以用唯一分解定理来做. 什么是唯一分解定理?百度即可,这里也简介一下. 对于任 ...
- Choose and divide(唯一分解定理)
首先说一下什么是唯一分解定理 唯一分解定理:任何一个大于1的自然数N,如果N不是质数,那么N可以分解成有限个素数的乘积:例:N=(p1^a1)*(p2^a2)*(p3^a3)......其中p1< ...
- NOIP2009Hankson 的趣味题[唯一分解定理|暴力]
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫 Hankson.现 在,刚刚放学回家的 Hankson 正在思考一个有趣的问题. 今天在课堂上,老师讲 ...
- POJ - 1845 G - Sumdiv (唯一分解定理)
Consider two natural numbers A and B. Let S be the sum of all natural divisors of A^B. Determine S m ...
- HDU 6069 Counting Divisors(唯一分解定理+因子数)
http://acm.hdu.edu.cn/showproblem.php?pid=6069 题意: 思路: 根据唯一分解定理,$n={a_{1}}^{p1}*{a2_{}}^{p2}...*{a_{ ...
- Irrelevant Elements UVA - 1635 二项式定理+组合数公式+素数筛+唯一分解定理
/** 题目:Irrelevant Elements UVA - 1635 链接:https://vjudge.net/problem/UVA-1635 题意:給定n,m;題意抽象成(a+b)^(n- ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
随机推荐
- Android Weekly Notes Issue #240
Android Weekly Issue #240 January 15th, 2017 Android Weekly Issue #240 Hello, 各位亲, 从本篇笔记开始, 以后并不包含An ...
- Ubuntu Linux系统环境变量配置文件【转】
本文转载自:https://my.oschina.net/qinlinwang/blog/30471 Ubuntu Linux系统环境变量配置文件: /etc/profile : 在登录时,操作系统 ...
- 如何创建WindowsService
创建Windows Service可分为以下几步: 1. 创建一个“Windows Service”项目 2. 设置服务的相关属性,以确定服务的名称及工作机制 属性 设置 ServiceName 服务 ...
- yii中调取字段名称时label与labelEx的区别
$form = $this->beginWidget('CActiveForm',array('id' => 'userRegisterForm')); echo $form->la ...
- 页面渲染——简化paint复杂程度和区域
Paint是填充像素并且最后合成在用户的屏幕上的过程. 通常是在管道中耗费最大的,你要尽可能的避免使用paint. 动画中使用除了transform和opacity的动画属性都将触发paint pai ...
- C++软件工程师,你该会什么?
请尊重原创: 转载注明来源 原创在这里哦 C语言广泛用于基础软件.桌面系统.网络通信.音频视频.游戏娱乐等诸多领域.是世界上使用最广泛的编程语言之一.随着物联网技术的发展,C/C++技术在3G网络 ...
- codeforces 669A A. Little Artem and Presents(水题)
题目链接: A. Little Artem and Presents time limit per test 2 seconds memory limit per test 256 megabytes ...
- hdu-5596 GTW likes gt(模拟+优先队列)
题目链接: GTW likes gt Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- 51nod1674:区间的价值2(分治,利用&和|的收敛性)
lyk拥有一个区间. 它规定一个区间的价值为这个区间中所有数and起来的值与这个区间所有数or起来的值的乘积. 例如3个数2,3,6.它们and起来的值为2,or起来的值为7,这个区间对答案的贡献为2 ...
- BZOJ_3786_星系探索_splay维护出栈入栈序
BZOJ_3786_星系探索_splay维护出栈入栈序 Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为 ...