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

Using publicly available code for long contest

$
0
0

I am not sure if I'm giving out too much information but for one of the questions in the long contest, I needed to use the segment tree data structure. So I copied the code from GeeksForGeeks and modified it to suit my needs.

If someone else has also used the code from this site, then his and my codes might be similar enough for MOSS to consider it as cheating.

What can I do about this?


sereja and lcm?

$
0
0

where i'm wrong!!! is the approach correct or not plz help i think it should pass at least for subtask 1

include <iostream>

define MOD 1000000007

using namespace std; int main() { int t; long long int m,tot,res,x,l,r,i,j,n,temp; cin>>t; while(t--) { cin>>n>>m>>l>>r; tot=1; for(i=1;i<=n;i++) tot=(totm); res=0; for(i=l;i<=r;i++) { x=m/i; temp=1; for(j=1;j<=n;j++) temp=(temp(m-x)); res=(res+tot-temp); } cout<<res%MOD<<"\n"; } return 0; }

CLPERM points problem :/

$
0
0

i submitted the solution for CLPERM approx 4 hours ago.. i got 100 pts.
my total score should be 515 after that submission but its only 445. I resubmitted the solution i got 100 pts again but it is still showing 445 pts. :/

What's happening? Are the solutions getting rejudged?

getting TLE in 'gcdqueries'..how to reduce compile time.

$
0
0

I am using a simple algorithm, which passed some testcases and gave 40 points..In the question it is given that USE FASTER IO.. Can anyone tell me what does it mean...I am using C.

Submission showing 0 memory

$
0
0

I am getting wrong answer as a result in the SEAVOTE problem of Long Challenge January, and that is fine with me, but my submission results are showing 0 memory used, can anyone explain, why does this happen? Why does a submission show 0 memory used?

January Challenge Sereja and Votes

$
0
0

The points aren't being updated for correct answers. Admin please look into it.

CHEFSTON - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Dmytro Berezin
Tester:Shiplu Hawlader
Editorialist:Lalit Kundu

DIFFICULTY:

CAKEWALK

PRE-REQUISITES:

Basic Maths

PROBLEM:

There are total N kinds of stones. There is unlimited supply of each kind of stone.
Chef knows that one stone of kind i needs Ai minutes to pick it from the ground and it will give Chef a profit of Bi Rs.
Chef has K minutes of free time. During this free time, Chef want to pick stones so as to maximize his profit. But he can not pick stones of different kinds, he has to pick stones of a single kind. Please help Chef to find the maximal possible profit.

EXPLANATION:

We traverse over each kind of stone assuming that he will pick that kind of stone and calculate profit in that case.
So, if it takes x minutes to pick up one stone(which gives a profit of y), in K minutes, you will pick up K/x stones(note the division is integer division). So profit in such a case will be (K/x) * y.

Pseudo Code:

ans=-1
for i=1 to N:
    ans = max(ans, (K/A[i])*B[i])

Note that we'll need to use 64-bit integers because of higher constraints.

Complexity: O(N).

SOLUTIONS:

Setter's solution
Tester's solution

is there any limit to the no. of submission.

$
0
0

iafter every submission it says wrong answer .but i am getting corect o/p in my editor.


stacking pancakes

NZEC error in java code for Queries in the String.

SEAVOTE - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Sergey Nagin
Tester:Shiplu Hawlader
Editorialist:Lalit Kundu

DIFFICULTY:

EASY-MEDIUM

PRE-REQUISITES:

Maths

PROBLEM:

Sereja conducted a voting about N of his opinions. Ai percent of people voted for opinion number i. This statistics is called valid if sum of all Ai is equal to 100.

Now let us define rounding up of a statistics A.

  • If Ai is not an integer, it will be rounded up to next integer.
  • Otherwise it will be left as it is.

Now let us consider a statistics B of size N in which each of Bi is an integer. Now he wants to know whether there exists some valid statistic A of size N (may contain real numbers) such that after rounding it up, it becomes same as B?

1 ≤ N ≤ 10000

EXPLANATION:

Let's define a small positive infinitesimal quantity e(epsilon).
For Bi, corresponding element in array A can be in range [Bi-1+e,Bi](both ranges included).
However, Bi = 0, the above range is invalid, so we'll ignore all zero elements. Let's say size of the remaining array B(after removing 0s) is N.
We now consider the lower and upper bounds on the sum of all numbers in A(note: any real number between these bounds can be generated). So, if 100 lies within these bounds, then answer is yes.

LOWER BOUND:

Now, what is the minimum possible sum? It's S = [sum over all Bi] - N(number of non-negative elements in B) + e. So, we get condition for validity that S ≤ 100. If we ignore the e, we get S = [sum over all Bi] - N(number of non-negative elements in B) < 100.

UPPER BOUND:

Now, what is the maximum possible sum? It's S = sum over all Bi. So, we get one more validity condition that S ≥ 100.

Complexity: O(N).

SOLUTIONS:

Setter's solution
Tester's solution

Note: Some people were confused about bounds on array A. A was not even in input.

Cannot understand Chef and Brackets

