You are not logged in. Please login at www.codechef.com to post your questions!

×

medium level problem - Marbles SIGSEGV error

for the problem Marbles : http://www.codechef.com/problems/MARBLES here is the code. But the problem is code chef is returning SIGSEGV error and i am unable to find out the problem in my code. So please tell me can my code be modified to fit the conditions of the question or do i have to think from a new perspective?

here is the code:

#include<iostream>
using namespace std;

long long int C(int n,int r)
{
    if(r==1)
        return n;
    if(n==r)
        return 1;
    long long int c=C(n-1,r)+C(n-1,r-1);
    return c;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,r;
        cin>>n>>r;
        cout<<C(n-1,r-1)<<endl;
    }
}

asked 05 Feb '14, 23:18

insaynasasin's gravatar image

1★insaynasasin
1054811
accept rate: 0%

edited 05 Feb '14, 23:20


You are using recursion may be due to stackoverflow you are getting this error. You should find a better efficient method for calculating nCr for given "n" and "r". You can further optimiz it . Posting code here but don't copy this code , try to understand it and then modify your code :) long long FindWays(int a,int b){ int n = a-1; int r = b-1,i=0; long long result=1; if (r > n/2) { r = n - r; } for (i = 0; i < r; i++) { result *= (n-i); result /= (i+1); } return result; }

link

answered 06 Feb '14, 00:17

chandan11111's gravatar image

3★chandan11111
3.6k133555
accept rate: 10%

I think chandan is right. . .

link

answered 06 Feb '14, 09:57

rachgonz's gravatar image

0★rachgonz
1
accept rate: 0%

I think RachGonz is right. . .

link

answered 10 Dec '17, 19:43

codesniper99's gravatar image

3★codesniper99
1186
accept rate: 7%

I think codesniper99 is right. . .

link

answered 11 Jan, 00:47

suraj_898's gravatar image

1★suraj_898
11
accept rate: 0%

edited 11 Jan, 00:48

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×2,556
×1,901
×278
×1

question asked: 05 Feb '14, 23:18

question was seen: 1,981 times

last updated: 11 Jan, 00:48