Lares - Them divide c people into t groups - c++

There is m men, and n women.The boss chooses k people. Them divide m+n-k remain people into t groups, each group exactly 2 men and 1 woman. Find max(t)
For example:
Input
264936043 821529140 438045170
Ouput
132468021
#include <iostream>
using namespace std;
int n, m, k, res, t;
int main(){
cin >> n >> m >> k;
for (int i = max(k-m, 0); i <= n; i++){
res = max(res, min((n-i)/2, m-k+i));
}
cout << res;
}
My code is TLE.
I think the number of men chosen is (n+2k-2m)/3 because (n-i) ~ 2*(m-k+i), but it is not correct.

Thank you #dratenik, I used derivative to analyze the function (n-x)/(m-k-x). And this is my accepted code.
#include <iostream>
using namespace std;
int n, m, k;
int main(){
cin >> n >> m >> k;
if (k >= m+n) cout << 0;
else if (n/2 <= m-k) cout << n/2;
else if ((n-k)/2 >= m) cout << m;
else {
int t = (n + 2*k - 2*m)/3;
cout << max(min((n-t)/2, m-k+t), min((n-t-1)/2, m-k+t+1));
}
}

Related

Sum of digits in C++

program asked is sum of digits :
Input data are in the following format:
first line contains N - the number of values to process;
and then N lines will follow describing the values for which sum of digits should be calculated by 3 integers A B C;
for each case you need to multiply A by B and add C (i.e. A * B + C) - then calculate sum of digits of the result.
Answer should have N results, also separated by spaces
MY CODE IN C++ :
#include <iostream>
using namespace std;
int main ()
{
int n, a, b, c, t, sum = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a >> b >> c;
t = a * b + c;
while (t % 10 != 0)
{
sum = sum + t % 10;
t = t / 10;
}
while (t % 10 == 0)
{
sum = sum;
t = t / 10;
}
}
cout << " ";
cout << sum;
cout << " ";
return 0;
}
I'm having hard time correcting my code.
Any help is appreciated.
My assumption is there should be a better way to code this other than using 2 while loops.
PS : I checked other topics just want somebody that could help with my code thank you.
You don't need second while loop, and first one should be corrected to while (t != 0). After that your program for computing sum works correctly.
Try it online!
#include <iostream>
using namespace std;
int main ()
{
int n, a, b, c, t, sum = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a >> b >> c;
t = a * b + c;
while (t != 0)
{
sum = sum + t % 10;
t = t / 10;
}
}
cout << " ";
cout << sum;
cout << " ";
return 0;
}
Input:
1
123 456 789
Output:
33
Just noticed that you need N separate outputs instead of single sum (like you did), so then your program becomes like this:
Try it online!
#include <iostream>
using namespace std;
int main ()
{
int n, a, b, c, t, sum = 0;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> a >> b >> c;
t = a * b + c;
sum = 0;
while (t != 0)
{
sum = sum + t % 10;
t = t / 10;
}
cout << sum << " ";
}
return 0;
}
Input:
2
123 456 789
321 654 987
Output:
33 15

C++ code Runtime error on 2D array declaration

I dont understand why is this code giving runtime error on input of any test case (size of string is n, and then string is input). Not even the word "CHECK" (in the solve() function) gets printed.. Please help!
The problem link is : problem
#include <bits/stdc++.h>
using namespace std;
int a[1000][26];
int mov(int l, int u, int x)
{
if(l==u)
{
if(a[l][x]-a[l-1][x]==1)
return 1;
else
return 0;
}
int m=(l+u)/2;
return max(mov(l,m,x+1)+a[u][x]-a[m][x],mov(m,u,x+1)+a[m][x]-a[l-1][x]);
}
void solve()
{
int i,k,j,n;
string str;
cin >> n >> str;
cout << "CHECK";// This is not getting printed
for(i=0;i<26;i++)
a[0][i]=0;
for(i=1;i<n+1;i++)
{
for(j=0;j<26;j++)
{
a[i][j]=a[i-1][j];
if(str[i-1]==j+'a')
a[i][j]++;
}
}
k=mov(1,n,0);
cout << n-k << "\n";
}
int main()
{
int t=1;
cin >> t;
while(t--)
solve();
}
Your very first input shows how many input "pairs" will be given. In your case, you have skipped this part and gone into gathering the input "pairs". This is why "CHECK" wont get printed. To resolve this, have a cin to capture the number of input pairs, then iterate through the inputs to get the pair.
void solve()
{
int i, k, j, n;
string str;
memset(a[0], 0, sizeof(int) * 26); // Use this instead.
cin >> n;
cout << "CHECK";
for (i = 1; i < n + 1; i++)
{
cin >> j >> str;
for (j = 0; j < 26; j++)
{
a[i][j] = a[i - 1][j];
if (str[i - 1] == j + 'a')
a[i][j]++;
}
}
k = mov(1, n, 0);
cout << k << "\n";
}
Also, try not to use using namespace std;.

Binomial coefficient programm outputs wrong results, if inputs are large(C++)