$
0
0

PLease if anyone could explain me the DP approach of the question Chef and Brackets as I am not able to understand the solution from the editorials.

Please explain so that a beginner like me could understand.

what is wrong with my code to display max output ??

$
0
0

include<stdio.h>

include<stdlib.h>

int main() { int T,N,i,A[100001],B[100001],C[100001],res[100001],max=0;

unsigned long int K;

scanf("%d",&T);

while(T--) { scanf("%d %d",&N,&K);

   for(i=0;i<N;i++)
   {
       scanf("%d",&A[i]);
   }
   for(i=0;i<N;i++)
   {
       scanf("%d",&B[i]);
   }
   for(i=0;i<N;i++)
   {
       C[i]=K/A[i];
   }
   for(i=0;i<N;i++)
   {
       res[i]=C[i]*B[i];
   }
   for(i=0;i<N;i++)
   { if(res[i]>max)

       max=res[i];
   }


   printf("%d\n",max);

} return 0; }

SEALCM - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Sergey Nagin
Tester:Shiplu Hawlader
Editorialist:Lalit Kundu

DIFFICULTY:

MEDIUM-HARD

PRE-REQUISITES:

Solving Dynamic Programming with Matrix Expo

PROBLEM:

In this problem Sereja is interested in number of arrays A[1], A[2], ..., A[N] (1 ≤ A[i] ≤ M, A[i] - integer) such that least common multiple (LCM) of all its elements is divisible by number D.

Please, find sum of answers to the problem with D = L, D = L+1, ..., D = R. As answer could be large, please output it modulo (109 + 7).

TECHNIQUE:

I will suggest to specifically read the whole post given in pre-requisite if you are not aware about this technique.

Let there be K states, s1,s2...sK.
For each state si where i = 1 to K recurrence is defined as:
f(n,si) = sum {j=1 to K} c{i,j} f(n-1,sj).
We define a transition matrix M where M[i][j]=c{i,j}.
Let's consider M{N-1}. Sum of all elements in row i will give us f(N,si) assuming f(0,i)=0 forall i.

EXPLANATION:

Let's solve for a single D first. Let's say D has prime factors p1a1, p2a2 ... pKaK, where all pi are unique.
We form a simple dp of dp[n,mask], which denotes the answer for N=n and D=product [pia_i], if i'th bit is set in mask. So, mask is a K bit number.
A simple recurrence would be:

&ret = dp[n,mask]
primes[] //prime factors of D.
count[] //count[i] denotes count of i'th prime in D
ret=0
for i = 0 to size(primes)-1:
    for j = 1 to M:
        //if j is kept at n'th index
        //and keeping it satisfies the i'th set bit condition
        //ie. count of primes[i] in j is greater than or equal to count[i]
        if count_prime(primes[i] in j) >= count[i]
            //set the i'th bit, add to current ret
            ret += dp[n-1, mask|(1<<i)]

So, we can clearly see that our dp[n,x] is dependent on dp[n-1,y], where x and y are two different masks/states. We can use matrix exponentiation here because in one state in our dp, answer for n is dependent on answer for n-1 and the transition among other states is not dependent on n.

So, we form the transition matrix, where we set at transit[i][j], the number of ways to reach mask j from mask i . Sum of all elements in (2K-1)'th (all bits of D are set) row of N-1'th power of this transition matrix, will give us the answer, in accordance with the technique explained above. Try to prove how this works.

So, complexity for single D is O(log N * (2K*3)). Note matrix multiplication of sizes P*P takes O(P3). And, maximum K is until 235711*13... is smaller than 1000(since D<1000). So, max K is 4. Total complexity for single D is O(log N * 212).

Doing it for 1000 such values of D will take O(log N * 222) worst case. Note it will be really lower than that because of variation in K.

SOLUTIONS:

Setter's solution
Tester's solution

XRQRS - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Fedor Korobeinikov
Tester:Shiplu Hawlader
Editorialist:Lalit Kundu

DIFFICULTY:

MEDIUM-HARD

PRE-REQUISITES:

Tutorial on trie and example problems

PROBLEM:

Given an initially empty integer array (1-indexed) and M queries:

  • Type 0: Add the integer number x at the end of the array.
  • Type 1: On the interval L..R find a number y, to maximize (x xor y).
  • Type 2: Delete last k numbers in the array
  • Type 3: On the interval L..R, count the number of integers less than or equal to x.
  • Type 4: On the interval L..R, find the kth smallest integer (kth order statistic).

1 ≤ M≤ 5 * 105

QUICK EXPLANATION:

Make a trie of integers experessed in binary and at each node in trie, also store the indexes of numbers which pass through that node.

EXPLANATION:

If you don't know about tries, read the complete post given in pre-requisites and try to implement those problems.
Let's say in query of Type 1, L=1 and R=N, how will we solve?
We'll build a trie and we can insert in O(number of bits) each element. We can also delete in O(number of bits) any number, but we'll have to store at each node count of elements passing through that node. The following image doesn't store counts at node.

image

