Carl is bored of playing with ordinary prime numbers. Thus, he comes up with some special numbers called Omega Primes.
A number X is called Omega Prime, if there exists no perfect square Y (Y > 1) such that Y divides X.
For example, 6 is an Omega Prime because there is no perfect square except 1 that divides 6.
On the other hand, 12 is not an Omega Prime as 4 (which is a perfect square) is a divisor of 12.
Carl decides to play a bit more with Omega Primes. He has an array A of integers. Carl wants to find the number of different subsets such that the product of elements for each subset, results in an Omega Prime. Help Carl find this number.
Since this number can be large, output the answer modulo 10^9 + 7.
Constraints
Input Format
The first argument is the integer array A. . Output Format
Return an integer denoting the number of different desired subsets modulo 10^9 + 7.
Input 1
A = [2, 4, 3]
Output 1
3
Input 2
A = [2, 2, 2]
Output 2
3
Given a complete rooted tree with N nodes numbered 1 to N. This tree has its leaves at the top and root at the bottom.
A complete tree is a tree on every leaf of the tree and in order to get all the fruits you have to shake the tree any number of times.
But this tree is a little different than the rest it has following properties:
The tree is rooted at 1. You may assume that root is one level above the ground so all fruits which fall from roots lands on the ground.
You have to find the minimum number of shakes you have to perform such that all the fruits are on the ground.
Constraints
Input Format
First argument is an integer array A of size N where A[i] denotes the number of fruits on ith node.
Second argument is an integer array B of size N where B[i] denotes the capacity of ith node.
Third argument is a 2D array of size (N-1) * 2 denotes edges in tree. There is an edge between nodes C[i][0] and C[i][1].
Output Format
Return an integer denoting the minimum number of shakes you have to perform such that all the fruits are on the ground.
Input 1
A = [0, 0, 0, 1, 1, 2]
B = [1, 1, 1, 1, 1, 2]
C = [
[1,2]
[1,3]
[2,5]
[2,6]
[3,4]
]
Output 1
4
Input 2
A = [0, 0, 5, 5]
B = [10, 3, 10, 10]
C = [
[1,2]
[2,3]
[2,4]
]
Output 2
9
You are the Prime Minister of a country and once you went for a world tour.
After 5 years, when you returned to your country you were shocked to see the condition of the roads between the cities. So, you plan to repair them, but you cannot afford to spend a lot of money.
The country can be represented as a N*M grid, where Country[i,j] is a city.
The cost of repairing a repairing a road between [i,j] and [i+1,j] is A[i]. The cost of repairing a road between [i,j] and [i,j+1] is B[i].
Return the minimum cost of repairing roads.
As the cost can be large, return the cost modulo 10^9+7.
Input Format
The first argument will be an integer array, A, of size N.
The second argument will be an integer array, B, of size M.
Output Format
Return an integer representing the minimum possible cost.
Constraints
Example
Input 1
A = [1, 1, 1]
B = [1, 1, 2]
Output 1
16
Input 2
A = [1, 2, 3]
B = [4, 5, 6]
Output 2
39
Question 2
*Overview:
*Approach:
i
, if the first fruit moves from node 'ito its parent at
time[i],` then every second after that until the final fruit is emptied, a fruit moves from it to its parent.i
to parent[i]
is continuous, there is no break.i
to parent[i] happens at depth[i], seconds. (this is because all leaves are the same level and have at least one fruits)ANS[i]
be the minimum time needed to empty node i
- `ans[i] = a[i]` for leaf node
sum[i]
is no of fruits that falls from its children.Depth[i] is depth of node with
depth[leaf node] = 1`sum[i] = sum(ans[child]-depth[child])
- The first fruit falls at `(depth[i] = (depth[child]+1))`, the last fruit falls at ANS[i] , and it falls continuously
- for each child no of fruits falling to the node `i` is `ans[child]-depth[child]`
maxi = max(ans[child])
ANS[i] = maxi + min(capacity[i], sum - (maxi - d[node]))
.maxi
time.sum - (maxi - d[node])
. If this greater than capacity, those just fall off, that's why min with capacity[i]
(maxi - d[node])
is no of fruits that fell to parent[i]
before last fruit reached node i
pseudocode:
vector<ll> ch[100005];
vector<bool> vis(100005, false);
vector<ll> ans(100005, 0);
vector<ll> a(100005);
vector<ll> b(100005);
vector<ll> d(100005, 0);
void dfs(ll node)
{
vis[node] = true;
ll sum = 0;
ll maxi = 0;
ll ct = 0;
for (auto child : ch[node])
{
if (!vis[child])
{
dfs(child);
if (ans[child] > maxi)
{
maxi = ans[child];
}
d[node] = max(d[node], d[child] + 1);
sum += (ans[child] - d[child]);
ct++;
}
}
if (ct == 0)
{
ans[node] = a[node];
}
else
{
ans[node] = maxi + min(b[node], sum - (maxi - d[node]));
}
}
Q1)
Assuming not considering empty subset
Dp,Dp with bitmasks
dp[prod][val]=dp[prod][val-1](Not considering val)+(prod%val==0)?dp[prod/val][val-1]*frequency[val](Considering val if prod divisible by val):0
where frequency[val] represent count of numbers with values=val in given array.dp[mask(prod)][val]=dp[mask(prod)][val-1]+(if mask(prod) could be divided by val)dp[mask(prod) XOR val][val-1]
for(int i=0;i < 32;i++)
dp[0][i]=1; // if mask is 0(i.e not a product of prime) then taking empty set
for(int mask=1;mask < (1 < < max_primes);mask++) //iterating over all masks
{
for(int value=1;value < =30;value++)
{
dp[mask][value]+=dp[mask][value-1];
dp[mask][value]%=MOD;
if(freq[value] and valid(value))//check whether value is also a omega prime or not
{
int mask_pf=mask_generate(value); //creating mask of value
//Suppose value=21(3*7) then mask_pf would be decimal of (0101000000)
//Suppose value=6(2*3) then mask_pf would be decimal of (1100000000)
if(mask_pf is submask of mask)//checking whether mask (which is just product represent in mask ) is divisible by value
{
dp[mask][value]+=(dp[mask^mask_pf][value-1]*freq[value])%MOD;
dp[mask][value]%=MOD;
}
}
}
}
Question 2
Approach
Pseudo code
int helper(vector<vector<int>> &adj, vector<int> &A, vector<int> &B, int i) {
int left=0, right=0;
int n = adj[i].size();
if(n >= 1)
left = helper(adj,A,B,adj[i][0]);
if(n == 2)
right = helper(adj,A,B,adj[i][1]);
int childtime = max(left,right);
int selftime = A[i-1] + min(left,right); // self gets extra fruits only when both children are passing fruits
if(n!=0 && A[i-1]==0) selftime += 1; // if not leaf and have 0 fruits initially, it takes 1 more time to store to get continuous passing of fruits
if(selftime > B[i-1]) selftime = B[i-1];
return childtime + selftime;
}
int main() {
vector<int> A = {0,0,5,5};
vector<int> B = {10,3,10,10};
vector<vector<int>> C = {{1,2},{2,3},{2,4}};
int n = A.size();
vector<vector<int>> adj(n+1);
for(auto &c:C) {
adj[c[0]].push_back(c[1]);
}
int ans = helper(adj,A,B,1);
court << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> a={1,1,1};
vector<int> b={1,1,2};
int n=a.size()+1,m=b.size()+1;
priority_queue<pair<int, pair<int, int>>,
vector<pair<int, pair<int, int>>>,
greater<pair<int, pair<int, int>>>> pq;
vector<vector<int>> vis(n+1,vector<int>(m+1,0));
// {wt,{i,j}}
pq.push({0,{0,0}});
int sum=0;
int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};
while(!pq.empty()){
auto it=pq.top();
pq.pop();
int cityx=it.second.first;
int cityy=it.second.second;
int wt=it.first;
if(!vis[cityx][cityy]){
vis[cityx][cityy]=1;
sum+=wt;
for(int i=0;i<4;i++){
int x=cityx+dx[i];
int y=cityy+dy[i];
if(x>=0 && x<n && y>=0 && y<m && !vis[x][y]){
if(dy[i]==0){
if(dx[i]==1){
pq.push({a[cityx],{x,y}});
}else{
pq.push({a[x],{x,y}});
}
}
if(dx[i]==0){
if(dy[i]==1){
pq.push({b[cityy],{x,y}});
}else{
pq.push({b[y],{x,y}});
}
}
}
}
}
}
cout<<sum<<endl;
return 0;
}
For the test case:
A = [0,0,0,1,1,1,1]
B = [4,2,2,1,1,1,1]
C = [ [1,2] [1,3] [2,4] [2,5] [3,6] [3,7]]
Ans should be 6, but your solution gives 7.
After Seconds: No of fruits array
1:[0,2,2,0,0,0,0]
2:[2,1,1,0,0,0,0]
3:[3,0,0,0,0,0,0]
4:[2,0,0,0,0,0,0]
5:[1,0,0,0,0,0,0]
6:[0,0,0,0,0,0,0]