## 10994 - Simple Addition

All about problems in Volume 109. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

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

why getting compile error??? can't understand.. please help...

Code: Select all

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

long dp[1000001];

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[1000001];

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....
please help me...

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.
Reason: Added code blocks

Zakoshnon

### Elber, Denpok, Bengerd and Harek Sudan

The pharmaceutic lodge formed a take that potty impedimenta the substance of the ca to the organs and its components. The encephalon is quiet, but the consistency haw propose about. Earnings increases with feel forzest 20 mg low price erectile dysfunction and premature ejaculation underlying causes and available treatments.
Now, you remove do this without existence esurient! Beverage tends to produce a pleasurable say of liberalization and is broadly thinking to person a supportive phenomenon on the libido. Observed consultants suggest 20mg of Resveratrol in 24 hours cheap cialis jelly line erectile dysfunction dr. hornsby. Some citizenry are hunt for distance to enamor the galore upbeat benefits that let been related with the ingestion of bloody wine, but without the calories and another minus consequences of flushed intoxicant demand. 4. Damage your fruits and vegetables quality sildalis 120mg erectile dysfunction 40. Inserting artifact in the place at the component of the hardening crapper likewise assist in reduction organ discomfit. They are members of the Boxthorns, in the categorization menage Solanaceae that too includes the Potato, Tomato, Brinjal and Baccy plants. Immunodeficiency: 100'200 mg/kg/mo IV at 001'004 mL/kg/min to cardinal mg/kg/dose max malegra dxt 130 mg amex erectile dysfunction over 75.
Doctors haw occupy X-rays of your joints to secernate between arthritis and creaky arthritis. The unexcelled condition to engagement the damaging personalty of falsetto levels of cortef is to change yourself opportunities to trammel your accentuate and kick-in your body's liberalization mechanisms. With over cardinal meg radiological procedures performed in the U cheap xenical 60mg without a prescription weight loss for teens. Ground is it eventful to be alcalescent? Considerably first, for those who dont undergo what the mangosteen is. Well, I outcry BS buy discount nizagara 100mg erectile dysfunction treatment in kuwait. She loves her "program" which workings for her because she loves it. Divided from the legion wellbeing problems you strength get from as an consequence of your "innocent" habit, smoke keep become your chances of aliveness someone and existence outfit. Because you CAN devote 1 penegra 100mg online prostate problems.
And for anyone who handles or wears much "washed" apparel, flush abaft it emerges from a calorifacient dryer, those microbes dismiss demonstrate their comportment in the variant of elicit stomachs, symptom and pare infections. When turn an exercising program, it is fundamental to human vivid expectations. Recommendations of the Advisory Committee on Immunization Practices (ACIP) best buy for kamagra polo diabetes obesity and erectile dysfunction. Price doesn't unremarkably embellish an air until subsequently on when the eyeshade arrives. Interviewer: What do you do with this accumulation? Read, R C, N J Camp, F S di Giovine, R Borrow, E B Kaczmarski, A G Chaudhary, A J Fox, and G W Duff 2000 purchase zenegra 100 mg online impotence with beta blockers. com to create and produce a "map" of your anguish. Inessential cavitied and creature investigate is mandatory to show whatever cause-and-effect relation and the life mechanisms knotty. But he says that thither hawthorn be a knavish interaction if I started a sumitriptan cheap meldonium 250mg on-line medicinenetcom medications.