给定长度为N 的字符串str 。任务是找到通过替换“?”获得的整数个数与任何数字,使得形成的整数在除以13时给出余数5 。
数字也可以从零开始。答案可能非常大,因此,输出答案模10 9 + 7 。
例子:
Input: str = “?44”
Output: 1
Only possible number is 044
Input: str = “7?4”
Output: 0
Input: str = “8?3?4233?4?”
Output: 770
方法:让dp[i][j]是创建与给定模式的前i位数字一致且与j模13一致的i位数字的方法数。作为我们的基本情况, dp[0][i]=0对于从1到12 的i ,并且dp[0][0]=1 (因为我们的长度为零的数字具有零值,因此是零模 13 。)
请注意,在j mod 13的数字末尾附加一个数字k会给出一个与10j+k mod 13全等的数字。我们使用这个事实来执行我们的转换。对于每个状态dp[i][j]且i < N ,迭代k的可能值。 (如果s[i]=’?’ ,则k有十种选择,否则只有一种选择。)然后,我们将dp[i][j] 添加到dp[i+1][( 10j+k)%13]。
为了得到我们的最终答案,我们可以简单地打印dp[N][5] 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define MOD (int)(1e9 + 7)
// Function to find the count of integers
// obtained by replacing '?' in a given
// string such that formed integer
// gives remainder 5 when it is divided by 13
int modulo_13(string s, int n)
{
long long dp[n + 1][13] = { { 0 } };
// Initialise
dp[0][0] = 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 10; j++) {
int nxt = s[i] - '0';
// Place digit j at ? position
if (s[i] == '?')
nxt = j;
// Get the remainder
for (int k = 0; k < 13; k++) {
int rem = (10 * k + nxt) % 13;
dp[i + 1][rem] += dp[i][k];
dp[i + 1][rem] %= MOD;
}
if (s[i] != '?')
break;
}
}
// Return the required answer
return (int)dp[n][5];
}
// Driver code
int main()
{
string s = "?44";
int n = s.size();
cout << modulo_13(s, n);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
static int MOD = (int)(1e9 + 7);
// Function to find the count of integers
// obtained by replacing '?' in a given
// string such that formed integer
// gives remainder 5 when it is divided by 13
static int modulo_13(String s, int n)
{
long [][]dp = new long[n + 1][13];
// Initialise
dp[0][0] = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 10; j++)
{
int nxt = s.charAt(i) - '0';
// Place digit j at ? position
if (s.charAt(i) == '?')
nxt = j;
// Get the remainder
for (int k = 0; k < 13; k++)
{
int rem = (10 * k + nxt) % 13;
dp[i + 1][rem] += dp[i][k];
dp[i + 1][rem] %= MOD;
}
if (s.charAt(i) != '?')
break;
}
}
// Return the required answer
return (int)dp[n][5];
}
// Driver code
public static void main(String []args)
{
String s = "?44";
int n = s.length();
System.out.println(modulo_13(s, n));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python3 implementation of the approach
import numpy as np
MOD = (int)(1e9 + 7)
# Function to find the count of integers
# obtained by replacing '?' in a given
# string such that formed integer
# gives remainder 5 when it is divided by 13
def modulo_13(s, n) :
dp = np.zeros((n + 1, 13));
# Initialise
dp[0][0] = 1;
for i in range(n) :
for j in range(10) :
nxt = ord(s[i]) - ord('0');
# Place digit j at ? position
if (s[i] == '?') :
nxt = j;
# Get the remainder
for k in range(13) :
rem = (10 * k + nxt) % 13;
dp[i + 1][rem] += dp[i][k];
dp[i + 1][rem] %= MOD;
if (s[i] != '?') :
break;
# Return the required answer
return int(dp[n][5]);
# Driver code
if __name__ == "__main__" :
s = "?44";
n = len(s);
print(modulo_13(s, n));
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
static int MOD = (int)(1e9 + 7);
// Function to find the count of integers
// obtained by replacing '?' in a given
// string such that formed integer
// gives remainder 5 when it is divided by 13
static int modulo_13(String s, int n)
{
long [,]dp = new long[n + 1, 13];
// Initialise
dp[0, 0] = 1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < 10; j++)
{
int nxt = s[i] - '0';
// Place digit j at ? position
if (s[i] == '?')
nxt = j;
// Get the remainder
for (int k = 0; k < 13; k++)
{
int rem = (10 * k + nxt) % 13;
dp[i + 1, rem] += dp[i, k];
dp[i + 1, rem] %= MOD;
}
if (s[i] != '?')
break;
}
}
// Return the required answer
return (int)dp[n,5];
}
// Driver code
public static void Main(String []args)
{
String s = "?44";
int n = s.Length;
Console.WriteLine(modulo_13(s, n));
}
}
// This code is contributed by 29AjayKumar
Javascript
1
时间复杂度: O(100 * N)
辅助空间: O(100 * N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。