I would like to know why I'm getting Runtime Error(SIGFPE)

problem’s link:here
it should be because of a division by zero, but in the problem is stated that the values are greater than zero and I don’t know how I can get that error if not for that.
CODE:
#include
#include
#include
#define ll long long
#define FOR(i,j,k) for(int i=j;i<k;i++)

void FastIO(){
std::ios_base::sync_with_stdio(0);
std::cin.tie(0);
std::cout.tie(0);
}

int power(int a,int b){
int result=1;
while(b>0){
if(b%2==1) result*=a;
a*=a;
b/=2;
}
return result;
}

int n2_pow(int n){
int cnt=0;
while(n>0){
n/=2;
cnt++;
}
return (cnt-1);
}

void solve(){
int r,g,b,m,res;
std::cin>>r>>g>>b>>m;
int maxr,maxg,maxb;
maxr=maxg=maxb=0;
FOR(i,0,r){
int rr;
std::cin>>rr;
if(rr>maxr) maxr=rr;
}
FOR(i,0,g){
int gg;
std::cin>>gg;
if(gg>maxg) maxg=gg;
}
FOR(i,0,b){
int bb;
std::cin>>bb;
if(bb>maxb) maxb=bb;
}
std::vector vec={maxr,maxg,maxb};
std::sort(vec.begin(),vec.end());
int p=vec[2]/vec[0];
int q=vec[1]/vec[0];
int pp=n2_pow(p);
int qq=n2_pow(q);
if(pp+qq>m){
p=vec[2]/vec[1];
pp=n2_pow(p);
if(pp>m){
res=vec[2]/power(2,m);
std::cout<<res<<"\n";
return;
}else{
vec[2]/=power(2,pp);
m-=pp;
int m_2=m/2;
if(m%2==0){
res=vec[2]/power(2,m_2);
std::cout<<res<<"\n";
return;
}else{
res=vec[1]/power(2,m_2);
std::cout<<res<<"\n";
return;
}
}
}else{
vec[2]/=power(2,pp);
vec[1]/=power(2,qq);
m-=(pp+qq);
int m_3=m/3;
if(m%3==0){
res=vec[2]/power(2,m_3);
std::cout<<res<<"\n";
return;
}else if(m%3==1){
res=vec[1]/power(2,m_3);
std::cout<<res<<"\n";
return;
}else{
res=vec[0]/power(2,m_3);
std::cout<<res<<"\n";
return;
}
}
}

int main(){
FastIO();
int t=1;
std::cin>>t;
while(t–){
solve();
}
return 0;
}

Check the following simpler priority-queue based solution.

Accepted

1 Like