## 11371 - Number Theory for Newbies

Moderator: Board moderators

### Re: 11371 - Number Theory for Newbies

Thanx so much Sohel,
I've got Accepted
C++ Is The Best.
amr saqr
New poster

Posts: 29
Joined: Tue Mar 11, 2008 6:35 pm

### Re: 11371 - Number Theory for Newbies

i changed my code and modified it but I still get WA

Code: Select all
`deleted after accepted`
Last edited by Mohamed Abd El-Monem on Thu Jun 12, 2008 12:30 am, edited 1 time in total.
Mohamed Abd El-Monem
New poster

Posts: 15
Joined: Mon Mar 31, 2008 1:20 am
Location: Egypt

### Re: 11371 - Number Theory for Newbies

Try this input.
Code: Select all
`1999999999`

-----
Rio

rio
A great helper

Posts: 385
Joined: Thu Sep 21, 2006 5:01 pm
Location: Kyoto, Japan

### Re: 11371 - Number Theory for Newbies

thaaax rio I get accepted
Mohamed Abd El-Monem
New poster

Posts: 15
Joined: Mon Mar 31, 2008 1:20 am
Location: Egypt

### 11371 - Number Theory for Newbies

Code: Select all
`Thanks rio for your input.My code has bug for this input.I took input as a string.I used long double as j to find the number like 1999999999.sum=0;for(i=0,j=1;str[i];i++,j=j*10)sum=sum+str[i]*j;`

lnr
Experienced poster

Posts: 134
Joined: Sat Jun 30, 2007 2:52 pm

### Re: 11371 - Number Theory for Newbies

Can anybody tell me why my code is giving me TLE

Code: Select all
`Deleted`
Last edited by Samiul on Fri Aug 22, 2008 9:51 pm, edited 1 time in total.
Samiul
New poster

Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm

### Re: 11371 - Number Theory for Newbies

i don't know actually,
try change the line
while(scanf("%lld" , &val1))
to
while(scanf("%lld" , &val1)!=EOF)
C++ Is The Best.
amr saqr
New poster

Posts: 29
Joined: Tue Mar 11, 2008 6:35 pm

### Re: 11371 - Number Theory for Newbies

Thanks a lot, there was the problem.
Samiul
New poster

Posts: 36
Joined: Thu Dec 13, 2007 3:01 pm

### Re: 11371 - Number Theory for Newbies

u r welcome
C++ Is The Best.
amr saqr
New poster

Posts: 29
Joined: Tue Mar 11, 2008 6:35 pm

### Re: 11371 - Number Theory for Newbies

can anyone tell me what is the problem with my code.
I have used all necessary data type,but still it is getting WA,why? why? why? why? why? why?
Code: Select all
`(code is done in C)#include <stdio.h>#include <string.h>#include <algorithm>#include <math.h> using namespace std ;int main(){ char a[20],b[20];     long long len,i,j,n,sum,g1,g2,x,y,z;     while(scanf("%s",a)==1)     {        sum=0;       int k=0,p=0,val=10;     len=strlen(a);     sort(&a[0],&a[len]);         for(i=0,j=len-1;i<len;i++,j--)         {         if(a[i]=='0') k++;         else{         p++;sum=sum*val+a[i]-'0'; }         b[j]=a[i];        }         b[len]=NULL;         g1=atol(b);       if(k!=0)      {  z=pow(10,p-1);        x=sum/z;        y=sum%z;       g2=(pow(10,k+p-1))/x+y;      }       else       g2=atol(a);       printf("%lld - %lld = %lld = 9 * %lld\n",g1,g2,g1-g2,(g1-g2)/9);    }return 0;}`
rehan
New poster

Posts: 5
Joined: Thu Dec 25, 2008 1:24 pm

### Re: 11371 - Number Theory for Newbies

rehan wrote:can anyone tell me what is the problem with my code.
I have used all necessary data type,but still it is getting WA,why? why? why? why? why? why?

Try this input

Code: Select all
`100`

Output should be..

Code: Select all
`100 - 100 = 0 = 9 * 0`
helloneo
Guru

Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

### Re: 11371 - Number Theory for Newbies

hi helloneo,
u have told me to take 100 as input
which show "100 - 100 = 0 = 9 * 0"
as output 4 my code also ,but still its getting WA!
can u solve my problem???????
rehan
New poster

Posts: 5
Joined: Thu Dec 25, 2008 1:24 pm

### Re: 11371 - Number Theory for Newbies

rehan wrote:hi helloneo,
u have told me to take 100 as input
which show "100 - 100 = 0 = 9 * 0"
as output 4 my code also ,but still its getting WA!
can u solve my problem???????

Oops.. Sorry for confusing..

More test cases..
Code: Select all
`100200300400500`

Output
Code: Select all
`100 - 100 = 0 = 9 * 0200 - 200 = 0 = 9 * 0300 - 300 = 0 = 9 * 0400 - 400 = 0 = 9 * 0500 - 500 = 0 = 9 * 0`

helloneo
Guru

Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

### Re: 11371 - Number Theory for Newbies

This post is from sohle vai.
There is a greedy approach!

The largest number can be found by sorting the input number in descending order.

The smallest one can be found by sorting in ascending order.. but there could be a case of leading 0. By swapping the smallest non zero digit with the first zero, this can be handled.

Example :
Input --> 12300

Largest number - 32100
Smallest number - 00123 --> 10023

But My question is now if the value is 100 then why the smallest value is 100?????
Some one plz explain.
try_try_try_try_&&&_try@try.com
This may be the address of success.
Obaida
A great helper

Posts: 380
Joined: Wed Jan 16, 2008 6:51 am

### Re: 11371 - Number Theory for Newbies

by rearranging 100, we can get
Code: Select all
`010001`

so the only valid number is 100 and it is the max and min both.
A learner......
shiplu_1320
New poster

Posts: 32
Joined: Sat Dec 29, 2007 9:08 pm
Location: CSEDU , Dhaka

PreviousNext