n!素因子分解2
来源:互联网 发布:相机判断年龄算法 编辑:程序博客网 时间:2024/05/17 03:27
有的时候我们需要对n!进行素因子分解,如果我们对1~n的每一个数都进行素因子分解然后再进行合并的话那么显然是很慢的因此我们需要重新来考虑这个问题。
我们对n!进行素因子分解:
n! = 1*2*3*4*…*n,因此它一定含有小于等于n 的所有素数我们要对这些素数分别进行处理求出含有多少项。
例如我们求n!含有多少个素因子p
设f[n][p] 表示n!含有多少素因子p;
1*2*3*4*…n = p^(n/p)(1*2*,,,,*(n/p))
那么 f[n][p] = n/p + f[n/p][p],就可以通过递归的方法求出了。
题目
#include <iostream>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;const int mod = 1e9+7;const int maxn=1e6+5;bool prime[maxn];int p[maxn/10];int k;void isprime(){ k=0; LL i,j; memset(prime, true, sizeof(prime)); for(i=2; i<maxn; i++) { if(prime[i]) { p[k++]=i; for(j=i*i; j<maxn; j+=i) prime[j]=false; } }}LL get(LL m, LL p){ if(m<p) return 0; return m/p+get(m/p,p);}LL quickmod(LL a, LL b){ LL ans = 1; while(b) { if(b&1) ans=ans*a%mod; b>>=1; a=a*a%mod; } return ans ;}int main(){ isprime(); int n; while(~scanf("%d",&n)) { LL ans=1; LL m = quickmod(2,(LL)mod-2); for(int i=0; i<k&&p[i]<=n; i++) { LL tmp = (get(n,p[i])*2+1)%mod; ans=ans*tmp%mod; } ans = ans*m%mod; ans = (ans+m%mod)%mod; printf("%lld\n",ans); } return 0;}
0 0
- n!素因子分解2
- n! 素因子分解
- n!素因子分解
- n!的素因子分解
- n!的素因子分解
- N!的素因子分解
- N!分解素因子及若干问题
- 2-07. 素因子分解
- Uva 10780 素因子分解的应用(n的分解、n!的分解)
- n!的质因子分解
- POJ1401 数学N!因子分解
- N!的质因子分解
- SGU154 Factorial 【二分答案】【n!素因子分解】
- n!素因子分解中素数p的幂
- N!末尾有多少0(素因子分解)
- 2-07. 素因子分解(20)
- 2-07. 素因子分解(20)
- 2-07. 素因子分解(20)
- hihoCoder - 1152 - Lucky Substrings
- acm-UVA11935解题报告
- 前端在线demo网站
- iOS Quartz2D - 给图片加水印
- php-面向对象(6)
- n!素因子分解2
- iOS Quartz2D - 屏幕截图
- 浅谈iOS中MVVM的架构设计与团队协作
- struts_link标签
- 机房收费之充值窗体
- hdfs小文件的解决方案
- malloc/free与new/delete的区别
- HashMap的实现
- acm-hdu2544解题报告