CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • All Contests
    • June Long 2012
    • May Cook-Off
    • May Long 2012
  • DISCUSS
    • Forums
    • Blog
    • Wiki
    • Facebook
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Tutorials
    • Long Contest Ranks
    • Short Contest Ranks
    • Event Calendar
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home » Practice(medium) » Best Sums

Best Sums

Problem code: BESTSUM

  • Submit
  • All Submissions

All submissions for this problem are available.

Given a sequence of positive integers a1, a2, , aN, and 1 i j N, the partial sum from i to j is ai + ai+1 + + aj.

In this problem, you will be given such a sequence and two integers P and K. Your task is to find the smallest partial sum modulo P that is at least K.

For example, consider the following sequence of integers:

    12     13     15     11     16     26     11

Here N = 7. Suppose K = 2 and P = 17. Then, the answer is 2 because 11+16+26 = 53 and 53 mod 17 is 2. On the other hand, if K = 0 the answer is 0 since 15 + 11 + 16 + 26 = 68 and 68 mod 17 is 0.

Input format

The first line contains three integers N, K and P. This is followed by N lines each containing a single integer.

You may assume 1 N 100000.

Output format

A single integer indicating the minimum possible segment sum modulo P that is at least K.

Example:

Sample Input

7 2 17
12
13
15
11
16
26
11

Sample Output

2

Author: admin
Date Added: 30-07-2009
Time Limit: 3 sec
Source Limit: 50000 Bytes
Languages: ADA, ASM, BASH, BF, C, C99 strict, CAML, CLOJ, CLPS, CPP 4.0.0-8, CPP 4.3.2, CS2, D, F#, FORT, GO, HASK, ICK, ICON, JAR, JAVA, JS, LISP clisp, LISP sbcl, LUA, NEM, NICE, PAS fpc, PAS gpc, PERL, PERL6, PHP, PIKE, PRLG, PYTH, PYTH 3.1.2, RUBY, SCALA, SCM guile, SCM qobi, ST, TEXT, WSPC


  • Submit

Comments

  • Login or Register to post a comment.

admin, can you please provide

oblivion @ 29 Aug 2009 02:18 PM

admin, can you please provide one more test case for this problem because it seems that the answer is always k.

 

plz clear the problem...what

gauravgupta @ 9 Sep 2009 12:11 AM

plz clear the problem...what is mean of smallest partial sum modulo P that is at least K.

minimize the value of sum

admin @ 9 Sep 2009 02:16 PM

minimize the value of sum such that sum % p is atleast k

Aniruddha are u a part of the

vikrantsingh02 @ 18 Sep 2009 01:14 AM
Aniruddha are u a part of the admin team?? Dont u think we have to calculate smallest of ( (partial sum % P) - > that is at least K )?

and we have to output that

vikrantsingh02 @ 18 Sep 2009 01:15 AM
and we have to output that minimum value which is bound to be greater or equal to K and less than P

what is the range of K and

vikrantsingh02 @ 18 Sep 2009 01:33 AM
what is the range of K and P?? Plzz helpmeout . . ..

P will be a number that fits

admin @ 18 Sep 2009 02:19 PM
P will be a number that fits in a 32 bit integer and K will be less than P.

Can some one please tell me

vinayakpk @ 25 Sep 2009 12:28 PM

Can some one please tell me why my program is throwing a compilation error. I am using Bloodshed Dev - CPP to compile the code on my machine and it compiles just fine.

 

Please check the FAQ and

admin @ 25 Sep 2009 02:15 PM

Please check the FAQ and http://www.codechef.com/wiki

Is it an ad hoc sequence or

vinayakpk @ 25 Sep 2009 03:53 PM

Is it an ad hoc sequence or are the terms related to each other in some way.

In the example it seems like

vinayakpk @ 25 Sep 2009 03:54 PM

In the example it seems like the terms are just arbitrary....

No, they are not related in

admin @ 25 Sep 2009 04:28 PM

No, they are not related in any way.

What if the sum of ALL

bezgin @ 30 Oct 2009 03:12 PM

What if the sum of ALL numbers are smaller than K? Do we not print anything?

That won't happen.

triplem @ 30 Oct 2009 03:18 PM

That won't happen.

My output matches with the

bezgin @ 30 Oct 2009 04:38 PM

My output matches with the obvious brute force solution's for more than 1000 cases. Since nobody has managed to submit a correct solution, are you guys sure there's nothing wrong with the control mechanism?

My output matches with the

