A string ss of length nn can be encrypted by the following algorithm:
- iterate over all divisors of nn in decreasing order (i.e. from nn to 11),
- for each divisor dd, reverse the substring s[1…d]s[1…d] (i.e. the substring which starts at position 11 and ends at position dd).
For example, the above algorithm applied to the string ss="codeforces" leads to the following changes: "codeforces" →→ "secrofedoc" →→ "orcesfedoc" →→"rocesfedoc" →→ "rocesfedoc" (obviously, the last reverse operation doesn't change the string because d=1d=1).
You are given the encrypted string tt. Your task is to decrypt this string, i.e., to find a string sssuch that the above algorithm results in string tt. It can be proven that this string ss always exists and is unique.
The first line of input consists of a single integer nn (1≤n≤1001≤n≤100) — the length of the string tt. The second line of input consists of the string tt. The length of tt is nn, and it consists only of lowercase Latin letters.
Print a string ss such that the above algorithm results in tt.
10 rocesfedoc
codeforces
16 plmaetwoxesisiht
thisisexampletwo
1 z
z
The first example is described in the problem statement.
题意:给一个长度为n的字符串,经过他的因子个数的几次翻转,让你求最初的字符串。
题解:模拟 那就从2开始判断是不是n的因子,是的话,就翻转从前面到因子的字符串
#include<bits/stdc++.h> using namespace std; int b[110],cnt,n; string s,a; int main() { cin>>n>>s; for(int i=2; i<=n; i++) if(n%i==0) b[++cnt]=i; for(int i=1; i<=cnt; i++) for(int j=0; j<b[i]/2; j++) swap(s[j],s[b[i]-j-1]); cout<<s<<endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n;char s[110]; cin>>n>>s; for(int i=2;i<=n;i++) if(n%i==0) reverse(s,s+i); cout<<s<<endl; }
n=int(input()) s=input() for i in range(2,n+1): if n%i==0: s=s[i-1::-1]+s[i:] print(s)