Explanation
Let's say that we want to find the answer for some subsection of the sample input. Then, we would need the number of elements to consider for Farmer John and Farmer Paul, and the size of the team. A naive approach would be to go through these in time, and due to the low bounds on , , and , this'll actually run in time. So, we'll just run a bottom-up DP where
= the number of teams of size after we've considered the first cows in Farmer John's team, the first cows in Farmer Paul's team.
Then, our transition would be:
=
If FJ's th cow is greater than FP's th cow, we also add , because by pairing up those two cows, we only have more spots to consider.
Implementation
Time Complexity:
C++
#include <bits/stdc++.h>using std::vector;const int MOD = 1e9 + 9;int main() {freopen("team.in", "r", stdin);freopen("team.out", "w", stdout);int n, m, k;
Java
import java.io.*;import java.util.*;public class Team {public static void main(String[] args) throws IOException {Kattio io = new Kattio("team");final int MOD = 1000000009;int n = io.nextInt();int m = io.nextInt();
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!