Vijos——P1137 组合数

https://vijos.org/p/1137

描述

组合公式 C=N!/(M!*(N-M)!). 问题是求 C 中不同的质因子的个数
例如 N=7, M=4. C=7!/(3!*4!)=5040/(6*24)=35=5*7. 则不同的质因子的个数为2 (分别是5,7)。

格式

输入格式

输入N,M

输出格式

输出一个整数

样例1

样例输入1

7 4

样例输出1

2

限制

质因数分解。。

 1 #include <cstdio>
 2 
 3 inline void read(int &x)
 4 {
 5     x=0; register char ch=getchar();
 6     for(; ch>'9'||ch<'0'; ) ch=getchar();
 7     for(; ch>='0'&&ch<='9'; ch=getchar()) x=x*10+ch-'0';
 8 }
 9 
10 const int N(100521);
11 int n,m,pri[N],cnt;
12 bool no_pri[N];
13 
14 inline void Get_Prime(int lim)
15 {
16     no_pri[1]=1;
17     for(int i=2; i<=lim; ++i)
18     {
19         if(!no_pri[i]) pri[++cnt]=i;
20         for(int j=1; j<=cnt; ++j)
21         {
22             if(pri[j]*i>lim) break;
23             no_pri[pri[j]*i]=true;
24             if(i%pri[j]==0) break;
25         }
26     }
27 }
28 
29 int _if[N],ans;
30 inline void Update(int x,int op)
31 {
32     for(int i=1; i<=cnt; ++i)
33     {
34         if(x%pri[i]) continue;
35         x/=pri[i]; _if[i]+=op;
36         for(; x%pri[i]==0; )
37             _if[i]+=op,x/=pri[i];
38     }
39 }
40 
41 int Presist()
42 {
43     read(n),read(m);
44     Get_Prime(n+1);
45     for(int i=m+1; i<=n; ++i) Update(i, 1);
46     for(int i=2; i<=n-m; ++i) Update(i,-1);
47     for(int i=1; i<=cnt; ++i) ans+=(_if[i]>0);
48     printf("%d
",ans);
49     return 0;
50 }
51 
52 int Aptal=Presist();
53 int main(int argc,char**argv){;}
——每当你想要放弃的时候,就想想是为了什么才一路坚持到现在。
原文地址:https://www.cnblogs.com/Shy-key/p/7860250.html