Moderator: Board moderators

dotnet12
New poster
Posts: 4
Joined: Fri Aug 17, 2012 9:43 pm

### 10994- Need help

The solution is correct but it exceeds time limit so what can I do about it? Please tell me anyone. Here is my solution:

#include <stdio.h>

int fu(int ab)
{

if(ab%10>0 && ab>0)
{
return ab%10;
}
else
{
return ab/10;
}
}

int S(int a,int b)
{
int ans=0,i;
for(i=a;i<=b;i++)
{
ans+=fu(i);

}
printf("%d", ans);
return 0;
}

int main()
{
int in1,in2,ans;
while((scanf("%d %d",&in1,&in2)==2) && (in1!=-1 && in2!=-1))
{

if(in1>in2)
{
S(in2,in1);

}
else
{
S(in1,in2);

}

}
return 0;

}

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: #10994- Need help

If you were asked to simply sum all the integers between p and q, would you loop? Is there a faster way?
Check input and AC output for thousands of problems on uDebug!

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 10994- Need help

Don't double post.
Check input and AC output for thousands of problems on uDebug!

triplemzim
New poster
Posts: 48
Joined: Sat Apr 06, 2013 6:02 pm

### Re: #10994- Need help

Code: Select all

``````#include<stdio.h>
#include<stdlib.h>
#include<string.h>

long dp;

long func(long n)
{
if(n==0) return 0;
if(dp[n]!=-1) return dp[n];
if((n/10)*10==n) func(n/10);
else return dp[n]=n%10;
//return dp[n];
}

int main()
{
int p,q;
long sum;
//    for(sum=0;sum<1000001;sum++)
//        dp.push_back(-1);
memset(dp,-1,sizeof(dp));

while(scanf("%d%d",&p,&q)==2)
{
if(p<0 && q<0) break;
sum=0;

for(int i=p;i<=q;i++)
sum+=func(i);
printf("%ld\n",sum);
//       cout<<sum<<endl;
}
return 0;
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: #10994- Need help

Click My Submissions and you can see the reason for your compile error.
Check input and AC output for thousands of problems on uDebug!

triplemzim
New poster
Posts: 48
Joined: Sat Apr 06, 2013 6:02 pm

### Re: #10994- Need help

i have seen the cause of compilation error and this time i got runtime error... here is my updated code:

Code: Select all

``````#include<stdio.h>
#include<stdlib.h>
#include<string.h>

long dp;

long func(long n)
{
if(n==0) return 0;
if(dp[n]!=-1) return dp[n];
if((n/10)*10==n) func(n/10);
else return dp[n]=n%10;
}

int main()
{
int p,q,i;
long sum;
memset(dp,-1,sizeof(dp));

while(scanf("%d%d",&p,&q)==2)
{
if(p>q)
{
i=q;
q=p;
p=i;
}
if(p<0 && q<0) break;
sum=0;

for(i=p;i<=q;i++)
sum+=func(i);
printf("%ld\n",sum);

}
return 0;
}
``````

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: #10994- Need help

What if q is larger than 1000000?
Check input and AC output for thousands of problems on uDebug!

holdonasec
New poster
Posts: 11
Joined: Tue Aug 05, 2014 9:18 am

### Re: 10994 - Simple Addition - TLE

Hello,

I got some TLE with my code which I think It's quite fast and can handle over 100 maximum test cases like 0 2147483647...but still TLE!

Code: Select all

``````// Author: Hoang Duc Viet (ducviet321)
#include <fstream>
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>

using namespace std;

I've got AC :D

Look at brainfry713's suggestion below
``````
Any suggestion? Thanks! Last edited by holdonasec on Sat Jan 10, 2015 3:58 pm, edited 1 time in total.

brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

### Re: 10994 - Simple Addition

print F(q) - F(p - 1)
Check input and AC output for thousands of problems on uDebug!

holdonasec
New poster
Posts: 11
Joined: Tue Aug 05, 2014 9:18 am

### Re: 10994 - Simple Addition

brianfry713 wrote:print F(q) - F(p - 1)
Thanks for your suggestion Finally got AC! mdminhaj329
New poster
Posts: 2
Joined: Thu Apr 23, 2015 7:25 am

### Re: 10994 - Simple Addition

i don't know why wrong ans,i test 100 test case with uva toolkit....

Code: Select all

``````#include<stdio.h>
#include<math.h>
#include<ctype.h>

long long sumf,sff;
long long s(long long p,long long q){
long long summ=0,i,n,pf,qf,pr,qr,s1,s2;
sff=0;
pf=(p/10);
pf=pf*10;
pr=p%10;

s1=((pr-1)*(pr-1) +(pr-1))/2;

qf=(q/10);
qf=qf*10;
qr=q%10;

if(p%10!=0){
sff=(p/10)%10;
}

s2=(qr*qr+qr)/2;

summ=((qf-pf)/10)*45+s2-s1-sff;
sumf=sumf+summ;
pf=(p/10);
qf=(q/10);
if(qf==0)
return sumf;
else
s(pf,qf);

}

int main(){
long long x,y,a,am,b,bm;
while(1){
scanf("%lld %lld",&x,&y);
sumf=0,sff=0;
if(x==-1 && y==-1)
break;
else if(x<=y && x>0){
sumf=s(x,y);

printf("%lld\n",sumf);
}

}

return 0;
}``````
Last edited by brianfry713 on Fri Jun 19, 2015 6:59 am, edited 1 time in total.