bezgin @ 30 Oct 2009 07:41 PM

My output matches with the obvious bruteforce solution for 100K cases now!!! Here is the brute force code:

#include <iostream>
#include <cstdlib>

int main() {
unsigned long N,K,P; scanf("%lu %lu %lu",&N, &K, &P);
unsigned long sums[N+1], sum, num, prev=0;

if(K>=P || N==0) exit(1);

sums[0] = 0;
for(unsigned long i=1; i<=N; i++) {
scanf("%lu", &num);
num = num %P;
if(num > (P-prev)) {
// overflow
sum = prev -(P -num);
} else
sum = prev + num;
prev = sum;
sums[i] = sum;
}
unsigned long tmp;
unsigned long resultSum=P, resultI=0, resultJ=0;

for(unsigned long j=1; j<=N; j++) {
for(unsigned long i=1; i<=j; i++) {
unsigned long add = P -sums[i-1];

if(add > (P-sums[j])) {
// overflow
tmp = sums[j] -(P -add);
} else
tmp = add +sums[j];

if(tmp>=K && (tmp < resultSum)) {
resultSum = tmp;
resultI = i;
resultJ = j;
}
#if 0
printf("%lu: (%ld, %ld) => ", tmp, i, j);
printf("%lu: (%ld, %ld)n", resultSum, resultI, resultJ);
#endif
if(resultSum == K) break;
}
if(resultSum == K) break;
}
if(resultSum != P) printf("%lu: (%ld, %ld)n", resultSum, resultI, resultJ);

return 0;
}

i have submittted solution in

hitesh.khamesra @ 14 Nov 2009 11:31 PM

i have submittted solution in ,NET. And i am getting "Time limit exceeded" error.

I am sure it is not somehing .NET causing slowness. But still want to confirm.

Can someone from Admin confirm this.

Thanks.

Can you explain this

akshaylive @ 25 Jan 2010 09:29 AM

Can you explain this again:

 

Here N = 7. Suppose K = 2 and P = 17. Then, the answer is 2 because 11+16+26 = 53 and 53 mod 17 is 2. On the other hand, if K = 0 the answer is 0 since 15 + 11 + 16 + 26 = 68 and 68 mod 17 is 0.

 

Please let me know why the answer is 2 and 0 respectively.

Which part of that exactly

triplem @ 25 Jan 2010 12:39 PM

Which part of that exactly don't you understand?

Here is another example. Suppose K=1, but the rest of the example is the same. No matter which partial sum you take, you'll find it is impossible to get a sum of 1 mod 17. It is possible to get 2 however in the same way as above (11+16+26 == 2 mod 17). So the answer is 2 again.

If a sum of 2 mod 17 wasn't possible, then you would need to see if 3 mod 17 was possible, and so on. Until you've found the smallest number >= K for which there is a partial sum adding to that, mod 17.

Thanks a lot Stephen; its

akshaylive @ 25 Jan 2010 02:01 PM

Thanks a lot Stephen; its clear now. One final question. Should the numbers we take be sequential wrt the input?

In the example, the sequence is " 12 13 15 11 16 26 11 "; Can we take a set as 12,15? Also how many numbers can form a set?

You are told that in the

triplem @ 25 Jan 2010 02:57 PM

You are told that in the first line of the problem. (It should say 1 <= i <= j <= N, of course).

Are you %100 positive that

bezgin @ 2 Feb 2010 04:16 PM

Are you %100 positive that there is nothing wrong with the judge? No successful submissions yet and i am sure my code works fine!

hey admin.. i've submitted

raman bhatia @ 31 May 2010 07:16 PM

hey admin.. i've submitted the code 8 times, different optimisations done at every attemt, they work fine at my place.. but all of'em show runtime error.. and of type- others.. even the help section is not of much help.. i am coding in c and the upper limit of n is 1,00,000 so i can't use an integer.. i am assuming that the server is not able to allocate that much size to an array.. so i reduced it.. but still , the same error every time.. please help me out.. coz its really very frustrating

Is it possible for you guys

hugurdag @ 1 Jun 2010 05:06 AM

Is it possible for you guys to give us the test case that fails our code? You are possibly testing against a very interesting corner case. Unfortunately, I have no idea what that corner case is. I am testing against the exhaustive solution with random input files of the largest possible size, and I always get an agreement between the two solutions. Now that algol has a succesful submission, I have faith in your testing procedure. However, looking for what is wrong in our code without the test case that fails the code is like looking for a needle in a hay stack. You guys should return the failing test case at least in the case of practice problems. You have a great website, and this way you can make it more useful. Thanx in advance.

 

