Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all 39796 articles
Browse latest View live

MGAME - EDITORIAL

$
0
0

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2

Setter:Smit mandavia
Tester:Xiuhan Wang
Editorialist:Taranpreet Singh

DIFFICULTY:

Easy

PREREQUISITES:

Modulo operator and Basic Combinatorics.

PROBLEM:

Given two integers $N$ and $P$, suppose the maximum value of $(((N \bmod i) \bmod j) \bmod k ) \bmod N$ be $M$ where $i, j, k \in [1, P]$, Find the number of ways to select $i, j, k \in [1, P]$ such that $(((N \bmod i) \bmod j) \bmod k ) \bmod N$ equals $M$.

SUPER QUICK EXPLANATION

  • The maximum value of $N \bmod x$ where $x \in [1,N]$, if $N$ is odd, is $(N-1)/2$ when $x = (N+1)/2$, and if $N$ is even, is $N/2-1$ when $x = N/2+1$.
  • We can achieve $(((N \bmod i) \bmod j) \bmod k ) \bmod N = M$ in three ways. Let $x = \lceil (N+1)/2 \rceil$
  • $i = x$ and $j,k > M$.
  • $i > N$, $j = x$ and $k > M$.
  • $i, j > N$ and $k = x$. Each of this case can be easily computed.

EXPLANATION

First of all, Let is find this value $M$. It has to be less than $min(i,j,k,N)$ which implies, $M < N$. Hence, if we want $M > 0$, we need $(((N \bmod i) \bmod j) \bmod k) < N$. So, We know for sure, that to maximize $M$, $min(i, j, k) \leq N$. Hence, we need maximum $(((N \bmod i) \bmod j) \bmod k) < N $ and now we can ignore the last $\bmod N$.

So, The maximum $N \bmod x$ can attain is $\lfloor (N-1)/2 \rfloor$. This happens when $x = \lceil (N+1)/2 \rceil$. It can be easily verified either by checking by hand, or writing a simple program ;)

Now, try finding out number of ways $(((N \bmod i) \bmod j) \bmod k)$ equals $M$. It can be approached in Simple case base analysis.

We can try all possible triplets of $(i,j,k)$ and generalize them into three cases.

  • When $i = \lceil (N+1)/2 \rceil$ and $j,k > M$
  • When $i > N$, $j = \lceil (N+1)/2 \rceil$ and $k > M$
  • When $i,j > N$ and $k = \lceil (N+1)/2 \rceil$

In all three cases, we can simply count the number of triplets $(i, j, k)$ satisfying any condition and print the answer.

Corner Case

When $N \leq 2$, $M = \lfloor (N-1)/2 \rfloor = 0$. This is because we cannot achieve $(((N \bmod i) \bmod j) \bmod k ) \bmod N > 0$. So, all triplets $(i, j, k)$ are valid.

Alternate solution - read at your own risk, you have been warned :D

For those curious enough not to be satisfied with such solutions, there also exists a pattern based solution too, using basic math. Just use brute solution to find the first terms of series and solve using the pattern formed. Number 6 is important. Enjoy :D

Time Complexity

Time complexity is $O(1)$ per test case.

AUTHOR'S AND TESTER'S SOLUTIONS:

Setter's solution
Tester's solution
Editorialist's solution

Feel free to Share your approach, If it differs. Suggestions are always welcomed. :)


Implementation of Dijkstra's Algorithm using set data structure in C++

$
0
0

I was trying to implement Dijkstra using set data structure in C++. I was coding in C++14 and there seems to be something out of order in the code for it is not giving me the right answer in Codechef IDE and on DevCpp it is not even compiling!

I believe the code is logically sound and there are only semantic errors in it.

Implementing Dijkstra using set in C++

RECTSQ, am I unable to understand?

$
0
0

https://www.codechef.com/problems/RECTSQ

My Code:

#include <stdio.h>

