Advertisement
chaosagent

USACO Cow Pedigrees

Jun 28th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 KB | None | 0 0
  1. /*
  2. ID: david.h2
  3. LANG: C++11
  4. TASK: nocows
  5. */
  6.  
  7. #include <iostream>
  8. #include <vector>
  9.  
  10. using namespace std;
  11.  
  12. vector<vector<int>> results(201, vector<int>(101, -1));
  13.  
  14. int findntrees(int n, int k) {
  15.     if (n == 0){
  16.         results[n][k] = 1;
  17.     } else if (k == 0) {
  18.         results[n][k] = 0;
  19.     } else if (results[n][k] == -1) {
  20.         int childpossibilities = 0;
  21.         for (int leftnodes = 0; leftnodes <= n - 2; leftnodes+=2) {
  22.             int rleft = findntrees(leftnodes, k - 1);
  23.             int rright = findntrees(n - 2 - leftnodes, k - 1);
  24.             childpossibilities += rleft * rright;
  25.             childpossibilities %= 9901;
  26.         }
  27.         results[n][k] = childpossibilities % 9901;
  28.     }
  29.     return results[n][k] % 9901;
  30. }
  31.  
  32. int main() {
  33.     FILE *fin = fopen("nocows.in", "r");
  34.     FILE *fout = fopen("nocows.out", "w");
  35.     int n, k;
  36.     fscanf(fin, "%d %d", &n, &k);
  37.     if (n % 2 == 0) {
  38.         fprintf(fout, "%d\n", 0);
  39.         return 0;
  40.     }
  41.     n -= 1;
  42.     k -= 1;
  43.     fprintf(fout, "%d\n", ((findntrees(n, k) + 9901) - findntrees(n, k - 1)) % 9901);
  44.     return 0;
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement