计数类问题中的取模运算总结
本篇随笔简单讲解一下信息学奥林匹克竞赛中计数类问题中的取模运算相关知识。算是一篇总结性质的博客,将会简单介绍在计数类问题进行答案统计的时候如何对算式进行合法的取模。
加法运算
如果是形如((a+b)\,\,mod\,\,p)的式子,可以变成:(a\,\,mod\,\,p+b\,\,mod\,\,p)。
减法运算
因为减法是加法的一种相反数运算,所以减法的取模运算也满足上面的式子:
((a-b)\,\,mod\,\,p)可以变成:(a\,\,mod\,\,p-b\,\,mod\,\,p)。
乘法运算
乘法运算可以被看成一种累加运算,所以乘法的取模运算也满足上面加法运算的相关式子:
即((a imes b)\,\,mod\,\,p)可以变成(a\,\,mod\,\,p imes b\,\,mod\,\,p)。
除法运算
除法运算在数学中一直是一种比较独特的运算,虽然它也是四则混合运算之一,但它的运算方式不一样。
好话说三遍:不一样!不一样!不一样!
也就是说,((a/b)\,\,mod\,\,p)并不等于(frac{a\,\,mod\,\,p}{b\,\,mod\,\,p})。
它的取模运算法则是:分子乘以分母的乘法逆元。
也就是说:
[a/bequiv a imes b^{p-2}\,\,\,(mod\,\,p)
]
乘方运算
其实我觉得我们只需要注意一下除法运算的取模,因为乘方运算的取模说白了就是连乘,而我们已经学会了乘法的取模运算,那么就只需要一直乘上去即可:
即:
[a^bequiv (a\%p)^b\,\,\,(mod\,\,p)
]