int ans;
int finder (int a, int b)
{
    if(b > a)
        {
            a += b;
            b = a-b;
            a = a-b;
        }

    int k = a/b;
    ans += k;
    if(a-(k*b))
        finder((a-(k*b)),b);
    else
        return 0;
}   
int main()
{
    int t;
    scanf(" %d",&t);
    while(t--)
        {
            int a, b;
            scanf(" %d%d",&a,&b);
            ans = 0;
            finder(a,b);
            printf("%d\n",ans);
        }
}

I know it is giving output as 3 even for the sample testcases given in the question, while the desired ones are 6 and 6. But I want to ask that if we have to return the minimum number of square plots then why is this incorrect? Take the first case 10, 15 for example, Square 1: 10x10 (5x10 remains from original 10x15) Square 2: 5x5 (5x5 remains from original 5x10) Square 3: 5x5 (Return to main)

So Minimum is 3 squares, why is 6 the answer? I think maybe I understood the question wrong, please help.

Thank You

KINGCON - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Jay Pandya
Tester:Hiroto Sekido
Editorialist:Anton Lunyov

DIFFICULTY:

EASY

PREREQUISITES:

Biconnected components

PROBLEM:

Getting rid of the story line the problem simply asks to find the number of articulation points in the undirected graph and multiply it by K.

EXPLANATION:

Naive approach would be for each vertex delete it from the graph and check the connectivity. Such approach has complexity O(N * M) and should get TLE. So something clever should be invented.

The problem could be solved using simple DFS in O(N + M) time. For this, some magic value low(v) is calculated for each vertex v and this allows to check whether v is articulation point in O(1) time inside DFS. The root of the DFS traversal tree should be analyzed in another way. Namely it will be an articulation point if and only if it has at least two sons in this tree.

But sincerely speaking biconnected components, articulation points and bridges is one of my unfavorite topics :)
So I prefer to forward you to some existing tutorials for further details of the mentioned approach:

  • Exercise 23-2 in famous book "Introduction to Algorithms" by Cormen and others.
  • The article on e-maxx.ru. It seems to be very well written but unfortunately it is only in Russian. But code there is working - I've checked and get AC here :) But be careful with IS_CUTPOINT(v); it could say this several times for the same vertex.
  • The article at Wikipedia. Unfortunately it contains no code snippets so it could be hard to learn this topic efficiently from there.
  • Yet another tutorial. It seems to be really good introduction to DFS and its applications with code snippets.
  • Several other tutorials: click, click, click.

Also I provide a lot of related problems on biconnected components, so don't blame me much for not writing yet another tutorial on this topic :)

AUTHOR'S AND TESTER'S SOLUTIONS:

Author's solution will be provided soon.
Tester's solution can be found here.

RELATED PROBLEMS:

UVA - 796 - Critical Links
TJU - 2840 - Apple Tree
UVA - 610 - Street Directions
UVA - 10972 - RevolC FaeLoN
PKU - 3177 - Redundant Paths
Live Archive - 4186 - Lucky cities

Previous CCDSAP exam questions

$
0
0

Where can I find all the previously conducted CCDSAP exam questions?

How to implement Pair Class in Java?

$
0
0

Is there any inbuilt library for pair class in java like in c++? If not how to we implement pair class in java and how can we do pair sort with respect to the first element and pair sort with respect to the second element in java?

DPAIRS - EDITORIAL

$
0
0

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2

Setter:Noszály Áron
Tester:Xiuhan Wang
Editorialist:Taranpreet Singh

DIFFICULTY:

Easy

PREREQUISITES:

Sorting and maps and a nice Observation.

PROBLEM:

Given two sets of distinct integers $A$ and $B$. Choose $|A|+|B|-1$ pairs $(x,y)$ such that values $A_x+B_y$ for all pairs is pairwise distinct where $|X|$ means size of set $X$.

SUPER QUICK EXPLANATION

  • Simple way to find distinct $|A|+|B|-1$ values is to sort both $|A|$ and $|B|$ in ascending order, pair smallest element of one set with all element from the second set and largest element of the second set with all but first element of the first set. This way, we get $|B|+(|A|-1) = |A|+|B|-1$ distinct values which is what we want.
  • The reason these values are distinct is that $x+y < x+z$ if $y < z$. This is happening for all consecutive pairs generated in this manner. Indices can be taken care of using maps or arrays itself.

