Given a positive integer x, construct an integer y such that:
x < yy is divisible by xy is exactly the same as the set of distinct digits appearing in xAny valid answer is accepted.
Constraints:
1 ≤ x < 10^9y ≤ 10^1837
3737
Explanation:
3737 = 37 × 101
The distinct digits in both 37 and 3737 are {3, 7}.
The sample output 7733 is also valid because:
7733 = 37 × 209
The problem allows us to output any valid answer, so we should try to construct one directly.
Concatenate x with itself:
y = xx
For example:
x = 37
y = 3737
If x has n digits, concatenating it with itself gives:
Factor out x:
Therefore, y is always divisible by x.
Because y contains two copies of x, it has exactly the same distinct digit types as x.
Also, since x < 10^9, x has at most 9 digits, so y has at most 18 digits and satisfies y ≤ 10^18.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String x = in.next();
System.out.println(x + x);
}
}
Let n be the number of digits in x.
O(n)O(n)The main type is:
It can also involve:
It is generally not a simulation problem, because there is no process that must be reproduced step by step. We directly build a valid answer using a fixed construction rule.
Recommended label:
Constructive Algorithm + Basic Mathematics + String Manipulation