Bella ciao long challenge What is wrong in my solution

#include
#include
#include
#include
#define ll long long
using namespace std;

int main(){
int t;
cin>>t;
while(t–){
int D,d,p,q,s,rate,count=0,total=0;
cin>>D>>d>>p>>q;
int arr[D];
for(int i=0;i<D;i++){
arr[i]=p;
}
//for(int i=d;i<D;i++){
// arr[i]+=q;
//}
for(int i=1;i<=D;i++){
s=i*d;
if(s<=D){
for(int i=s;i<D;i++){
arr[i]+=q;
}
}
else{
count++;
}
}
for(int i=0;i<D;i++){
total+=arr[i];
}
cout<<total<<endl;

}
return 0;
}

I did something just like this. Just try further and make up a formula

Getting TLE right?
Try to do in O(1) time complexity
By making formula (Sum of n terms of AP)

(post withdrawn by author, will be automatically deleted in 24 hours unless flagged)

Bro my all test cases were passed but while submitting it… It showed wrong submition

Try to dry run the code. You will catch exception cases easily. In problems like these, it is better to make a formula for different types of cases.

Hint : value of D/d plays an important role

Passing test cases doesn’t mean it will run with every possible value. Look for values that will generate a runtime error.

When will we get an updated rating?

Wait for a few hours

oh okay, thanks

Yes when I placed ll int instead of just int my submition passed partially… But later it showed TLE.

Actually brother there is no need for loops here.
I solved it like this

#include <bits/stdc++.h>
using namespace std;

int main() {
// your code goes here
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long t;
cin>>t;
while(t–){
long long int D,d,p,q;
cin>>D>>d>>p>>q;
long long di = D/d;
long long rem = D%d;
long long sum=0;
sum = (di*(2p+(di-1)q)(d))/2;
sum += rem
(p+q*di);
cout<<sum<<endl;
}
return 0;
}

3 Likes

Your algorithm has a time complexity of O(n^2) I think, it’s hard to read the code. And since this problem can be solved with O(1) complexity, the time limit has been set very low.