USACO Silver 2016 January - Subsequences Summing to Sevens

Authors: Kevin Sheng, thetazero, Melody Yu

Official Analysis (Java)

Video Solution

Note: The video solution might not be the same as other solutions. Code in C++.

Implementation

Time Complexity: O(N)\mathcal{O}(N)

C++

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const int MOD = 7;
int main() {
freopen("div7.in", "r", stdin);

Java

import java.io.*;
import java.util.*;
public final class Div7 {
private static final int MOD = 7;
public static void main(String[] args) throws IOException {
long start = System.currentTimeMillis();
BufferedReader read = new BufferedReader(new FileReader("div7.in"));
int cowNum = Integer.parseInt(read.readLine());

Python

MOD = 7
with open("div7.in") as read:
cows = [int(read.readline()) for _ in range(int(read.readline()))]
best_photo = 0
# first_occ[i] stores the index of the first time a prefix sum % 7 == i
first_occ = [-1 for _ in range(MOD)]
first_occ[0] = 0

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!