Skip to content

Instantly share code, notes, and snippets.

@hoke-t
Created May 22, 2016 19:26
Show Gist options
  • Save hoke-t/ce6421af1f0860a8cdca91dc1b3e5ba9 to your computer and use it in GitHub Desktop.
Save hoke-t/ce6421af1f0860a8cdca91dc1b3e5ba9 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.IOException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Scanner;
public class BigBadSequence {
public static void main(String[] args) throws IOException {
ArrayList<BigInteger> input = new ArrayList<>();
Scanner file = new Scanner(new File("bigbad.in"));
file.nextLine();
while (file.hasNextBigInteger()) {
input.add(file.nextBigInteger());
}
int bestLength = 0;
int bestStartIndex = 0;
System.out.println(nCrIsEven(new BigInteger("10"), new BigInteger("2")));
for (int sequenceLength = 4000; sequenceLength < 9000; sequenceLength++) {
System.out.println("Starting sequence length " + sequenceLength + ".");
boolean sequenceFound = false;
for (int currentStartIndex = 0; currentStartIndex <= 10000-sequenceLength && !sequenceFound; currentStartIndex++) {
for (int index = 0; index < sequenceLength; index++) {
BigInteger n = input.get(currentStartIndex+index);
if (!nCrIsEven(n, new BigInteger(sequenceLength+""))) {
currentStartIndex = currentStartIndex+index;
break;
}
if (index == sequenceLength-1) {
if (sequenceLength > bestLength) {
bestLength = sequenceLength;
bestStartIndex = currentStartIndex;
sequenceFound = true;
System.out.println(bestLength + " " + bestStartIndex);
}
}
}
}
}
System.out.println(bestLength + " " + bestStartIndex);
}
public static boolean nCrIsEven(BigInteger n, BigInteger r) {
return !r.and(n.subtract(r)).equals(BigInteger.ZERO);
}
}
@hoke-t
Copy link
Author

hoke-t commented Jul 7, 2016

My solution to HSCTF 3's Big Bad Sequence problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment