Uva--10375(数论,组合,唯一分解)

本文介绍了一种计算两个组合数比值的方法,通过唯一素数分解的方式,避免了直接计算阶乘带来的数值溢出问题。适用于p, q, r, s等数值在10000以内的场景。

2014-09-01 20:08:45

Problem D: Choose and divide

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

         m!
C(m,n) = --------
         n!(m-n)!

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

The Input

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

The 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

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

Output for Sample Input

0.12587
505606.46055
1.28223
0.48996
2.00000
3.99960

思路:第二版小白书例题,参考了书里的思路,C(m,n) = m!/n!/(m-n)!,所以C(p,q) / C(r,s) = (p!*s!*(r-s)!) / (q!*r!*(p-q)!),因为p,q,r,s的大小都在10000以内,所以考虑分别对分子和分母进行唯一素数分解,从而求出答案的唯一素数分解。
 1 /*************************************************************************
 2     > File Name: 10375.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Mon 01 Sep 2014 07:25:01 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 
17 int anti_prime[10005];
18 int e[10005];
19 int p,q,r,s;
20 double ans;
21 vector<int> Prime;
22 
23 void Erato(int n){
24     memset(anti_prime,0,sizeof(anti_prime));
25     int m = sqrt(n + 0.5); //limit
26     for(int i = 2; i <= m; ++i) if(!anti_prime[i])
27         for(int j = i * i; j <= n; j += i) anti_prime[j] = 1;
28 }
29 
30 void Cal_factorial(int n,int tag){
31     for(int i = 2; i <= n; ++i){
32         int v = i;
33         for(int j = 0; j < Prime.size(); ++j){
34             while(v % Prime[j] == 0){
35                 v /= Prime[j];
36                 e[j] += tag;
37             }
38             if(v == 1) break;
39         }
40     }
41 }
42 
43 int main(){
44     Erato(10000);
45     for(int i = 2; i <= 10000; ++i) if(!anti_prime[i])
46         Prime.push_back(i);
47     while(scanf("%d%d%d%d",&p,&q,&r,&s) != EOF){
48         ans = 1.0;
49         memset(e,0,sizeof(e));
50         Cal_factorial(p,1);
51         Cal_factorial(s,1);
52         Cal_factorial(r - s,1);
53         Cal_factorial(q,-1);
54         Cal_factorial(p - q,-1);
55         Cal_factorial(r,-1);
56         for(int i = 0; i < Prime.size(); ++i)
57             ans *= pow(1.0 * Prime[i],1.0 * e[i]);
58         printf("%.5lf\n",ans);
59     }
60     return 0;
61 }
62 
63 
64         

 

转载于:https://www.cnblogs.com/naturepengchen/articles/3949945.html

【顶级EI复现】计及连锁故障传播路径的电力系统 N-k 多阶段双层优化及故障场景筛选模型(Matlab代码实现)内容概要:本文介绍了名为《【顶级EI复现】计及连锁故障传播路径的电力系统 N-k 多阶段双层优化及故障场景筛选模型(Matlab代码实现)》的研究资源,重点围绕电力系统中连锁故障的传播机制,提出了一种N-k多阶段双层优化模型,并结合故障场景筛选方法提升系统安全性与鲁棒性。该模型通过Matlab代码实现,可用于模拟复杂电力系统在多重故障下的响应特性,支持对关键故障路径的识别与优化决策,适用于高水平科研复现与工程仿真分析。文中还列举了大量相关技术方向的配套资源,涵盖智能优化算法、电力系统管理、机器学习、路径规划等多个领域,并提供了网盘链接以便获取完整代码与资料。; 适合人群:具备电力系统、优化理论及Matlab编程基础的研究生、科研人员及从事能源系统安全分析的工程技术人员,尤其适合致力于高水平论文(如EI/SCI)复现与创新的研究者。; 使用场景及目标:①复现顶级期刊关于N-k故障与连锁传播的优化模型;②开展电力系统韧性评估、故障传播分析与多阶段防御策略设计;③结合YALMIP等工具进行双层优化建模与场景筛选算法开发;④支撑科研项目、学位论文或学术成果转化。; 阅读建议:建议读者按照文档提供的目录顺序系统学习,优先掌握双层优化与场景筛选的核心思想,结合网盘中的Matlab代码进行调试与实验,同时参考文中提及的智能算法与电力系统建模范例,深化对复杂电力系统建模与优化的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值