给定三个整数X , Y和Z ,任务是找到在mod 10 ^ 9 + 7下具有4个至多X次, 5个至多Y次和6个至多Z次形成的所有数字的总和。
例子:
Input: X = 1, Y = 1, Z = 1
Output: 3675
Explanation:
4 + 5 + 6 + 45 + 54 + 56
+ 65 + 46 + 64 + 456 + 465
+ 546 + 564 + 645 + 654 = 3675
Input: X = 4, Y = 5, Z = 6
Output: 129422134
方法:
- 由于该问题具有子问题重叠和最优子结构的性质,因此可以使用动态编程来解决。
- 对于所有i
都需要具有精确i 4s , j 5s和k 6s的数字,以获得所需的总和。 - 因此,dp数组精确数[i] [j] [k]将存储具有精确i 4s , j 5s和k 6s的数字的精确计数。
- 如果精确数[i – 1] [j] [k] ,精确数[i] [j – 1] [k]和精确数[i] [j] [k –1]是已知的,则可以观察到这些总和是必需的答案,除非是exactnum [i – 1] [j] [k] , exactnum [i] [j – 1] [k]或exactnum [i] [j] [k – 1] ]不存在。在这种情况下,只需跳过它。
- precisionsum [i] [j] [k]以与i相同的方式存储具有i 4, j 5和k 6的精确数之和。
exactsum[i][j][k] = 10 * (exactsum[i - 1][j][k] + exactsum[i][j - 1][k] + exactsum[i][j][k - 1]) + 4 * exactnum[i - 1][j][k] + 5 * exactnum[i][j - 1][k] + 6 * exactnum[i][j][k - 1]
下面是上述方法的实现:
C++
// C++ program to find sum of all numbers
// formed having 4 atmost X times, 5 atmost
// Y times and 6 atmost Z times
#include
using namespace std;
const int N = 101;
const int mod = 1e9 + 7;
// exactsum[i][j][k] stores the sum of
// all the numbers having exact
// i 4's, j 5's and k 6's
int exactsum[N][N][N];
// exactnum[i][j][k] stores numbers
// of numbers having exact
// i 4's, j 5's and k 6's
int exactnum[N][N][N];
// Utility function to calculate the
// sum for x 4's, y 5's and z 6's
int getSum(int x, int y, int z)
{
int ans = 0;
exactnum[0][0][0] = 1;
for (int i = 0; i <= x; ++i) {
for (int j = 0; j <= y; ++j) {
for (int k = 0; k <= z; ++k) {
// Computing exactsum[i][j][k]
// as explained above
if (i > 0) {
exactsum[i][j][k]
+= (exactsum[i - 1][j][k] * 10
+ 4 * exactnum[i - 1][j][k])
% mod;
exactnum[i][j][k]
+= exactnum[i - 1][j][k] % mod;
}
if (j > 0) {
exactsum[i][j][k]
+= (exactsum[i][j - 1][k] * 10
+ 5 * exactnum[i][j - 1][k])
% mod;
exactnum[i][j][k]
+= exactnum[i][j - 1][k] % mod;
}
if (k > 0) {
exactsum[i][j][k]
+= (exactsum[i][j][k - 1] * 10
+ 6 * exactnum[i][j][k - 1])
% mod;
exactnum[i][j][k]
+= exactnum[i][j][k - 1] % mod;
}
ans += exactsum[i][j][k] % mod;
ans %= mod;
}
}
}
return ans;
}
// Driver code
int main()
{
int x = 1, y = 1, z = 1;
cout << (getSum(x, y, z) % mod);
return 0;
}
Java
// Java program to find sum of all numbers
// formed having 4 atmost X times, 5 atmost
// Y times and 6 atmost Z times
class GFG
{
static int N = 101;
static int mod = (int)1e9 + 7;
// exactsum[i][j][k] stores the sum of
// all the numbers having exact
// i 4's, j 5's and k 6's
static int exactsum[][][] = new int[N][N][N];
// exactnum[i][j][k] stores numbers
// of numbers having exact
// i 4's, j 5's and k 6's
static int exactnum[][][] = new int[N][N][N];
// Utility function to calculate the
// sum for x 4's, y 5's and z 6's
static int getSum(int x, int y, int z)
{
int ans = 0;
exactnum[0][0][0] = 1;
for (int i = 0; i <= x; ++i)
{
for (int j = 0; j <= y; ++j)
{
for (int k = 0; k <= z; ++k)
{
// Computing exactsum[i][j][k]
// as explained above
if (i > 0)
{
exactsum[i][j][k]
+= (exactsum[i - 1][j][k] * 10
+ 4 * exactnum[i - 1][j][k]) % mod;
exactnum[i][j][k]
+= exactnum[i - 1][j][k] % mod;
}
if (j > 0)
{
exactsum[i][j][k]
+= (exactsum[i][j - 1][k] * 10
+ 5 * exactnum[i][j - 1][k]) % mod;
exactnum[i][j][k]
+= exactnum[i][j - 1][k] % mod;
}
if (k > 0)
{
exactsum[i][j][k]
+= (exactsum[i][j][k - 1] * 10
+ 6 * exactnum[i][j][k - 1]) % mod;
exactnum[i][j][k]
+= exactnum[i][j][k - 1] % mod;
}
ans += exactsum[i][j][k] % mod;
ans %= mod;
}
}
}
return ans;
}
// Driver code
public static void main (String[] args)
{
int x = 1, y = 1, z = 1;
System.out.println(getSum(x, y, z) % mod);
}
}
// This code is contributed by AnkitRai01
Python3
# Python3 program to find sum of all numbers
# formed having 4 atmost X times, 5 atmost
# Y times and 6 atmost Z times
import numpy as np
N = 101;
mod = int(1e9) + 7;
# exactsum[i][j][k] stores the sum of
# all the numbers having exact
# i 4's, j 5's and k 6's
exactsum = np.zeros((N, N, N));
# exactnum[i][j][k] stores numbers
# of numbers having exact
# i 4's, j 5's and k 6's
exactnum = np.zeros((N, N, N));
# Utility function to calculate the
# sum for x 4's, y 5's and z 6's
def getSum(x, y, z) :
ans = 0;
exactnum[0][0][0] = 1;
for i in range(x + 1) :
for j in range(y + 1) :
for k in range(z + 1) :
# Computing exactsum[i][j][k]
# as explained above
if (i > 0) :
exactsum[i][j][k] += (exactsum[i - 1][j][k] * 10 +
4 * exactnum[i - 1][j][k]) % mod;
exactnum[i][j][k] += exactnum[i - 1][j][k] % mod;
if (j > 0) :
exactsum[i][j][k] += (exactsum[i][j - 1][k] * 10+
5 * exactnum[i][j - 1][k]) % mod;
exactnum[i][j][k] += exactnum[i][j - 1][k] % mod;
if (k > 0) :
exactsum[i][j][k] += (exactsum[i][j][k - 1] * 10
+ 6 * exactnum[i][j][k - 1]) % mod;
exactnum[i][j][k] += exactnum[i][j][k - 1] % mod;
ans += exactsum[i][j][k] % mod;
ans %= mod;
return ans;
# Driver code
if __name__ == "__main__" :
x = 1; y = 1; z = 1;
print((getSum(x, y, z) % mod));
# This code is contributed by AnkitRai01
C#
// C# program to find sum of all numbers
// formed having 4 atmost X times, 5 atmost
// Y times and 6 atmost Z times
using System;
class GFG
{
static int N = 101;
static int mod = (int)1e9 + 7;
// exactsum[i][j][k] stores the sum of
// all the numbers having exact
// i 4's, j 5's and k 6's
static int [,,]exactsum = new int[N, N, N];
// exactnum[i][j][k] stores numbers
// of numbers having exact
// i 4's, j 5's and k 6's
static int [,,]exactnum= new int[N, N, N];
// Utility function to calculate the
// sum for x 4's, y 5's and z 6's
static int getSum(int x, int y, int z)
{
int ans = 0;
exactnum[0, 0, 0] = 1;
for (int i = 0; i <= x; ++i)
{
for (int j = 0; j <= y; ++j)
{
for (int k = 0; k <= z; ++k)
{
// Computing exactsum[i, j, k]
// as explained above
if (i > 0)
{
exactsum[i, j, k]
+= (exactsum[i - 1, j, k] * 10
+ 4 * exactnum[i - 1, j, k]) % mod;
exactnum[i, j, k]
+= exactnum[i - 1, j, k] % mod;
}
if (j > 0)
{
exactsum[i, j, k]
+= (exactsum[i, j - 1, k] * 10
+ 5 * exactnum[i, j - 1, k]) % mod;
exactnum[i, j, k]
+= exactnum[i, j - 1, k] % mod;
}
if (k > 0)
{
exactsum[i, j, k]
+= (exactsum[i, j, k - 1] * 10
+ 6 * exactnum[i, j, k - 1]) % mod;
exactnum[i, j, k]
+= exactnum[i, j, k - 1] % mod;
}
ans += exactsum[i, j, k] % mod;
ans %= mod;
}
}
}
return ans;
}
// Driver code
public static void Main ()
{
int x = 1, y = 1, z = 1;
Console.WriteLine(getSum(x, y, z) % mod);
}
}
// This code is contributed by AnkitRai01
输出:
3675