Problem: http://uva.onlinejudge.org/external/5/530.pdf
Last Submissions | ||||||
---|---|---|---|---|---|---|
Problem | Verdict | Lang | Time | Best | Rank | Submit Time |
| discuss530 - | Accepted | C++11 | 0.000 | 0.000 | 3421 | 2 mins ago |
Solution: (time AC: 0s)
#include<bits/stdc++.h>
#define int long long
using namespace std;
unordered_map<string, int> F;
string C(int n, int k){
return to_string(n)+" "+to_string(k);
}
int f(int n, int k){
if (F.count(C(n,k))) return F[C(n,k)];
if (k==1) return n;
if (k==n || k==0) return 1;
int C_k1_n1= f(n-1, k-1);
int C_k_n1= C_k1_n1 * (n-k) *1.0/ k; //2f -> 1f
return F[C(n,k)]= C_k1_n1 + C_k_n1;
//return F[n][k]= f(n-1, k-1) + f(n-1, k);
}
int32_t main(){
ios_base::sync_with_stdio(0);
int n, k;
while(cin >> n >> k && (n || k)){
k= min(k, n-k);
cout<<f(n,k)<<"\n";
}
}
No comments:
Post a Comment