Hi all. I was doing this problem: D - Multiple of 2019 from the Prefix Sums practice problems. My code works for the test cases given in the problem, but fails some of the cases when submitting it to the site. It gives me a runtime error. Can anyone help me fix my code? Thanks!
#include<bits/stdc++.h>
using namespace std;
int main(){
string input;
cin>>input;
int n = input.size();
if(n < 4){
cout<<“0”;
}
else{
int ans=0;
int power[100000];
power[0]=1;
for (int i=1; i<100000; i++){
power[i] = (10power[i-1])%2019;
}
int a[n];
for (int i=0; i<n; i++){
a[i] = int(input[i]) -48;
}
int prefix[n];
prefix[0]=a[0];
for (int i=1; i<n; i++){
prefix[i] = (10prefix[i-1] + a[i])%2019;
}
for (int j=0; j<n; j++){
if (prefix[j]%2019==0){
ans++;
}
}
for (int i=1; i<n; i++){
for (int j=i; j<n; j++){
if((prefix[j]- power[j-i+1] * prefix[i-1])%2019==0){
ans++;
}
}
}
cout<<ans;
}
}
I forgot to mention that I have tried debugging this problem with my own test cases like 12019 and so on. And my code seems to work, but doesn’t work for some of the test cases on the AtCoder website.
Hi all. I was doing this problem: D - Multiple of 2019 from the Prefix Sums practice problems. My code works for the test cases given in the problem, but fails some of the cases when submitting it to the site. It gives me a runtime error. Can anyone help me fix my code? Thanks!
//cpp code here
#include<bits/stdc++.h>
using namespace std;
int main(){
string input;
cin>>input;
int n = input.size();
if(n < 4){
cout<<“0”;
}
else{
int ans=0;
int power[100000];
power[0]=1;
for (int i=1; i<100000; i++){
power[i] = (10 power[i-1])%2019;
}
int a[n];
for (int i=0; i<n; i++){
a[i] = int(input[i]) -48;
}
int prefix[n];
prefix[0]=a[0];
for (int i=1; i<n; i++){
prefix[i] = (10 prefix[i-1] + a[i])%2019;
}
for (int j=0; j<n; j++){
if (prefix[j]%2019==0){
ans++;
}
}
for (int i=1; i<n; i++){
for (int j=i; j<n; j++){
if((prefix[j]- power[j-i+1] * prefix[i-1])%2019==0){
ans++;
}
}
}
cout<<ans;
}
}
Your code still isn’t properly formatted.