# Count ways to make the number formed by K concatenations of a numeric string divisible by 5

Given a string **S** consisting of **N** digits and an integer **K**, the task is to count the number of ways to remove digits from the number formed by the concatenating the string **S**, **K** number of times, such that the resulting string is divisible by **5**. Since count can be very large, so print it to modulo **10 ^{9} + 7**.

**Examples:**

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the **Essential Maths for CP Course** at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

Input:S = 1256, K = 1Output:4Explanation:

Following are the ways to remove the characters so that the string S(= “1256”) is divisible by 5:

- Remove the character 6 from the string S modifies the string to 125, which is divisible by 5.
- Remove the character 1, and 6 from the string S modifies the string to 25, which is divisible by 5.
- Remove the character 2, and 6 from the string S modifies the string to 15, which is divisible by 5.
- Remove the character 1, 2, and 6 from the string S modifies the string to 5, which is divisible by 5.
Therefore, the total count of numbers formed which is divisible by 5 is 4.

Input:S = 13390, K = 2Output:528

**Approach:** The given problem can be solved by the fact that the number is divisible by 5 if and only if its last digit is either **0** or **5**. If **sol[i]** is the number of ways to form the numbers divisible by **5** ending at **i**, then the count of numbers is given by **(sol[1] + sol[2] + â€¦ + sol[N])**. For each index **i**, on** **the **right** of **i**, the choice is to delete all digits and on the **left** of **i**, then there are two choices, either to delete or to take i.e., **2 ^{(i – 1)}**.

Therefore, the total count of numbers for **K** number of concatenation is given by:

ans = ans * (2

^{(K*N)}-1) / (2^{N}â€“ 1)

Below is the implementation of the above approach:

## C++

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `typedef` `long` `long` `LL;` `const` `int` `MOD = 1000000007;` `// Function to find the value of a^b` `// modulo 1000000007` `int` `exp_mod(LL a, LL b)` `{` ` ` `// Stores the resultant value a^b` ` ` `LL ret = 1;` ` ` `// Find the value of a^b` ` ` `for` `(; b; b >>= 1, a = a * a % MOD) {` ` ` `if` `(b & 1)` ` ` `ret = ret * a % MOD;` ` ` `}` ` ` `return` `ret;` `}` `// Function to count the number of ways` `// such that the formed number is divisible` `// by 5 by removing digits` `int` `countOfWays(string s, ` `int` `k)` `{` ` ` `int` `N = s.size();` ` ` `// Stores the count of ways` ` ` `LL ans = 0;` ` ` `// Find the count for string S` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// If the digit is 5 or 0` ` ` `if` `(s[i] == ` `'5'` `|| s[i] == ` `'0'` `) {` ` ` `ans = (ans + exp_mod(2, i)) % MOD;` ` ` `}` ` ` `}` ` ` `// Find the count of string for K` ` ` `// concatenation of string S` ` ` `LL q = exp_mod(2, N);` ` ` `LL qk = exp_mod(q, k);` ` ` `LL inv = exp_mod(q - 1, MOD - 2);` ` ` `// Find the total count` ` ` `ans = ans * (qk - 1) % MOD;` ` ` `ans = ans * inv % MOD;` ` ` `return` `ans;` `}` `// Driver Code` `int` `main()` `{` ` ` `string S = ` `"1256"` `;` ` ` `int` `K = 1;` ` ` `cout << countOfWays(S, K);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `class` `GFG` `{` ` ` `static` `long` `MOD = ` `1000000007` `;` ` ` `// Function to find the value of a^b` ` ` `// modulo 1000000007` ` ` `public` `static` `long` `exp_mod(` `long` `a, ` `long` `b) {` ` ` `// Stores the resultant value a^b` ` ` `long` `ret = ` `1` `;` ` ` `// Find the value of a^b` ` ` `for` `(; b > ` `0` `; b >>= ` `1` `, a = a * a % MOD) {` ` ` `if` `((b & ` `1` `) > ` `0` `)` ` ` `ret = ret * a % MOD;` ` ` `}` ` ` `return` `ret;` ` ` `}` ` ` `// Function to count the number of ways` ` ` `// such that the formed number is divisible` ` ` `// by 5 by removing digits` ` ` `public` `static` `long` `countOfWays(String s, ` `int` `k) {` ` ` `int` `N = s.length();` ` ` `// Stores the count of ways` ` ` `long` `ans = ` `0` `;` ` ` `// Find the count for string S` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) {` ` ` `// If the digit is 5 or 0` ` ` `if` `(s.charAt(i) == ` `'5'` `|| s.charAt(` `0` `) == ` `'0'` `) {` ` ` `ans = (ans + exp_mod(` `2` `, i)) % MOD;` ` ` `}` ` ` `}` ` ` `// Find the count of string for K` ` ` `// concatenation of string S` ` ` `long` `q = exp_mod(` `2` `, N);` ` ` `long` `qk = exp_mod(q, k);` ` ` `long` `inv = exp_mod(q - ` `1` `, MOD - ` `2` `);` ` ` `// Find the total count` ` ` `ans = ans * (qk - ` `1` `) % MOD;` ` ` `ans = ans * inv % MOD;` ` ` `return` `ans;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `main(String args[]) {` ` ` `String S = ` `"1256"` `;` ` ` `int` `K = ` `1` `;` ` ` `System.out.println(countOfWays(S, K));` ` ` `}` `}` `// This code is contributed by _saurabh_jaiswal.` |