I tried optimizing a lot but

everest @ 26 Jun 2010 12:57 PM

I tried optimizing a lot but doesnt work. I get TLE.

There are n2/2 sums. Shouldn't we check all of them (of course stopping inbetween if we are already at least possible answer = K)? That leads to an O(n2) algorithm and I guess thats slow for this problem.

Is there any way to get rid of using the modulo operator in this problem? That might be slowing things down.

A correct algorithm does not

triplem @ 26 Jun 2010 02:26 PM

A correct algorithm does not need to check all n^2/2 sums.

Hi Just one

shwetanka_19 @ 9 Aug 2010 01:39 AM

Hi

Just one question..

Suppose sequence is 12 13 15 11 16 26 11 19

K = 2 and P = 17 Then also result will be 2 and Partial sum will be 19

Am I thinking Correct?

I submitted a solution in

vineet.goyal123 @ 12 Oct 2010 04:55 PM

I submitted a solution in python and got this error

:( internal error occurred in the system

The solution works fine on my system. Is there smthing that I am missing while submitting codes in Python

Can You Tell Error in

omega2692 @ 20 Feb 2011 12:11 PM

Can You Tell Error in this....it gives correct results logically and to all your test cases....

#include<stdio.h>

int main()

{int n,k,i,j,p,a[10000],sum,ans;

scanf("%i %i %i",&n,&k,&p);

for(i=0;i<n;i++)

{scanf("%i",&a[i]);}

for(i=0;i<n;i++)

{sum=a[i];

for(j=i+1;j<n;j++)

{sum=sum+a[j];

if( (sum%p) < ans && (sum%p) >= k)

{ans=sum%p;}

}

}

printf("%i",ans);

}

can we have i=j in the

abhipranay @ 31 Dec 2011 05:25 PM
can we have i=j in the problem?i mean can we have only one element in the partial sum set?

SUCCESSFUL SUBMISSIONS FOR THIS PROBLEM:

Programming Competition Fetching successful submissions
Directi Go for Gold

HELP

Program should read from standard input and write to standard output. After you submit a solution you can see your results by clicking on the [My Submissions] tab on the problem page. Below are the possible results:

 

  • Accepted Your program ran successfully and gave a correct answer. If there is a score for the problem, this will be displayed in parenthesis next to the checkmark.
  • Time Limit Exceeded Your program was compiled successfully, but it didn't stop before time limit. Try optimizing your approach.
  • Wrong Answer Your program compiled and ran succesfully but the output did not match the expected output.
  • Runtime Error Your code compiled and ran but encountered an error. The most common reasons are using too much memory or dividing by zero. For the specific error codes see the help section.
  • Compilation Error Your code was unable to compile. When you see this icon, click on it for more information.
  • If you are still having problems, see a sample solution here.

CodeChef is a global programming communityCodeChef hosts online programming competitions
CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of algorithms, computer programming and programming contests. At CodeChef we work hard to revive the geek in you by hosting a programming contest at the start of the month and another smaller programming challenge in the middle of the month. We also aim to have training sessions and discussions related to algorithms, binary search, technicalities like array size and the likes. Apart from providing a platform for programming competitions, CodeChef also has various algorithm tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our programming contest judge accepts solutions in over 35+ programming languages. Preparing for coding contests were never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming challenges that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly coding contest and the shorter format Cook-off coding contest. Put yourself up for recognition and win great prizes. Our programming contests have prizes worth up to Rs.20,000 and $700lots more CodeChef goodies up for grabs.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be a part of CodeChef's Forums and interact with all our programmers - they love helping out other programmers and sharing their ideas. Have discussions around binary search, array size, branch-and-bound, Dijkstra's algorithm, Encryption algorithm and more by visiting the CodeChef Forums and Wiki section.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. You can also host a coding contest for your institute on CodeChef, organize an algorithm event and be a guest author on our blog.

Go For Gold

The Go for Gold Initiative was launched about a year after CodeChef was incepted, to help prepare Indian students for the ACM ICPC World Finals competition. In the run up to the ACM ICPC competition, the Go for Gold initiative uses CodeChef as a platform to train students for the ACM ICPC competition via multiple warm up contests. As an added incentive the Go for Gold initiative is also offering over Rs.8 lacs to the Indian team that beats the 29th position at the ACM ICPC world finals. Find out more about the Go for Gold and the ACM ICPC competition here.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com