For maximum xor query, for L=1 and R=N, we'll just traverse over the whole trie and starting from most significant bit(MSB), we try to form largest number possible. Example:

image

But our query is for range L to R, so we store at each node also the indexes of numbers which pass through the that node. For example:

image

But how does this help? When we go down the tree during a query and maximising the xor, we go in a direction that contains at least one index in range L to R, otherwise we go in the other direction. Let's consider the above image as example and we want maximum xor with '110' in range [1,2] (note the zero indexing):

image

About inserting a number x which occurs at position y in array, we express x in binary and then traverse the trie and at each step push the value y at each vector of node that we pass through.
So, a total of log(x) times, we'll push y in different vectors. Therefore, total memory used won't exceed M*log(MAX). These vectors will always be sorted as we insert in increasing order of y.

About deleting last k elements, we can do it in O(h * k)(h=height of trie = log MAX). Point worth noting is that sum of k over all deletion queries can't exceed M. So, we can do deletions in O(h * k). While deleting one element from end, we just go traverse over the trie along the path of element being deleted and pop from the end of the vector(ie. pop the index of element) at each node.

Now, about the type4 query, we need k'th smallest element in range L to R.
Again, let's solve for L=1, R=N.
I start from the root, and at each step I know, that if I go towards left, I'll encounter say K1 leaves(numbers) and at right side I'll encounter K2 leaves(numbers) and considering that all numbers on left side are smaller than those at right side, I can easily make a decision whether I want to go to left side or right side(ie. whether this bit of my answer will be set or not, we already have calculate previous bits of our answer).

Similarily to the way we handle xor range queries, we can do for a range query in k'th order statistics. We can count number of indexes in range [L,R] in a vector using lower_bound, upper_bound.

SOLUTIONS:

Setter's solution
Tester's solution


GCDQ - Editorial

$
0
0

PROBLEM LINK:

Practice
Contest

Author:Praveen Dhinwa
Tester:Shiplu Hawlader
Editorialist:Lalit Kundu

DIFFICULTY:

EASY

PRE-REQUISITES:

GCD, Precomputation

PROBLEM:

You are given an array A of integers of size N. You will be given Q queries where each query is represented by two integers L, R. You have to find the gcd(Greatest Common Divisor) of the array after excluding the part from range L to R inclusive (1 Based indexing). You are guaranteed that after excluding the part of the array remaining array is non empty.
1 ≤ N, Q≤ 100000

EXPLANATION:

If you do it naively(ie. calculating GCD of remaining array for each query), the worstcase complexity will be O(N * Q).
Let's denote by G(L, R), the GCD of AL, AL+1 ... AR. We can observe that for query [L, R], we need GCD of G(1, L-1) and G(R+1, N).
So, we precalculate prefix and suffix gcd arrays.
If we have:
Prefixi = GCD of A1, A2 ... Ai
Suffixi = GCD of AN, AN-1 ... Ai
answer to query [L, R], would be GCD of PrefixL-1 and SuffixR+1.

We can calculate prefix and suffix arrays in O(N) if we notice that:
Prefixi = GCD(Prefixi-1, Ai)
Suffixi = GCD(Suffixi+1, Ai)

Pseudo Code for building prefix and suffix arrays:

n,a=input
pre[n],suf[n]

//base case
pre[1]=a[1]
suf[n]=a[n]

for i=2 to n:
    pre[i] = gcd(pre[i-1], a[i])

for i=n-1 to 1:
    suf[i] = gcd(suf[i+1], a[i])

So, overall complexity would be O((N + Q) * K), where K is a constant factor for gcd calculation.

ALTERNATIVE SOLUTION:

Use segment trees for range gcd queries. But note that a factor of log N will be increased in complexity.

SOLUTIONS:

Setter's solution
Tester's solution

Runtime Error..

December Long14 problem understanding Sereja and GCD

$
0
0

In december long14, problem Sereja and GCD
Example
Input:
2
5 5 1 5
5 5 4 5
Output:
3125
2
How is the first answer 3125?
As per my understanding,
N=5,M=5 hence number of permutations of 5 elements from set [1,2,3,4,5].
L=1,R=5 so, D=1,2,3,4,5.
Thus for D=1, required array is =>[1,2,3,4,5], no of permutations = 120.
For D=2, required array is =>[2,4], no of permutations = 2.
For D=3, required array is =>[3], no of permutations = 1.
For D=4, required array is =>[4], no of permutations = 1.
For D=5, required array is =>[5], no of permutations = 1.
Total =120+2+1+1+1=125.
Please correct me if I am wrong.
PS: If the answer reveals strategy than please do not answer.

Sereja and number game 2

$
0
0

Hello, I was not able to solve sereja and number game 2 problem of jan15. Can anyone provide me the editorial or any hint on solving the problem.

Wrong Answer in Spoj Army

$
0
0

I am unable to understand why I am getting WA in this problem Will someone please point out the mistake or provide me the test case for which my Algo fails ....i have used the approach of comparing each and every soldier of the army by their strength ...My solution link is http://ideone.com/02V8D2

Viewing all 39796 articles
Browse latest View live


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