Here is the link to the problem: Aladin on Kattis.comProblemAladin was walking down the path one day when he found the strangest  thing: N empty boxes right next to a weird alien machine. After a bit  of fumbling around he got the machine to do something. The machine now  accepts 4 integers \$L\$, \$R\$, \$A\$ and \$B\$. After that hitting the big red  glowing button labeled NE DIRAJ1 causes the machine to go crazy and  follow the next routine:Set the number of stones in the box labeled L to A modulo B.It proceeds to fly to the box labeled L + 1, and set the number of stones there to (2  A) mod B.It procedes to fly to the box labeled L + 2, and set the number of stones there to (3  A) modB.Generally, it visits each box labeled between L and R, and set the number of stones there to ((X  L + 1)  A) mod B, where X is the box label.After it visits the box labeled R. It settles down for further instructions.During the game Aladin wonders what is the total number of stones in some range of boxes.Write a program that simulates the device and answers Aladin's questions.InputThe first line contains two integers: the number of boxes \$N\$ (\$1 \le N \le 1000000000\$) and the number of queries \$Q\$ (\$1 \le Q \le 50000\$).The next \$Q\$ lines contain information about the simulation.If the line starts with 1, than it follows the format 1 L R A B  (\$1 \le L \le R \le N\$) (\$1 \le A, B \le 1000000\$), meaning that Aladin keyed in numbers \$L\$, \$R\$, \$A\$ and \$B\$ in the device and allowed the device to do its job.If the line starts with 2, then it follows the format 2 L R  (\$1 \le L \le R \le N\$), meaning that Aladin wonders how many stones in total are  ther stones are in boxes labeled \$L\$ to \$R\$ (inclusive).OutputFor each query beginning with 2 output the answer to that particular query. Queries should be processed in the order they  are given in the input.Sample Input 16 32 1 61 1 5 1 22 1 6Sample Output 103Sample Input 24 51 1 4 3 42 1 12 2 22 3 32 4 4Sample Output 23210Sample Input 34 41 1 4 7 92 1 41 1 4 1 12 1 4Sample Output 3160#include<iostream>#include<fstream>#include<string>using namespace std;long countStones(long, long, long, long, long, long);int main(int __argc, char *__argv[]){    std::string filepath;    long N, Q, *boxes;  if(__argc == 2) {    filepath = std::string(__argv[1]);  }  std::ifstream* infile = !filepath.empty() ? new std::ifstream(filepath.c_str()) : static_cast<std::ifstream*>(&std::cin);  *infile >> N >> Q;  long opt, L1 = 0, R1 = 0, L2 = 0, R2 = 0, A, B;  for(long i = 1; i <= Q; i++) {    *infile >> opt;    if(opt == 1) {      *infile >> L1 >> R1 >> A >> B;    }    if(opt == 2) {      *infile >> L2 >> R2;      cout << countStones(L1, R1, L2, R2, A, B) << endl;    }  }    return 0;}long countStones(long L1, long R1, long L2, long R2, long A, long B) {  long ret = 0, start = L1, end = R1;  if(R2 < L1) return 0;  if(L2 > R1) return 0;  if (L2 > L1) start = L2;  if (R2 < R1) end = R2;  for(long i = start; i <= end; i++) {    ret += ((i - L1 + 1) * A) % B;  }  return ret;}The problem works fine with all the with Sample Cases #1, #2, & #3. But when I upload the code on the website (open.kattis.com) it fails on the case #4, which is unknown to me. I couldn't find a logical error in my code. Also, when I had written the code which was storing the modulo calculations for each box (array) the problem did pass the case #4 but failed #5 (Memory Exceeded). So I changed the code to NOT STORE the values in the array & just calculate on the fly & add it to the count of stones.I need to modify the code so that it passes Test #4, whose input values are unknown to me. That is to find the logical error in the code.
Summation of Arithmetic Progression Modulo Series
c++;algorithm;programming challenge