EXPLANATION

Subtask 1: $|A|, |B| \leq 10^3$.

Here, constraints are small enough to try each pair of elements in both sets, finding pairs having distinct values and printing $|A|+|B|-1$ pairs with distinct sums.

Subtask 2: $|A|, |B| \leq 2*10^5$

Now, we cannot iterate over every pair of values. We need to be smart.

Let us sort both sets in ascending order, and pair first element of $A$ with all elements of $B$, getting $|B|$ pairs. If the first element of $A$ is $x$, Sum of these $|B|$ pairs is pairwise distinct as the set of sums of these pairs is nothing but all values of $B$ increased by $x$.

Now, suppose $y$ is the largest value present in $B$ and $z$ be second value in $A$. Since $z > x$, we have $y+z > y+x$. So, we can now pair the second element of $A$ with the last element of $B$ to get a distinct sum value.

This way, we can continue to pair all values in $A$ with the largest element of $B$ (except first, as it is already paired), getting $|A|-1$ more pairs.

Hence, we have got our required $|A|+|B|-1$ pairs. Indices can be preserved using maps, or arrays itself.

Want to solve it faster? See the box below.

View Content

Exercise:

Prove that the minimum number of pairs that can be generated having distinct sums is $|A|+|B|-1$ irrespective of the value present in set $A$ and $B$ have distinct values.

Also, Generate a large test case where this minimum value is achieved. (Hint in box)

View Content

Time Complexity

The time complexity is $(|A|*log(|A|)+|B|*log(|B|))$ due to sorting.

AUTHOR'S AND TESTER'S SOLUTIONS:

Setter's solution
Tester's solution
Editorialist's solution

Feel free to Share your approach, If it differs. Suggestions are always welcomed. :)

Invitation to CodeChef January Cook-Off 2019 sponsored By ShareChat

$
0
0

Greetings CodeChef community!

I invite you to participate in CodeChef’s January Cook-Off 2019 sponsored by ShareChat*. This short contest lets you solve 5 problems in 2.5 hours. The contest problem statements will also be available in English, Hindi, Bengali, Russian, Mandarin and Vietnamese. This contest is open to programmers of all ages and skill levels across the world.

All participants of the Cook-Off will be eligible to apply for jobs at ShareChat - India’s fastest growing social network. Visit the contest link for more details.

I hope you will join your fellow programmers and enjoy the contest problems. Joining me on the problem setting panel are:

I hope you will enjoy solving them. Please give your feedback on the problem set in the comments below, after the contest.

Contest Details:

Time: 20th January 2019 (2130 hrs) to 21st January 2019 (0000 hrs). (Indian Standard Time — +5:30 GMT) — Check your timezone.

Contest link:https://www.codechef.com/COOK102

Registration: You just need to have a CodeChef handle to participate. For all those, who are interested and do not have a CodeChef handle, are requested to register in order to participate.

Prizes:

Top 10 performers in Global and Indian category will get CodeChef laddus, with which the winners can claim cool CodeChef goodies. Know more here: https://www.codechef.com/laddu. (For those who have not yet got their previous winning, please send an email to winners@codechef.com)

Good Luck!
Hope to see you participating!!
Happy Programming !!


Regarding JavaFX Library error.

$
0
0

when we import JavaFX library in codechef IDE.

it was showing a syntax error.

How can we rectify this one??

Need your help! YVMUN

$
0
0

I cant understand why i am getting wrong answer in YVNUM. I matched all testcases and corner cases that i can think of ..with the testers solution and my solution ..all are hiving same answer..therefore i request u to guide me where i am getting wrong .given below is the ideone link of my code please do check https://ideone.com/e.js/GrO14s"

PARRTY - EDITORIAL

$
0
0

PROBLEM LINK:

Practice
Contest: Division 1
Contest: Division 2

