-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReversingEncryption.java
47 lines (44 loc) · 1.18 KB
/
ReversingEncryption.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*https://codeforces.com/problemset/problem/999/B*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.ArrayList;
public class ReversingEncryption
{
public static void main(String[] args)throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(reader.readLine());
ArrayList<Integer> divisors = new ArrayList<Integer>();
StringBuffer str = new StringBuffer(reader.readLine());
int i;
for (i = 1; i < Math.sqrt(n); ++i)
{
if (n%i == 0)
{
divisors.add(i);
StringBuffer temp = new StringBuffer(str.substring(0,i));
temp.reverse();
temp.append(str.substring(i));
str = temp;
}
}
if (Math.sqrt(n) == (int)Math.sqrt(n))
{
i = (int)Math.sqrt(n);
StringBuffer temp = new StringBuffer(str.substring(0,i));
temp.reverse();
temp.append(str.substring(i));
str = temp;
}
for (int j = divisors.size()-1; j >= 0; --j)
{
i = n/(Integer)divisors.get(j);
StringBuffer temp = new StringBuffer(str.substring(0,i));
temp.reverse();
temp.append(str.substring(i));
str = temp;
}
System.out.println(str);
}
}