when submitted under c++14/c++17 it was giving runtime error but accepted by c++20
#include <bits/stdc++.h>
#define int long long
#define mod 1000000007
using namespace std;
int fast_pow (int b , int p)
{
if(p==0)
return 1;
return p%2 ? (b*(fast_pow((b*b)%mod,p/2)%mod))%mod : fast_pow((b*b)%mod,p/2)%mod ;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
freopen("error.txt","w",stderr);
#endif
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
vector<int> v(n);
for(int &a:v) cin>>a;
string s;
cin>>s;
int i=0,j=n-1;
for(auto val:s){
if(i>=j) break;
if(val=='L') i++;
else j--;
}
int p=v[i];
vector<int> ans;
p%=m;
ans.push_back(p);
for(int k=s.size()-2;k>=0;k--){
if(s[k]=='L'){
i--;
p*=v[i];
p%=m;
}
else{
j++;
p*=v[j];
p%=m;
}
ans.push_back(p);
}
std::reverse(ans.begin(), ans.end());
for(auto val:ans) cout<<val<<" ";
cout<<endl;
}
}
Auto comment: topic has been updated by 15-03-2024 (previous revision, new revision, compare).
The problem is at
s.size()-2
, becauses.size()
is unsigned, it causes overflow, you should cast(int)s.size()
It is showing runtime error due to
#define int long long
.I do not see why people use this shit :/
It's not.