Setter:Mohammad Nematollahi
Tester:Xiuhan Wang
Editorialist:Taranpreet Singh

DIFFICULTY:

Medium-Hard

PREREQUISITES:

Segment Tree and Implementation would do.

PROBLEM:

Given $N$ people numbered $1$ to $N$ and $M$ conflict pairs of people, answer $Q$ queries of format. Given $K$ pairs in a query namely $[L_i, R_i]$ inviting all people in range $[L_i, R_i]$ to party. Determine if all people invited to party get along or not.

QUICK EXPLANATION

  • The brute solution to answer each query in $O(N+M)$ time can be easily written. This solution takes $O(Q*(N+M))$ in the worst case if all queries consist of a single interval of people invited and shall time out.
  • We can develop another solution answering each query in $O(K^2)$ time by iterating over every pair of intervals in the query and checking if both of the intervals contain a pair of people who do not get along. If it holds true for any pair of intervals in query, answer is NO, otherwise YES. This solution too won't work as it may take time to answer if there are too many intervals in few queries.
  • To find out whether any pair of an interval of people contains any conflict pair, we shall use segment tree operations.
  • Now combine the above two solutions, using the first solution when $K$ is large, and the second solution when $K$ is small, to fit the overall solution into the time limit.

EXPLANATION

The solution to this problem is based on merging two slower solutions to fit the time limit, so let us discuss both solutions.

Brute Solution:

To answer each query in $O(N+M)$ time, we can simply maintain an array included, marking all the elements present in intervals and then iterating over all conflict pairs to check if both persons in any pair are included or not. This solution performs well when there are few queries with a high number of intervals.

Segment Tree Solution:

We can also build a solution which, for each query check all pairs of intervals to see if the first member of any conflict pair lies in the first interval and second person of conflict pair lie in the second interval of selected pair. If we tried to maintain a set of conflict pairs, it shall time out. We need something smarter.

Let us store these queries and answer them all together efficiently.

What we can do is, to build a range max Segment tree initially filled with -1 and start considering each person one by one. While considering yth person, Consider all conflict pairs $(x,y)$, $x < y$ and update these positions with value y.

Now, whenever we have an ith interval in query $[L, R]$ where $R$ is the person being considered, we can check all intervals ending before $L$ and check if range max of any interval is always strictly smaller than $L$. The reasoning is as follows.

Considering only first $i$ persons, when we reach the ith person, we update all positions having a conflict with the ith person with value i. Now, If any interval has range maximum x if implies that in that interval, there is a person having a conflict with the xth person. So to check if any conflict pair is there, we iterate over all intervals ending before the current interval and check if this interval has a conflict with any person having index $\geq L$. (Meaning people in the current interval). This compares all unordered pairs of intervals and thus, runs in $O((N+M)*log(N)+\sum K^2)$ to answer all queries.

This solution won't work due to square factor and shall time out when there are few queries with a large number of intervals.

Merging solutions:

Let us define a limit $SQ$. We use brute solution when $K \geq SQ$ achieving worst case when $K = SQ$ taking $O(Q*(N+M))$. This may seem large, but here, $Q*SQ \leq 2*10^5$

We shall use the second solution when $K < SQ$ as our second solution is good in answering queries when $K$ is small. If all queries are answered by this solution, it takes $O((N+M)*log(N)+\sum K^2)$ time. But, since $K < SQ$, $\sum K$ cannot exceed $2*10^5*SQ$.

By choosing a reasonable value of $SQ$, we can achive $O((N+M)*Q/SQ +(N+M)*log(N)+Q*SQ)$.

Implementation hints:

It is useful in the second solution to sort the intervals beforehand so you only need to check intervals indexed up to the current interval. Additionally, do not forget to consider the case when a conflicting pair lies inside a single interval only in the second solution. This can be easily handled by considering the pair of an interval with itself too.

There are quite a number of problems merging solutions, but I don't remember any at present, so please share the link of any problem you know using a similar idea.

Time Complexity

