Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8132385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:20:12+00:00 2026-06-06T09:20:12+00:00

I am trying to run a program and SOMETIMES I’m getting this error when

  • 0

I am trying to run a program and SOMETIMES I’m getting this error when trying to compile

ld returned 1 exit status

Although, when it compiles (like I said, not always) and trying to put certain input, the program outputs the answer… and then crashes. Is there any way is this possible at all?

Here is my code:

#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;

int main() {
  bool flag;
  int n,x[] = {1,-1};
  long long int A,cont,mul,acum;
  while(cin >> n >> A) {
    acum = 0;
    vector<long long int> nums(n),nums2;
    for(int i=0;i<n;i++) cin >> nums[i];

    sort(nums.begin(),nums.end());

    for(int i=n-1;i>=0;i--) {
      flag = 1;
      for(int j=0;j<i and flag;j++) if(nums[i]%nums[j] == 0) flag = 0;
      if(flag) nums2.push_back(nums[i]);
    }

    n = nums2.size();

    vector<long long int> mult(n,0);

    const long long int num = (1<<n);
    for(int i=0;i<num;i++) {
      cont = 0, mul = 1;
      for(int j=n-1;j>=0;j--) if(i & (1<<j)) cont++, mul *= nums2[j];
      mult[cont-1] += A/mul;
    }

    for(int i=0;i<n;i++) acum += (x[i%2]*mult[i]);

    cout << acum << endl;
  }
  return 0;
}

and this is the input I put when the program crashes:

17 1000
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:20:14+00:00Added an answer on June 6, 2026 at 9:20 am

    If you run your program in a debugger, you’ll see that the problem occurs on the line

    mult[cont-1] += A/mul;
    

    when cont is 0, which means that you are trying to modify element -1 of the vector.

    I can’t tell what the algorithm is trying to do, so I can’t tell why cont might end up zero.

    Overall advice: Learn to use a debugger. It’s your friend. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to run this program but i keep getting the error esent.h
Trying to run a program in Eclipse on Tomcat 7.0.12 and I'm getting this
I am trying to run a program like this: $CMD $ARGS where $ARGS is
When i am trying to run my program it is giving the following error
I have this problem when trying to run hello world program using android SDK.
I am trying to run this example program - http://pysnmp.sourceforge.net/examples/1.x/snmpbulk.html I am on Mac
I was trying to run a program with a mediaplayer. Why am I getting
trying to run this program. I think that to setup all of the web
I'm trying to run the program I'm debugging as a different user. Now, this
This is frustrating. I'm trying to run a program which loads into memory a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.