在以下程序中使用c ++获取SIGABRT

时间:2013-08-15 09:08:00

标签: c++ primes sigabrt sieve-of-atkin

我在spoj中提交了此问题,这显示runtime error(SIGABRT)。它在我的机器和Ideone.com上正常工作但在那里显示错误。任何原因?我在这里写我的代码: 我想要做的是计算订单10 ^ 8的质数并处理它上面的一些操作。 这是问题链接:http://www.spoj.com/problems/CPRIME

#include<iostream>
#include<vector>
#include<cmath>
#include<cstdio>

using namespace std;
int n=100000001;
int d = 10001;//3163;
vector<int>num(100000001,0);

int main (){
    //atkin's seive for generating prime numbers of order 10^8
for (int x = 1; x <= d; x++){
        for (int y = 1; y <= d; y++){
            long long sqx=x*x,sqy=y*y;
            long long m =(3*sqx)-sqy;
             //m=m-sqx;
             if(m<n){
                 if(x>y&&m%12==11)num[m]=num[m]^1;
                 m=m+2*sqy;
                 if(m<n){
                    if(m%12==7)num[m]=num[m]^1;
                    m=m+sqx;
                    if(m<n&&(m%12==1||m%12==5))num[m]=num[m]^1;
                    }
                 }

            }
    }
   for (int i=5;i<d;i++){
       if(num[i]){
           for(int j=i*i;j<n;j+=i*i)num[j]=0;
       }
   }   //sieve finished here
    //main code start from here and for loop is for counting number of primes
    //less than or equal to that number.
    int add=2;
    num[2]=1;num[3]=2;
  for(int a=4;a<n;a++){
      if(num[a]){
          //if(a==2||a==3||a==5)cout<<num[a]<<" "<<a<<" ";
          add++;
          num[a]=add;
      }
      else num[a]=add;
  }
  //cout<<num[2]<<" "<<num[3]<<endl;
  while(1){
      int t;
      scanf("%d",&t);
      if(t==0)break;
      double ans=(num[t]-t*1.0/(log(t)))*100.0/num[t];
      if(ans<0)ans*=-1.0;
      printf("%.1lf\n",ans);
  }
  return 0;


}

2 个答案:

答案 0 :(得分:0)

100000001*4 = 400000004 bytes
              400000.004 kilo bytes
              400.000004 mega bytes

你需要半场演出 最少! 因为矢量也增加了一些开销。

操作系统将 bannanas 试图将其放入堆中。


作为旁注:用O(n ^ 2)时间计算素数是最天真和最慢的方法。考虑使用Atkins Sieve


确保你没有被零除。 100.0/num[t]。通常除以零会生成SIGFPE,但硬件也可以在此类除法上生成SIGABRT

答案 1 :(得分:0)

您应该将num作为数组从堆而不是堆栈中分配 int* num=new int[size];这样可以正常使用。