### Re: 11371 - Number Theory for Newbies

Posted:

**Fri May 09, 2008 1:32 pm**Thanx so much Sohel,

I've got Accepted

I've got Accepted

The UVa Online Judge board

https://uva.onlinejudge.org/board/

https://uva.onlinejudge.org/board/viewtopic.php?f=41&t=26752

Page **3** of **5**

Posted: **Fri May 09, 2008 1:32 pm**

Thanx so much Sohel,

I've got Accepted

I've got Accepted

Posted: **Fri May 30, 2008 12:23 am**

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

Code: Select all

```
deleted after accepted
```

Posted: **Mon Jun 02, 2008 10:19 am**

Try this input.
-----

Rio

Code: Select all

`1999999999`

Rio

Posted: **Thu Jun 12, 2008 12:28 am**

thaaax rio I get accepted

Posted: **Fri Jun 20, 2008 8:43 am**

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;
```

Posted: **Fri Aug 22, 2008 6:47 pm**

Can anybody tell me why my code is giving me TLE

Code: Select all

```
Deleted
```

Posted: **Fri Aug 22, 2008 8:42 pm**

i don't know actually,

try change the line

while(scanf("%lld" , &val1))

to

while(scanf("%lld" , &val1)!=EOF)

try change the line

while(scanf("%lld" , &val1))

to

while(scanf("%lld" , &val1)!=EOF)

Posted: **Fri Aug 22, 2008 9:52 pm**

Thanks a lot, there was the problem.

Posted: **Sat Aug 23, 2008 1:10 am**

u r welcome

Posted: **Thu Dec 25, 2008 2:06 pm**

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?

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;
}
```

Posted: **Thu Dec 25, 2008 4:57 pm**

Try this inputrehan 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?

Code: Select all

`100`

Code: Select all

`100 - 100 = 0 = 9 * 0`

Posted: **Sat Dec 27, 2008 2:51 pm**

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???????

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???????

Posted: **Sat Dec 27, 2008 4:15 pm**

Oops.. Sorry for confusing..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???????

More test cases..

Code: Select all

```
100
200
300
400
500
```

Code: Select all

```
100 - 100 = 0 = 9 * 0
200 - 200 = 0 = 9 * 0
300 - 300 = 0 = 9 * 0
400 - 400 = 0 = 9 * 0
500 - 500 = 0 = 9 * 0
```

Posted: **Sun Dec 28, 2008 10:33 am**

I am confused about this solution???

This post is from sohle vai.

Some one plz explain.

This post is from sohle vai.

But My question is now if the value is 100 then why the smallest value is 100?????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

Some one plz explain.

Posted: **Sun Dec 28, 2008 4:13 pm**

by rearranging 100, we can get
and both has leading zero.

so the only valid number is 100 and it is the max and min both.

Code: Select all

```
010
001
```

so the only valid number is 100 and it is the max and min both.