Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created March 6, 2017 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/736bcdad4cb8e3ab8f5d42510fc7477c to your computer and use it in GitHub Desktop.
Save jianminchen/736bcdad4cb8e3ab8f5d42510fc7477c to your computer and use it in GitHub Desktop.
Hackerrank - lucky 8 - study code
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static int MOD = 1_000_000_007;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
String number = in.next();
int[][] count = new int[n][8];
count[0][0] = 1;
count[0][(number.charAt(0) - '0') % 8]++;
for (int i = 1; i < n; i++) {
for (int md = 0; md < 8; md++) {
count[i][md] = count[i - 1][md];
}
for (int md = 0; md < 8; md++) {
int newMd = (10 * md + (number.charAt(i) - '0')) % 8;
count[i][newMd] = (count[i][newMd] + count[i - 1][md]) % MOD;
}
}
System.out.println((count[n - 1][0] + MOD - 1) % MOD);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment