博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA1635 Irrelevant Elements —— 唯一分解定理 + 二项式定理
阅读量:4597 次
发布时间:2019-06-09

本文共 1956 字,大约阅读时间需要 6 分钟。

题目链接:

(紫书320)

题解:

1.根据二项式定理, 可得递推公式: C(n,k) = (n-k+1)/k * C(n, k-1)

2.某一项与余数(%m)无关, 即表明该项的的系数是m的倍数, 由于 1<=n<=1e5, 直接运算的话肯定溢出。

所以 :将数字进行分解质因数, 记录质因子以及其个数。由于题目只需判断某项的系数是否为m的倍数, 所以只需要考虑m所拥有的质因子。

3.fac[i]记录m的质因数, num_m[i]记录m的质因数fac[i]的个数, num_c[i]记录二项式系数(动态)的质因数fac[i]的个数。

4.对于所有的i, 如果num_c[i] >= num_m[i] 则表明此系数是m的倍数, 即此项与余数无关。

代码如下:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 using namespace std;14 #define pb push_back15 #define mp make_pair16 #define ms(a, b) memset((a), (b), sizeof(a))17 #define eps 0.000000118 typedef long long LL;19 const int INF = 2e9;20 const LL LNF = 9e18;21 const int mod = 1e9+7;22 const int maxn = 1e5+10;23 24 int n, m, cnt;25 int fac[maxn], num_m[maxn], num_c[maxn];26 int ans[maxn], sum;27 28 void init()29 {30 n = n - 1; //题目从a[1]~a[n], 而二项式定理中, 从a[0]~a[n], 所以要与二项式定理中的n对应。31 ms(num_m, 0);32 ms(num_c, 0);33 cnt = 0;34 35 int tmp = m;36 for(int i = 2; i*i<=tmp; i++)37 {38 if(tmp%i==0)39 {40 fac[++cnt] = i;41 while(tmp%i==0) tmp /= i, num_m[cnt]++;42 }43 }44 if(tmp>1) fac[++cnt] = tmp, num_m[cnt]++;45 }46 47 int test(int x)48 {49 int a = n-x+1;50 int b = x;51 52 for(int i = 1; i<=cnt; i++)53 {54 while(a%fac[i]==0) num_c[i]++, a /= fac[i];55 while(b%fac[i]==0) num_c[i]--, b /= fac[i];56 }57 58 for(int i = 1; i<=cnt; i++)59 if(num_m[i]>num_c[i]) return 0;60 return 1;61 }62 63 void solve()64 {65 sum = 0;66 for(int i = 1; i<=n-1; i++) //二项式的第0项和第n项都为1, 不需要考虑67 if(test(i))68 ans[++sum] = i+1; //二项式的第i项, 对应题目中的第i+1项69 70 printf("%d\n", sum);71 for(int i = 1; i<=sum; i++)72 printf("%s%d", i==1?"":" ", ans[i]);73 putchar('\n');74 }75 76 int main()77 {78 while(scanf("%d%d",&n, &m)!=EOF)79 {80 init();81 solve();82 }83 }
View Code

 

 

 

 

 

转载于:https://www.cnblogs.com/DOLFAMINGO/p/7538687.html

你可能感兴趣的文章
Apache Kafka - KIP-42: Add Producer and Consumer Interceptors
查看>>
ArcGIS JS Demo
查看>>
webservice发布问题,部署iis后调用不成功
查看>>
Koch 分形,海岸线,雪花
查看>>
ubuntu系统下Python虚拟环境的安装和使用
查看>>
IOS7开发~新UI学起(二)
查看>>
软件过程度量和CMMI模型概述
查看>>
数据结构(DataStructure)与算法(Algorithm)、STL应用
查看>>
Linux与Windows xp操作系统启动过程
查看>>
linux运维、架构之路-Kubernetes1.13离线集群部署双向认证
查看>>
[Leetcode]Substring with Concatenation of All Words
查看>>
Gem install rmagick 报错问题~
查看>>
验证一个方法触发时机
查看>>
25句充满正能量的句子
查看>>
python学习手册笔记——27.更多实例
查看>>
Spring Cloud Alibaba 新版本发布:众多期待内容整合打包加入!
查看>>
Android Camera 使用小结
查看>>
20170908 校内模拟赛 游戏
查看>>
P1774 最接近神的人_NOI导刊2010提高(02)
查看>>
4245: [ONTAK2015]OR-XOR
查看>>