From 35235d8f17b82476fe22a3a1b737575c95ac16f5 Mon Sep 17 00:00:00 2001 From: geegatomar <64457308+geegatomar@users.noreply.github.com> Date: Sun, 3 Jan 2021 21:10:02 +0530 Subject: [PATCH 1/2] Adding solution to S - Digit Sum --- S-Digit_Sum.cpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 S-Digit_Sum.cpp diff --git a/S-Digit_Sum.cpp b/S-Digit_Sum.cpp new file mode 100644 index 0000000..f29b05a --- /dev/null +++ b/S-Digit_Sum.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; +#define int long long +const int M = 1e9 + 7; +const int N = (int)1e4; + +// Concept used: Digit DP + +int dp[N][2][100]; + + +int f(int ind, int is_tight, int sum_of_dig, string &k, int &d) +{ + + sum_of_dig %= d; + + if(ind >= k.size()) + return ((sum_of_dig % d == 0) ? 1 : 0); + + if(dp[ind][is_tight][sum_of_dig] != -1) + return dp[ind][is_tight][sum_of_dig]; + + int ans = 0; + int curr_dig = k[ind] - '0'; + + if(is_tight) + { + for(int i = 0; i <= curr_dig; i++) + { + if(i == curr_dig) + ans += f(ind + 1, 1, i + sum_of_dig, k, d); + else + ans += f(ind + 1, 0, i + sum_of_dig, k, d); + ans %= M; + } + } + else + { + for(int i = 0; i <= 9; i++) + { + ans += f(ind + 1, 0, i + sum_of_dig, k, d); + ans %= M; + } + } + return dp[ind][is_tight][sum_of_dig] = ans % M; +} + + +int32_t main() +{ + string k; + int d; + cin >> k >> d; + memset(dp, -1, sizeof(dp)); + + cout << (f(0, 1, 0, k, d) - 1 + M) % M; + // subtracting 1 because '0' would have been counted as an answer too +} From 9f368bff3a979ebeaefb97e71a9fc4738c6782a1 Mon Sep 17 00:00:00 2001 From: geegatomar <64457308+geegatomar@users.noreply.github.com> Date: Sun, 3 Jan 2021 21:21:07 +0530 Subject: [PATCH 2/2] Adding solution J - Sushi --- J-Sushi.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 J-Sushi.cpp diff --git a/J-Sushi.cpp b/J-Sushi.cpp new file mode 100644 index 0000000..da26f38 --- /dev/null +++ b/J-Sushi.cpp @@ -0,0 +1,44 @@ +#include +using namespace std; +#define ll long long int +const int M = 1e9 + 7; +int n; +double dp[301][301][301]; + + +// Concept used: Expected Values and Probability + + +double f(int x, int y, int z) +{ + if(x < 0 || y < 0 || z < 0) + return 0; + if(x == 0 && y == 0 && z == 0) + return 0; + if(dp[x][y][z] > 0) + return dp[x][y][z]; + + double p0 = (n - (x + y + z))/(1.0 *n); + double p1 = x / (1.0 * n); + double p2 = y / (1.0 * n); + double p3 = z / (1.0 * n); + + return dp[x][y][z] = (n + x*f(x-1, y, z) + y*f(x+1, y-1, z) + z*f(x, y+1, z-1))/(x+y+z); +} + +int main() +{ + + int i, m; + + cin >> n; + int ones = 0, twos = 0, threes = 0; + for(int i = 0; i < n; i++) + { + cin >> m; + if(m == 1) ones++; + else if(m == 2) twos++; + else threes++; + } + cout << setprecision(10) << f(ones, twos, threes); +}