Created
March 6, 2017 22:45
-
-
Save jianminchen/736bcdad4cb8e3ab8f5d42510fc7477c to your computer and use it in GitHub Desktop.
Hackerrank - lucky 8 - study code
This file contains 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.*; | |
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