Implementation
C++
#include <cassert>#include <iostream>#include <vector>const int MOD = 1e9 + 7;long long pow(long long base, long long exp) {assert(exp >= 0);base %= MOD;long long res = 1;
Java
import java.io.*;public class Bots {private static final int MOD = (int)Math.pow(10, 9) + 7;public static void main(String[] args) throws IOException {int max_moves = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());long total_states = 1;long curr_level = 1;
Python
Warning!
Due to Python's slow speed, the below code only passes all test cases if you submit with PyPy.
MOD = int(1e9) + 7def mod_inv(n: int):return pow(n, MOD - 2, MOD)max_moves = int(input())total_states = 1
Solution 2
Let us first define a DP recurrence relation, with being the number of unique states where the blue bot has made moves and the red bot has made moves:
It follows that the answer is the sum of all the elements in .
This is what might look like for :
| 1 | 1 | 1 | 1 | 1 |
| 1 | 2 | 3 | 4 | 5 |
| 1 | 3 | 6 | 10 | 15 |
| 1 | 4 | 10 | 20 | 35 |
| 1 | 5 | 15 | 35 | 70 |
Note that these numbers are exactly the same as the ones in Pascal's triangle, only rotated a bit! These numbers form a rotated square of length (henceforth referred to as ), and we can express its sum as
Using the hockey stick identity, we see that this equals
We also know from the hockey stick identity that . Thus, we get that
Implementation 2
Time Complexity:
C++
#include <cassert>#include <iostream>#include <vector>using std::cout;using std::endl;using std::pair;const int MOD = 1e9 + 7;
Java
import java.io.*;public class Bots {private static final int MOD = (int)Math.pow(10, 9) + 7;public static void main(String[] args) throws IOException {int maxMoves = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());int[] resBinom = new int[] {2 * maxMoves + 2, maxMoves + 1};long resNum = 1;
Python
Warning!
Due to Python's slow speed, the below code only passes all test cases if you submit with PyPy.
MOD = int(1e9) + 7def mod_inv(n: int):return pow(n, MOD - 2, MOD)max_moves = int(input())res_binom = [2 * max_moves + 2, max_moves + 1]
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!