| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 22473 | Accepted: 5522 |
Description

Input
Output
Sample Input
5 4 1 1 0 0
Sample Output
126 2
题意:从左下角到右上角的路径数目
思路:本来用的dp,后来发现dp方程正是杨辉三角的递推公式。。处理C(n,k)最重要的是防止阶乘越界,用递推式是个不错的选择,但难以防止MLE,下面这种“先除后乘”再强制类型转换以前在codeforces上用过,这次居然没想起来。。。看了题解后才AC了。。
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> using namespace std; const int maxn=10200; const int INF=(1<<28); typedef long long ll; ll n,m; ll C(ll n,ll k) { double res=1.0; if(k>n-k) k=n-k; ll a=n,b=k; while(b>0){ res*=a*1.0/b; a--; b--; } if(res-(ll)res>0.5) res++; return res; } int main() { while(cin>>n>>m,n||m){ cout<<C(n+m,n)<<endl; } return 0; }
原文:http://www.cnblogs.com/--560/p/4366373.html