You are on page 1of 3

1.

Smallest Multiple in permuted digits


Given two integers N, d , find the smallest number that is a multiple of d that
could be formed by permuting the digits of N. You must use all the digits of N,
and if the smallest multiple of d has leading zeros, they can be dropped. If no
such number exists, output -1.
Input

A line containing two space separated integers, representing N and d.

Output

A single line giving the permutation of N that is the smallest multiple of d,


without any leading zeroes, if any. If not such permutation exists, the output
should be -1

Constraints

1≤N≤1000000000000 

1≤d≤1000000

Example 1

Input 

210 

Output 

12

Example 2

Input 

1707693158 

853684

Output 
513917768

Example 3

Input 

531 

Output 

-1

2. Cheating Exam - Galois Zigzag


You know Galois is a very smart student. He dislikes History exams and you
know that he will find a smart way to cheat. You are the supervisor in the exam
hall for his History exam and you recover from him a sheet of paper that
contains several lines that appear to be in a language not even remotely
resembling English even though the alphabets used are from the English
language. The Principal will not believe you unless you establish beyond doubt,
that the paper recovered from him contains an answer to one of the questions.
You show the paper to the head of Computer Science department and he says
that it is a well-known crypto method and agrees to decrypt the same for you. It
turns out that Galois did the following, He writes the original message in a zig
zag pattern and reads of the lines horizontally. For example,
IAMSMARTERTHANYOU is written first as and encoded as
IEUATROMRTYSAHNMA. Write in V pattern with 5 rows to picturize it. Given the
number of rows used by Galois to encode the text and the encoded text, write a
program to recover the original text. Note that if the length of the string is not
adequate to complete the pattern, Galois pads it with the character "X" to make
up the length. These must not appear in the output.

Input Format: The first line of the input is the number of rows (depth) Galois
used in his coding scheme The next line is the encoded message

Output Format: The decoded message with all the padding characters (if any)
removed.

Constraints: Depth ≤10


Example 1
Input
5
CCEAEOSNDDEYUEHOT
Output
CANYOUDECODETHESE
Example 2
Input
5
WTXHUTXAOHXTBIXAS
Output
WHATABOUTTHIS

You might also like