lundi 23 mars 2020

Why I am getting runtime error in Uva, when it's looks like a I have no mistakes in my code?

I can't find where the error is, the problem I'm trying to solve is 583-Prime factors from Uva. When I compile it in my pc, it runs with no problem, it confuse me a lot. I think the problem is with the vector I am using, but I'm not sure of that. Or maybe defining long long as ll or vector as vll is causing the runtime error?

#include <iostream>
#include <bitset>
#include <vector>
using namespace std;
#define HACK ios::sync_with_stdio(0); cin.tie(0);
#define M 46340
#define ll long long
#define vll vector<ll>
bitset<M>sieve;
void fill_sieve(){
    sieve.set();
    sieve.set(1,0);
    int i, j;
    for(i=2; i*i<=M; i++){
        if(sieve[i]==1){
            for(j=i*i; j<=M; j+=i)
                sieve[j]=0;             
        }
     }
 }

vll  factors(ll n){
    ll prim=2;
    vll ans;
    while(prim*prim<=n){
        while(n%prim==0){
            ans.push_back(prim);
            n/=prim;            
        }
        prim++;
        while(!sieve[prim]){
            prim++;
        }
    }
    if(n!=1){
        ans.push_back(n);
    }
    return ans;
}

int main(){
    HACK
    fill_sieve();
    int i;
    ll g;
    vll ans;
    while(cin>>g){
        if(g==0) return 0;
        ans.clear();
        if(g<0){
            ans= factors(-g);
            cout<<g<<" = -1 x ";
            for(i=0; i<ans.size(); i++){
                if(i==ans.size()-1) cout<<ans[i];
                else cout<<ans[i]<<" x ";
            }
            cout<<"\n";
        }
        else{
            ans= factors(g);
            cout<<g<<" = ";
            for(i=0; i<ans.size(); i++){
                if(i==ans.size()-1) cout<<ans[i];
                else cout<<ans[i]<<" x ";
            }
            cout<<"\n";
        }
    }
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire