Countering Overflow, while calculating LCM, Please help!


#1

Please, check the last loop in which LCM is being calculated. Thanks, in advance.

long long int powe(long long int x,long long int y)
{
long long int ans = 1;
while(y > 0)
{
if(y % 2 == 1)
{
ans = (ans * x)%1000000007;
}
x = (x * x)%1000000007;
y = y / 2;
}
return ans;
}
int Solution::solve(vector &A) {
vector values;
string temp;
string initial;
long long int turn = 1;
for(long long int i=0;i<A.size();i++)
{
temp = A[i];
initial = A[i];
long long int ind = -1;
for(turn=1;;turn++)
{
for(long long int j=0;j<A[i].size();j++)
{
ind = j - turn;
if(ind < 0)
{
ind = (((A[i].size() - abs(j - turn)%A[i].size()))%A[i].size());
}
A[i][ind] = temp[j];
}
if(A[i] == initial)
{
values.push_back(turn);
break;
}
temp = A[i];
}
}
long long int ans = values[0];
long long int numerator;
long long int denominator;
long long int denominator_inv;
for(long long int i=1;i<values.size();i++)
{
numerator = (values[i] * ans);
denominator = (__gcd(values[i],ans));
denominator_inv = powe(denominator,1000000005);
ans = (numerator * denominator_inv)%1000000007;
}
return ans;
}