Time complexity of this solution is $O((N+M)*Q/SQ +(N+M)*log(N)+Q*SQ)$ in worst case. Memory complexity is $O(N*log(N)+M+\sum K)$ in worst case.

AUTHOR'S AND TESTER'S SOLUTIONS:

Setter's solution
Tester's solution
Editorialist's solution

Feel free to Share your approach, If it differs. Suggestions are always welcomed. :)

Is there any formula to calculate nth fibonacci number ?

$
0
0

Suppose we are given, a(0)=x ; a(1)=y

and the relation, a[i]=a[i-1]+a[i-2]

and , we are asked to calculate a(n) , how to calculate it if n is as big as 10^9 ?

Thanks ! :-)

Doubt in MGAME explanation

$
0
0

In the first line of the explanation why does M have to be less than min(i,j,k,N) and how are we removing the last mod N.??

GetCode Codechef/Codeforces/SPOJ plugin for Sublime Text 3

$
0
0

GetCode

This is an ST3 plugin which:

  1. Take the URL of a problem such as https://www.codechef.com/problems/INVLIS.
  2. Parse the URL and get the problem code(In this case it is INVLIS).
  3. Create a new .cpp or .py file with a pre-determined template(cpp.snippets which is customized via settings).

Keyboard shortcuts

For all Operating Systems, ctrl+alt+x opens a new dialogue box which takes the problem url as user input

How to install

  1. Install Package Control from here
  2. In Sublime, Open Package Control

Open Package Control from the Preferences Menu

  1. Select Package Control: Install Packages

enter image description here

  1. Now you should be able to search for and install the GetCode Package.

How to use it

After installing, it is necessary to configure the settings(setting the default directory and snippets).

After configuring the settings, ctrl+alt+x opens a new dialogue box which takes the problem url as user input

Supported websites(for now)

  1. Codechef
  2. Codeforces
  3. SPOJ
  4. Virtual Judge

Invitation to RATED Contest(Div1+Div2) Enigma on Codechef, 10 January 2019

$
0
0

Hello, CodeChef community,

Tired of searching for the perfect teammates in every competition? Want to prove your worth as a solo coder? Looking for the perfect platform to display your skills?

This solo coding competition is designed for the masterminds who think they perform best alone.

Interested? Wanna find out more about it? No Problem! Plinth in association with Aavas Financiers Limited has a rated contest on CodeChef waiting just for you!

The contest shall be a great opportunity for students to test their programming aptitude. This national level programming contest shall assess the intelligence of participants through various rounds. It is an Online Qualifier Round for the Onsite Round to be held during Plinth'19.

There will be 2 rounds to decide the winner:

First Round: Online Qualifier for onsite will be held on 10th January 2019 on Codechef which is a rated round(Div1+Div2).

Second Round: Onsite round will be held at The LNM Institute of Information Technology, Jaipur on 20th January. More details will be announced later.

The Contest details of Enigma'19 are as follows:
- Contest duration will be 3 hours.
- Solo Coding Event
- Start time: 10th January 2019, 21:00 hrs IST
- End time: 11th January 2019, 00:00 hrs IST

Contest Link: Click Here

Problem Setters: panikxodiacaditya10_tds115

alt text

Good Luck and have Fun!


GetCode Codechef/Codeforces/SPOJ plugin for Sublime Text 3

$
0
0

GetCode

This is an ST3 plugin which:

  1. Take the URL of a problem such as https://www.codechef.com/problems/INVLIS.
  2. Parse the URL and get the problem code(In this case it is INVLIS).
  3. Create a new .cpp or .py file with a pre-determined template(cpp.snippets which is customized via settings).

Keyboard shortcuts

For all Operating Systems, ctrl+alt+x opens a new dialogue box which takes the problem url as user input

How to install

  1. Install Package Control from here
  2. In Sublime, Open Package Control

Open Package Control from the Preferences Menu

  1. Select Package Control: Install Packages

enter image description here

  1. Now you should be able to search for and install the GetCode Package.

How to use it

After installing, it is necessary to configure the settings(setting the default directory and snippets).

