📌  相关文章
📜  计数方法使由 K 连接形成的数字字符串可被 5 整除

📅  最后修改于: 2022-05-13 01:56:09.170000             🧑  作者: Mango

计数方法使由 K 连接形成的数字字符串可被 5 整除

给定一个由N个数字和一个整数K组成的字符串S ,任务是计算从连接字符串S形成的数字中删除数字的次数, K次,使得结果字符串可以被5整除.由于计数可能非常大,因此将其打印为模10 9 + 7

例子:

方法:给定的问题可以通过以下事实来解决:当且仅当它的最后一位数字是05时,该数字可以被 5 整除。如果sol[i]是形成以i结尾的可被5整除的数字的方式数,则数字计数由(sol[1] + sol[2] + … + sol[N])给出。对于每个索引i ,在 i右边,选择删除所有数字,在i左边,有两个选择,要么删除,要么取,即2 (i – 1)

因此, K个连接的总数由下式给出:

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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


输出:
4

时间复杂度: O(N*log K)
辅助空间: O(1)