Codechef Starters 46 Division 4





Counting Words

 

Harsh was recently gifted a book consisting of N pages. Each page contains exactly M words printed on it. As he was bored, he decided to count the number of words in the book.

Help Harsh find the total number of words in the book.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of two space-separated integers on a single line, N and M — the number of pages and the number of words on each page, respectively.

Output Format

For each test case, output on a new line, the total number of words in the book.

Constraints

  • 1T100
  • 1N100
  • 1M100

Sample Input 1 

4
1 1
4 2
2 4
95 42

Sample Output 1 

1
8
8
3990

Explanation

Test case 1: The book consists of only 1 page, and each page has only 1 word. Hence, the total number of words is 1.

Test case 2: The book consists of 4 pages, and each page has 2 words. Hence, the total number of words is 2+2+2+2=8.

Test case 3: The book consists of 2 pages, and each page has 4 words. Hence, the total number of words is 4+4=8.

Test case 4: The book consists of 95 pages, and each page has 42 words. Hence, the total number of words is 3990.


Solution : 

#include <iostream>

using namespace std;

int main() {

int t,a,b;

cin>>t;

while(t--){

    cin>>a>>b;

    cout<<a*b<<endl;

}

return 0;

}






For the upcoming semester, the admins of your university decided to keep a total of X seats for the MATH-1 course. A student interest survey was conducted by the admins and it was found that Y students were interested in taking up the MATH-1 course.

Find the minimum number of extra seats that the admins need to add into the MATH-1 course to make sure that every student who is interested in taking the course would be able to do so.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of two-space separated integers on a single line, X and Y — the current number of seats up for enrolment and the number of students interested in taking up the course in the upcoming semester, respectively.

Output Format

For each test case, output on a new line the minimum number of seats required to be added.

Constraints

  • 1T100
  • 1X,Y105

Sample Input 1 

4
1 1
12 34
50 49
49 50

Sample Output 1 

0
22
0
1

Explanation

Test case 1: Exactly 1 seat is available for enrolment, and exactly 1 student is interested in taking up the course, hence addition of more seats is not required.

Test case 2: 12 seats are available for enrolment but 34 students are interested in taking up the course, hence the admins would have to add 3412=22 more seats to make sure that every student interested in the course gets a seat.

Test case 3: 50 seats are available for enrolment and 49 students are interested in taking up the course, hence addition of more seats is not required.

Test case 4: 49 seats are available for enrolment, but 50 students are interested in taking up the course, hence the admins would have to add 5049=1 more seat to make sure that every student interested in the course gets a seat.


Solution : 

#include <iostream>


using namespace std;


int main() {


int t,a,b;


cin>>t;


while(t--){


    cin>>a>>b;

        if(b>=a)

    cout<<b-a<<endl;

    else

        cout<<0<<endl;

}


return 0;


}


Group Assignment 




Chef is hosting a party at his house and N people are invited to it. Everyone has arrived and they are eager to make a group and chit-chat.

The ith person prefers to be in a group of exactly Pi people (including himself). A person who is not in a group of preferred size gets upset. Find whether Chef would be able to assign every person to a group such that everyone remains happy.

Input Format

  • The first line of input will contain a single integer T, denoting the number of test cases.
  • Each test case consists of multiple lines of input.
    • The first line of each test case contains an integer N — the number of people at the party.
    • The next line contains the array P — where Pi denotes the preferred group size for ith person.

Output Format

For each test case, output YES, if Chef can assign every person to a group such that everyone remains happy. Otherwise output NO.

The output is case-insensitive, meaning that outputs such as yESYeSYES and yes mean the same.

Constraints

  • 1T1000
  • 2N105
  • 2PiN
  • Sum of N over all test cases does not exceed 105.

Sample Input 1 

4
5
2 3 2 3 3
5
5 5 5 5 5
5
3 2 2 3 2
4
4 4 4 3

Sample Output 1 

YES
YES
NO
NO

Explanation

Test case 1:

  • Person 1 and 3 can make a group (As both want to be in a group of 2).
  • Person 24 and 5 can make a group (As they all want to be in a group of 3).

So, there is a possible arrangement that Chef can make so that all of them are happy.

Test case 2:

  • All of the 5 people can make a group (As all want to be in a group of 5).

So, there is a possible arrangement that Chef can make so that all of them are happy.

Test case 3: It can be shown that there is no possible arrangement that Chef can make so that all of them are happy.

Test case 4: It can be shown that there is no possible arrangement that Chef can make so that all of them are happy.


Solution :


#include <bits/stdc++.h>

using namespace std;


int main() {


int t;


cin>>t;


while(t--){

        int a,cnt=0;

        cin>>a;

        int b[a];

        map<int,int> m;


    for(int i=0;i<a;i++){

        cin>>b[i];

        m[b[i]]++;

    }

    string ans ="YES";

for(auto j:m){

if(j.second%j.first!=0){

ans="NO";break;}}

cout<<ans<<endl;

}


return 0;


}





Future Tamplate 


#include <bits/stdc++.h>
using namespace std;

#define frr(i,a,b) for(int i=a;i<=b;i++)
#define test(t) int t; cin >> t; frr(tno,1,t)
#define endl '\n'
#define int long long

const int N = 1000000007;
const int N2 = 998244353;
const int INF = 1000000000000000000;

signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    cout.precision(numeric_limits<double>::max_digits10);
    // freopen("input12.txt","r",stdin);
    // freopen("ans12.txt","w",stdout);
    test(t) {
        int n,x;
        cin >> x >> n;
        cout << (2*n-x)*(x-1) << endl;
    }
    return 0;
}