Created
May 22, 2016 19:26
-
-
Save hoke-t/ce6421af1f0860a8cdca91dc1b3e5ba9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My solution to HSCTF 3's Big Bad Sequence problem.