Skip to content

Adding solution to J and S #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions J-Sushi.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include<bits/stdc++.h>
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);
}
58 changes: 58 additions & 0 deletions S-Digit_Sum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include<bits/stdc++.h>
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
}