My issue
My approach is to make a vector of size n+1 with first element as 1 and store the multiplication of given index in that vector. Since the constraint of n is 10^5 and of a[i] is 100, the multiplication will go till 10^7.then I will just divide the vector’s rth element with (l-1)th element and taking modulo of it I will get my answer. But the submission tells runtime error I don’t know why
My code
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
// your code goes here
int n;
cin>>n;
vector<int> a(n);
for(int i=0;i<n;++i) cin>>a[i];
vector<int> ans( n+1 , 1);
ans[0]=1;
for(int i=0;i<n;++i) ans[i+1]=ans[i]*a[i];
int t;
cin>>t;
while(t--){
int l,r,m;
cin>>l>>r>>m;
int final=(ans[r]/ans[l-1]);
final%=m;
cout<<final<<"\n";
}
return 0;
}
Problem Link: Chef and Segments Practice Coding Problem - CodeChef