1055. Combinations -- ACM RU
1055. Combinations
https://acm.timus.ru/problem.aspx?space=1&num=1055
思路
对于组合数 C(M,N) 不能使用公式计算最终值,然后再根据最终值,分解质因数,统计质因数个数;
因为这种情况会导致数值越界。
改变思路, 组合数公式实际上可以通过三部分计算得到, C(M, N) = M! / (N! * (M-N)!)
可以分别统计 每一个部分的 质因数
然后在M!的质因数的个数中,剔除 N!和(M-N)!质因数的个数
reference
---- N 个盒子,M个球,组合计数问题 -------
https://www.cnblogs.com/pycr/p/14392407.html
https://www.cnblogs.com/ke-xin/p/13532206.html
Code
引用:
https://www.cnblogs.com/rabbit-fan/p/17022356.html
#include <bits/stdc++.h> using namespace std; map<int,int>mp; int main(){ int m,n; cin>>m>>n; for(int i=1;i<=m;i++){ int num=i; for(int j=2;j*j<=i&&num!=1;j++){ while(num%j==0){ num/=j; mp[j]++; } } if(num!=1){ mp[num]++; } } for(int i=1;i<=n;i++){ int num=i; for(int j=2;j*j<=i&&num!=1;j++){ while(num%j==0){ num/=j; mp[j]--; } } if(num!=1){ mp[num]--; } } for(int i=1;i<=m-n;i++){ int num=i; for(int j=2;j*j<=i&&num!=1;j++){ while(num%j==0){ num/=j; mp[j]--; } } if(num!=1){ mp[num]--; } } int ans=0; for(auto t:mp){ if(t.second!=0){ ans++; } } cout<<ans<<endl; }
出处:http://www.cnblogs.com/lightsong/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。