UVa 10375 Choose and divide

题目:

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19601

思路:

maxn=10000 如果计算maxn!再保存的话显然装不下。

但答案由阶乘的积或商组成,所以可以用唯一分解定理求解。大题思路就是把目前答案的质因子的指数用数组e保存,乘除都对e操作。

需要注意的是筛法求素数优化后的写法。

代码:

 #include<iostream>
#include<cstdio>
#include<iomanip>
#include<vector>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int maxn = + ; vector<int> primes;
void make_primes() {
int vis[maxn];
memset(vis,,sizeof(vis));
int m=sqrt(maxn+0.5);
for(int i=;i<=m;i++) if(!vis[i]) {
primes.push_back(i);
for(int j=i*i;j<maxn;j+=i) vis[j]=;
}
for(int i=m;i<maxn;i++) //primes分两部分 m之后也有质数 不能忽略
if(!vis[i]) primes.push_back(i);
}
/*
bool is_prime(int n) {
int m = floor(sqrt(n) + 0.5);
for(int a = 2; a <= m; a++)
if(n % a == 0) return false;
return true;
}
void make_primes(){
for(int i=2;i<=10000;i++)
if(is_prime(i)) primes.push_back(i);
}
*/ int e[maxn]; //第i个质数的指数 void add_integer(int x,int d){
for(int i=;i<primes.size();i++) {
int m=primes[i];
while(x%m==){
x/=m; e[i]+= d;
}
if(x==) break;
}
}
void add_factorial(int x,int d){
for(int i=;i<=x;i++)
add_integer(i,d);
} int main() {
make_primes(); //return primes
int p,q,r,s;
while(cin>>p>>q>>r>>s) {
memset(e,,sizeof(e));
//add_factorial(a,d); 向质子表中乘(a!)^d;
add_factorial(p,);
add_factorial(q,-);
add_factorial(p-q,-);
add_factorial(s,);
add_factorial(r-s,);
add_factorial(r,-); double ans=1.0;
for(int i=;i<primes.size();i++)
ans *= pow(primes[i],e[i]);
//cout<<setw(5)<<ans<<"\n";
printf("%.5lf\n",ans);
}
return ;
}

【暑假】[数学]UVa 10375 Choose and divide的更多相关文章

  1. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

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

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

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

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

  4. UVA 10375 Choose and divide

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

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

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

  6. 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 ...

  7. UVA10375 Choose and divide 质因数分解

    质因数分解: Choose and divide Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %l ...

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

    题意: 求组合数C(p, q) / C(r, s)结果保留5为小数. 分析: 先用筛法求出10000以内的质数,然后计算每个素数对应的指数,最后再根据指数计算答案. #include <cstd ...

  9. 【暑假】[数学]UVa 1262 Password

    UVa 1262  Password 题目: Password   Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld ...

随机推荐

  1. PAT-乙级-1012. 数字分类 (20)

    1012. 数字分类 (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一系列正整数,请按要求对数字进 ...

  2. List<T> 排序

    List<T>的排序 List<DataPoint> dataPointsDataPints = ...; //按RegisterAddress升序排序 dataPointsD ...

  3. 定长内存池之BOOST::pool

    内存池可有效降低动态申请内存的次数,减少与内核态的交互,提升系统性能,减少内存碎片,增加内存空间使用率,避免内存泄漏的可能性,这么多的优点,没有理由不在系统中使用该技术. 内存池分类: 1.      ...

  4. hdu 4336 Card Collector 容斥原理

    读完题目就知道要使用容斥原理做! 下面用的是二进制实现的容斥原理,详见:http://www.cnblogs.com/xin-hua/p/3213050.html 代码如下: #include< ...

  5. 获取Android系统时间

    目的: 输入 2014-09-09 14:02:03    输出 等待:1小时20分 注意: HH:mm:ss 为获取手机 24小时格式的时间  15:03    hh:mm:ss 为12小时模式的时 ...

  6. Spring在代码中获取bean的几种方式

    方法一:在初始化时保存ApplicationContext对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObj ...

  7. android 64位的so文件 报错

     问题解决了,原因是因为豌豆荚搞了个64位的so文件,然后其他用到so的就必须也要有64位的,把他们的64位的so文件删除了,就OK了...

  8. ZooKeeper 安装部署及hello world

    ZooKeeper  安装部署及hello world 先给一堆学习文档,方便以后查看官网文档地址大全: OverView(概述)http://zookeeper.apache.org/doc/r3. ...

  9. 190. Reverse Bits

    题目: Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented ...

  10. Android安装常见问题

    在初次运行Android程序的时候会出现类似的错误,导致程序无法继续运行,如下面的几个例子: 问题1:PC安卓模拟器 PANIC: Could not open: C:\Documents and S ...