After configuring the settings, ctrl+alt+x opens a new dialogue box which takes the problem url as user input

Supported websites(for now)

  1. Codechef
  2. Codeforces
  3. SPOJ
  4. Virtual Judge

Invitation to RATED Contest(Div1+Div2) Enigma on Codechef, 10 January 2019

$
0
0

Hello, CodeChef community,

Tired of searching for the perfect teammates in every competition? Want to prove your worth as a solo coder? Looking for the perfect platform to display your skills?

This solo coding competition is designed for the masterminds who think they perform best alone.

Interested? Wanna find out more about it? No Problem! Plinth in association with Aavas Financiers Limited has a rated contest on CodeChef waiting just for you!

The contest shall be a great opportunity for students to test their programming aptitude. This national level programming contest shall assess the intelligence of participants through various rounds. It is an Online Qualifier Round for the Onsite Round to be held during Plinth'19.

There will be 2 rounds to decide the winner:

First Round: Online Qualifier for onsite will be held on 10th January 2019 on Codechef which is a rated round(Div1+Div2).

Second Round: Onsite round will be held at The LNM Institute of Information Technology, Jaipur on 20th January. More details will be announced later.

The Contest details of Enigma'19 are as follows:
- Contest duration will be 3 hours.
- Solo Coding Event
- Start time: 10th January 2019, 21:00 hrs IST
- End time: 11th January 2019, 00:00 hrs IST

Contest Link: Click Here

Problem Setters: panikxodiacaditya10_tds115

alt text

Good Luck and have Fun!

Video Editorial | Chef and Modulo Game | MGAME

$
0
0

Check out the video Editorial for MGAME from January long challenge 2019 LINK

Runtime error

$
0
0

I am getting a runtime error(SIGSEGV) when I run the code on codechef's compiler. Below is my code but when I run this code on my local machine then it is giving me the right outputs. I don't know what is the problem in the code.

include <iostream>

include<limits.h>

include<vector>

using namespace std; vector <int> g; int dif(int m1) { int d=INT_MAX,el,c; vector<int>::iterator r; vector<int>::iterator i; for (i = g.begin(); i != g.end(); ++i) { el=*i; if((m1-el)>=0 && (m1-el)<d) { r=i; d=(m1-el); c=1; } } if(c==1) { m1=d; g.erase(r); }

return m1;

}

int main() { int t,n,m,count,x; cin>>t; for(int i=0;i<t;i++) {="" <br=""/> int y; cin>>n; cin>>m; y=n; int *a=new int[n]; for(int j=0;j<n;j++) {="" cin="">>a[j]; g.push_back(a[j]); } while(m>0 && y!=0) { x=dif(m); m=x; y--; }

    if(m==0)
        cout<<"Yes"<<"\n";
    else
        cout<<"No"<<"\n";
    g.clear();
}
return 0;

}

Extremely unbalanced short contests

$
0
0

It has been my observation that CodeChef's short contests like monthly Cook-Offs and Lunchtimes have become extremely unbalanced in difficulty level.

The best example I have is of COOK101 (December Cook-Off). Being in Division 1, I could not do a single question from that contest (thankfully I made no submissions). I understand Division 1 is meant to be hard but among 5 questions at least 1 should be on the easier side (greedy algorithms, implementation problems, etc.).

But the true problem occurs with Division 2 (COOK101B). Their first question was easy, and the next 4 questions are common with Division 1. Look at the fall in successful submissions, 2080 to 85. That means for more than 95% of participants, acing the Cook-Off meant doing the easy question fast. That was the end of the contest for them.

And usually also, if there are 5 questions in a Cook-Off or Lunchtime, the last 3 have successful submissions in single digits, compared to >100 submissions for the first two problems.

Now, I understand that having hard questions is good for learning. But still the contests are so unbalanced, it's not fun at all. And Division 2 contests are not appropriate for Division 2 level at all. Division 1 also feels like as if its for rating >= 2100.

I think that many other users might also be disappointed with the current difficulty distributions.

Viewing all 39796 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>