## Python3

`# Python program for the above approach` `MOD ` `=` `1000000007` `# Function to find the value of a^b` `# modulo 1000000007` `def` `exp_mod(a, b):` ` ` ` ` `# Stores the resultant value a^b` ` ` `ret ` `=` `1` ` ` ` ` `# Find the value of a^b` ` ` `while` `(b):` ` ` `if` `(b & ` `1` `):` ` ` `ret ` `=` `ret ` `*` `a ` `%` `MOD` ` ` `b >>` `=` `1` ` ` `a ` `=` `a ` `*` `a ` `%` `MOD` ` ` ` ` `return` `ret` `# Function to count the number of ways` `# such that the formed number is divisible` `# by 5 by removing digits` `def` `countOfWays(s, k):` ` ` ` ` `N ` `=` `len` `(s)` ` ` ` ` `# Stores the count of ways` ` ` `ans ` `=` `0` ` ` ` ` `# Find the count for string S` ` ` `for` `i ` `in` `range` `(N):` ` ` ` ` `# If the digit is 5 or 0` ` ` `if` `(s[i] ` `=` `=` `'5'` `or` `s[i] ` `=` `=` `'0'` `):` ` ` `ans ` `=` `(ans ` `+` `exp_mod(` `2` `, i)) ` `%` `MOD` ` ` ` ` `# Find the count of string for K` ` ` `# concatenation of string S` ` ` `q ` `=` `exp_mod(` `2` `, N)` ` ` `qk ` `=` `exp_mod(q, k)` ` ` `inv ` `=` `exp_mod(q ` `-` `1` `, MOD ` `-` `2` `)` ` ` ` ` `# Find the total count` ` ` `ans ` `=` `ans ` `*` `(qk ` `-` `1` `) ` `%` `MOD` ` ` `ans ` `=` `ans ` `*` `inv ` `%` `MOD` ` ` ` ` `return` `ans` `# Driver Code` `S ` `=` `"1256"` `K ` `=` `1` `print` `(countOfWays(S, K))` `# This code is contributed by shivani` |

## C#

`// C# program for the above approach` `using` `System;` `public` `class` `GFG` `{` ` ` `static` `long` `MOD = 1000000007;` ` ` `// Function to find the value of a^b` ` ` `// modulo 1000000007` ` ` `public` `static` `long` `exp_mod(` `long` `a, ` `long` `b) {` ` ` `// Stores the resultant value a^b` ` ` `long` `ret = 1;` ` ` `// Find the value of a^b` ` ` `for` `(; b > 0; b >>= 1, a = a * a % MOD) {` ` ` `if` `((b & 1) > 0)` ` ` `ret = ret * a % MOD;` ` ` `}` ` ` `return` `ret;` ` ` `}` ` ` `// Function to count the number of ways` ` ` `// such that the formed number is divisible` ` ` `// by 5 by removing digits` ` ` `public` `static` `long` `countOfWays(String s, ` `int` `k) {` ` ` `int` `N = s.Length;` ` ` `// Stores the count of ways` ` ` `long` `ans = 0;` ` ` `// Find the count for string S` ` ` `for` `(` `int` `i = 0; i < N; i++) {` ` ` `// If the digit is 5 or 0` ` ` `if` `(s[i] == ` `'5'` `|| s[0] == ` `'0'` `) {` ` ` `ans = (ans + exp_mod(2, i)) % MOD;` ` ` `}` ` ` `}` ` ` `// Find the count of string for K` ` ` `// concatenation of string S` ` ` `long` `q = exp_mod(2, N);` ` ` `long` `qk = exp_mod(q, k);` ` ` `long` `inv = exp_mod(q - 1, MOD - 2);` ` ` `// Find the total count` ` ` `ans = ans * (qk - 1) % MOD;` ` ` `ans = ans * inv % MOD;` ` ` `return` `ans;` ` ` `}` ` ` `// Driver Code` ` ` `public` `static` `void` `Main(String []args) {` ` ` `String S = ` `"1256"` `;` ` ` `int` `K = 1;` ` ` `Console.WriteLine(countOfWays(S, K));` ` ` `}` `}` `// This code is contributed by shikhasingrajput` |

**Output:**

4

**Time Complexity:** O(N*log K)**Auxiliary Space:** O(1)