please help, segmentation fault in Yalalovichik Numbers, Dec Cookoff

My code works fine on my computer but when I try submitting, i get a segmentation fault error? Can someone please tell me what i am doing wrong? It’d be a huge help. Thanks in advance.
CodeChef: Practical coding for everyone (link to my code)

code//

#include
#include
#include
using namespace std;
#define ll long long
#define mod %(100010001000 +7)
#include<bits/stdc++.h>
#include
#include

int main(){
int T;
cin>>T;

while(T--){
    ll sum =0;
string c;
cin>>c;

int len= c.length();
int n= stoi(c);
int i=0;

for(i;i<len;i++){
    ll p= pow(10,len);
    p=p%(1000*1000*1000 +7);
    p=pow(p,len-i-1);
    p=p%(1000*1000*1000 +7);
    sum= (sum + (n*p));
    sum=sum%(1000*1000*1000 +7);
    if(len > 1)
        rotate(c.begin(), c.begin()+1, c.end());
    n=stoi(c);
    //n = (n/10) + (n%10)*pow(10,len-1);
}
cout<<sum<<endl;

}

return 0;
}

Hi, @bansal_aman – thanks for providing a link to your code; that’s usually easier to read than pasting the code in the Discussion text box.

In line 21 and 33, you take the input string c (possibly left shifted as in line 33) and convert it to an integer.

   int n= stoi(c);

Have you tested your algorithm when the length of c is 11 or more? (The problem statement allows the input number to have up to 10000 digits.)

Good luck!

You are storing the result of stoi() in an int variable, which as per the constraints of the problem is not possible (since |n| < 10^5, hence n < 10^{10^5}). stoi() throws an out_ of_range exception (and hence a segmentation fault) if data can’t fit into the size it is represented in. You can read more about it here.

I would suggest not to convert the string to an integer as a whole, but first compute the value of n (taken input as a string) modulo MOD, and then repeatedly slice of the first digit, do some changes in the above value and append the first digit to the end of the string. Do this till the length of the number is traversed. This way you avoid doing any unexpected data type conversions.

Thanks a lot! This was a huge help.

Thanks a lot! This was a huge help.