题目大意 给你一个n*m的矩阵,最开始在左上角,只能向下或者向右,求,从左上走到右下的所有路线的方案数。点击进入原题地址 不难发现,需要走n+m步,然后从n+m中挑出n个走下,即答案就是
但是! 本题的询问较多,且差距较大,是无法通过递推得到的。 所以就要利用一种求单个组合数的方法。
思路 的本质是 所以我们把它当成k个 分数 乘起来就好了。(分子分母分别乘可能会爆longlong) 即ans= 由于浮点数可能会产生误差,所以最终结果要加上0.5再取整
代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 #include <cstdio> #include <iostream> #include <cstring> using namespace std;int n,m;unsigned calc (unsigned n,unsigned m) { double ans=1.0 ,a=(double )m+n,b=(double )min (n,m); while (b){ ans*=a/b; a--;b--; } ans+=0.5 ; return (unsigned )ans; } int main () { while (~scanf ("%d%d" ,&n,&m)){ if (!m&&!n) break ; printf ("%u\n" ,calc (n,m)); } return 0 ; }
“人们都在沉醉,人们都已忘却,人们都装作看都这结尾,一味陷入争辩,无人聆听箴言,该可悲可泣或改叹可怜。”–《绝体绝命》(阿良良木健/洛天依)