I have created a programm, that calculates binomial coefficient:
#include <iostream>
#include <string>
using namespace std;
int factorial(int num){
int res = 1;
for(int i = 1;i <= num; ++i){
res *= i;
}
return res;
}
int binom(int n,int k){
int factorial(int num);
int binom_coef;
binom_coef = factorial(n)/(factorial(k)*factorial(n-k));
return binom_coef;
}
int main(){
int n,k;
int binom(int n,int k);
try{
cout << "Enter number n:";
cin >> n;
cout << "Enter number k:";
cin >> k;
if(n < k){
throw -1;
}
cout << binom(n,k) << "\n";
}catch(int x)
cout << "n must be biggger than k";
}
For small inputs(up to 10) everything works perfectly fine. But if I enter big enough n and k(more than 10) the calculations are totally wrong and I cannot figure out why is that.
Can you help me?
The factorial is a function that grows really fast. It may be that for n > 10 your factorials already overflow the int type.
In fact, 12! = 479001600
You’re applying the theoretical formula for the binomial coefficient, but if you try the operative definition you may have less problems:
10 : 2 = 10! / (2! * 8!) = (10 * 9) / (2 * 1)
So what you can do is: multiply all numbers in [n, n - k), then divide them for all the numbers in [k, 1).
int bin_coeff(int n, int k) {
int res = 1, i = n;
while (i > n - k) res *= i--;
while (i > 0) res /= i--;
return res;
}
As you can see, you’ll calculate 10 : 2 by simply doing 90 / 2, which are not big numbers, and you’ll even get a better efficiency for your algorithm.

C++: implementing Modular Exponentiation

I am using this New and improved code I corrected in order to solve this question I have.
I am using modular Exponentiation to use the formula [a^k mod n] to get my answer for an assignment I had to do where I was required to code it in two steps.
First int k must be converted to a binary
representation K consisting of a list of 0s and 1s. Second, Modular Exponentiation must be performed
using a, n and K[] as arguments..
Earlier My code was incorrect and was able to correct it.
The Problem I now face is that when I google the online calculator for modular Exponentiation of 5^3 % 13, it should == 8
The result that I get from my code is 5.
I am trying to understand if there something minor I'm missing from the code or my math is wrong? Thanks
#include <iostream>
#include <vector>
using namespace std;
vector <int> BinaryK(int k);
int ModularExpo(int a, vector <int> & k, int n);
int main()
{
int a = 0;
int k = 0;
int n = 0;
cout << "a^k % n" << endl;
cout << "a = ";
cin >> a;
cout << "k = ";
cin >> k;
cout << "n = ";
cin >> n;
vector<int> B = BinaryK(k);
int result = ModularExpo(a, B, n);
cout << "a ^ k mod n == " << result << endl;
return 0;
}
// c == b^e % m
vector<int> BinaryK(int k)
{
vector<int> K; //hint: make K a vector
int tmp = k;
while (tmp > 0)
{
K.push_back(tmp % 2); //hint: use pushback
tmp = tmp / 2;
}
return K;
}
int ModularExpo(int a, vector<int> & K, int n)
{
if (n == 1)
return 0;
int b = 1;
if (K.size() == 0)
return b;
int A = a;
if (K[0] == 1)
b = a;
for (int i = 1; i < K.size() - 1; i++)
{
A = A * A % n;
if (K[i] == 1)
b = A*b % n;
}
return (b);
}
Change this one line:
for (int i = 1; i < K.size(); i++) // K.size() not K.size()-1

C++ line.exe has stopped working

I make some program in C++ which in matrix finds longest horizontal line of 0s.
In first line I input n and m (rows and columns in matrix array a), and later array. Main problem is when I insert first line (all correct with it) the program stops with error -1073741510, line.exe has stopped working.
#include <iostream>
using namespace std;
int main()
{
int n, m;
cin >> n, m;
int a[n][m];
int i,j,k;
for (i=0;i<n;i=i+1){
for(j=0;j<m;j=j+1){
int temp;
cin >> temp;
a[i][j] = temp;
}
}
int max;
for (i=0;i<n;i++){
for(j=0;j<m;j++){
if(a[i][j]==0){
for(k=j+1;k<m;k++){
if(a[i][k]==0){
max++;
}else{break;}
}
}
}
}
cout << max;
return 0;
}
Sorry for big amount of for loops, I don't know better way of solving problem.
To read both numbers n and m use:
cin >> n >> m;
Otherwise, as you have cin >> n, m; it is equivalent with
cin >> n;
m; // This has no effect
For a solution to the correct operation of your algorithm, try this:
int maxZeros = 0;
int lineContainingMaxZeros = 0;
for (i = 0; i < n; i++) {
int countZero = 0;
for (j = 0; j < m; j++) {
if (a[i][j] == 0)
countZero++;
}
if (countZero > maxZeros) {
maxZeros = countZero;
lineContainingMaxZeros = i;
}
}
cout << "Line: " << lineContainingMaxZeros << " containing " << maxZeros << " zeros";