Count ways to make the number formed by K concatenations of a numeric string divisible by 5
Last Updated :
19 Jul, 2022
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 109 + 7.
Examples:
Input: S = 1256, K = 1
Output: 4
Explanation:
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 = 2
Output: 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) / (2N – 1)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MOD = 1000000007;
int exp_mod(LL a, LL b)
{
LL ret = 1;
for (; b; b >>= 1, a = a * a % MOD) {
if (b & 1)
ret = ret * a % MOD;
}
return ret;
}
int countOfWays(string s, int k)
{
int N = s.size();
LL ans = 0;
for ( int i = 0; i < N; i++) {
if (s[i] == '5' || s[i] == '0' ) {
ans = (ans + exp_mod(2, i)) % MOD;
}
}
LL q = exp_mod(2, N);
LL qk = exp_mod(q, k);
LL inv = exp_mod(q - 1, MOD - 2);
ans = ans * (qk - 1) % MOD;
ans = ans * inv % MOD;
return ans;
}
int main()
{
string S = "1256" ;
int K = 1;
cout << countOfWays(S, K);
return 0;
}
|
Java
class GFG
{
static long MOD = 1000000007 ;
public static long exp_mod( long a, long b) {
long ret = 1 ;
for (; b > 0 ; b >>= 1 , a = a * a % MOD) {
if ((b & 1 ) > 0 )
ret = ret * a % MOD;
}
return ret;
}
public static long countOfWays(String s, int k) {
int N = s.length();
long ans = 0 ;
for ( int i = 0 ; i < N; i++) {
if (s.charAt(i) == '5' || s.charAt( 0 ) == '0' ) {
ans = (ans + exp_mod( 2 , i)) % MOD;
}
}
long q = exp_mod( 2 , N);
long qk = exp_mod(q, k);
long inv = exp_mod(q - 1 , MOD - 2 );
ans = ans * (qk - 1 ) % MOD;
ans = ans * inv % MOD;
return ans;
}
public static void main(String args[]) {
String S = "1256" ;
int K = 1 ;
System.out.println(countOfWays(S, K));
}
}
|
Python3
MOD = 1000000007
def exp_mod(a, b):
ret = 1
while (b):
if (b & 1 ):
ret = ret * a % MOD
b >> = 1
a = a * a % MOD
return ret
def countOfWays(s, k):
N = len (s)
ans = 0
for i in range (N):
if (s[i] = = '5' or s[i] = = '0' ):
ans = (ans + exp_mod( 2 , i)) % MOD
q = exp_mod( 2 , N)
qk = exp_mod(q, k)
inv = exp_mod(q - 1 , MOD - 2 )
ans = ans * (qk - 1 ) % MOD
ans = ans * inv % MOD
return ans
S = "1256"
K = 1
print (countOfWays(S, K))
|
C#
using System;
public class GFG
{
static long MOD = 1000000007;
public static long exp_mod( long a, long b) {
long ret = 1;
for (; b > 0; b >>= 1, a = a * a % MOD) {
if ((b & 1) > 0)
ret = ret * a % MOD;
}
return ret;
}
public static long countOfWays(String s, int k) {
int N = s.Length;
long ans = 0;
for ( int i = 0; i < N; i++) {
if (s[i] == '5' || s[0] == '0' ) {
ans = (ans + exp_mod(2, i)) % MOD;
}
}
long q = exp_mod(2, N);
long qk = exp_mod(q, k);
long inv = exp_mod(q - 1, MOD - 2);
ans = ans * (qk - 1) % MOD;
ans = ans * inv % MOD;
return ans;
}
public static void Main(String []args) {
String S = "1256" ;
int K = 1;
Console.WriteLine(countOfWays(S, K));
}
}
|
Javascript
let MOD = 1000000007;
function exp_mod(a, b)
{
a = BigInt(a);
b = BigInt(b);
let ret = 1n;
for (; b; b >>= 1n, a = a * a % BigInt(MOD)) {
if (b & 1n)
ret = ret * a % BigInt(MOD);
}
return parseInt(ret);
}
function countOfWays(s, k)
{
let N = s.length;
let ans = 0;
for ( var i = 0; i < N; i++) {
if (s[i] == '5' || s[i] == '0' ) {
ans += (exp_mod(2, i)) % MOD;
ans %= MOD;
}
}
let q = exp_mod(2, N);
let qk = exp_mod(q, k);
let inv = exp_mod(q - 1, MOD - 2);
ans = (ans * (qk - 1)) % MOD;
ans = (ans * inv) % MOD;
return ans;
}
let S = "1256" ;
let K = 1;
console.log(countOfWays(S, K));
|
Time Complexity: O(N*log K)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...