First of all, I am not sure if this is the right way to ask, but in case it is, I am stuck on a problem and don't have a clue how to solve it. I would like to get some hints :).
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 150 |
First of all, I am not sure if this is the right way to ask, but in case it is, I am stuck on a problem and don't have a clue how to solve it. I would like to get some hints :).
Name |
---|
Try breaking the problem down into two parts: figuring out if a number can be created to be divisible by 10, and seeing if the number can be divisible by 9. If both of these are true, then the number is also divisible by 90.
$$$90 = 9 \times 10$$$. So a number will be divisible by $$$90$$$ if and only if it's divisible by $$$9$$$ and $$$10$$$.
A number will be divisible by $$$10$$$ if it has a trailing $$$0$$$. A number will be divisible by $$$9$$$, if the sum of it's digits are divisible by $$$9$$$.
After taking input, count the number of $$$5$$$'s (we denote it by $$$f$$$). The number of $$$0$$$'s will be then $$$z = n - f$$$.
Remember we need the number to be divisible by both $$$9$$$ and $$$10$$$. So if $$$f = n$$$ (or $$$z = n - f = 0$$$), then there is no solution. Print $$$-1$$$.
At this point we know, there is at least one $$$0$$$. So we can construct a number that is divisible by $$$10$$$. The other condition was that the number has to divisible by $$$9$$$. Although the $$$0$$$'s sum up to $$$0$$$ and $$$0$$$ is divisible by $$$9$$$, we can't place leading $$$0$$$'s. So in order to satisfy the divisibility by $$$9$$$ constraint, we have to place a number of $$$5$$$'s to the left of $$$0$$$(or $$$0$$$'s) such that the number of $$$5$$$'s sum up to a number that's divisible by $$$9$$$.
How many $$$5$$$'s are needed to make the sum of $$$5$$$'s divisible by $$$9$$$? Notice that $$$\text{lcm}(5, 9) = 45$$$. So we need at least $$$9$$$ $$$5$$$'s to make the sum of digits divisible by $$$9$$$. In other words, let $$$f^\prime$$$ be the number of $$$5$$$'s that sum up to a number that's divisible by $$$9$$$. Then $$$f^\prime$$$ has to be a multiple of $$$9$$$. We have already counter the number of $$$5$$$'s (denoted by $$$f$$$). So we have to find the multiple of $$$9$$$ that is closest to $$$f$$$. We can find it by, $$$f^\prime = \lfloor \frac{f}{9} \rfloor \times 9 $$$. Think about as to why it's true.
We place $$$f^\prime$$$ number of $$$5$$$'s at the start. Now are we done? Almost. We already have a trailing $$$0$$$ and a bunch of $$$5$$$'s to its left. To make the number as large as possible, we place the remaining $$$0$$$'s (if there are any), between the $$$5$$$'s and the trailing $$$0$$$.
Submission: 90701750