题目:
Today Tonnnny the monkey learned a new algorithm called Bogo Sort. The teacher gave Tonnnny the code of Bogo sort:
bool is_sorted(int a[], int n) {
for (int i = 1; i < n; i++) {
if (a[i] < a[i – 1]) {
return false;
}
}
return true;
}
void bogo_sort(int a[], int n) {
while (!is_sorted(a, n)) {
shuffle(a, a + n);
}
}
The teacher said the shuffle function is to uniformly randomly permute the array with length , and the algorithm’s expectation complexity is O(n \\cdot n!)O(n⋅n!).
However, Tonnnny is a determined boy — he doesn’t like randomness at all! So Tonnnny improved Bogo Sort. He had chosen one favourite permutation with length , and he replaced the random shuffle with shuffle of , so the improved algorithm, Tonnnny Sort, can solve sorting problems for length array — at least Tonnnny thinks so.
int p
= {…}; // Tonnnny’s favorite permutation of n
void shuffle(int a[], int n) {
int b
;
for (int i = 0; i < n; i++) {
b[i] = a[i]
}
for (int i = 0; i < n; i++) {
a[i] = b[p[i]];
}
}
void tonnnny_sort(int a[], int n) {
assert (n == N); // Tonnnny appointed!
while (!is_sorted(a, n)) {
shuffle(a, a + n);
}
}
Tonnnny was satsified with the new algorithm, and decided to let you give him a different array of length every day to sort it with Tonnnny Sort.
You are the best friend of Tonnnny. Even though you had found the algorithm is somehow wrong, you want to make Tonnnny happy as long as possible. You’re given N,\\ pN, p, and you need to calculate the maximum number of days that Tonnnny will be happy, since after that you can’t give Tonnnny an array that can be sorted with Tonnnny Sort and didn’t appeared before.
The answer may be very large. Tonnnny only like numbers with at most digits, so please output answer mod 10^N instead.
输入描述:
The first line contains one integer N(1≤N≤10^5).The second line contains integer indicating , which forms a permutation of 1, 2, \\cdots, N1,2,⋯,N.
输出描述:
The maximum number of days that Tonnnny will be happy, module 10 ^ N10
N
注意:
首先这题需要高精度;
然后这题需要找到环的长度的;
并且求出这些环长度的最小公倍数。
上代码
#include<bits/stdc++.h>using namespace std;int n,m;int l=1;int sum,tot;int z[100100],aaa[100100],s[1000010],v[1000010],a[1000010];int ans[1000010];void add(int x){int i,j;for(i=1;i<=l;i++) ans[i]*=x;for(i=1;i<l;i++) ans[i+1]+=ans[i]/10,ans[i]%=10;while(l<n&&ans[l]>=10) ans[l+1]+=ans[l]/10,ans[l]%=10,l++;ans[l+1]=0;}int main(){int i,j;for(i=2;i<1e5+10;i++){int f=0,num=0;for(j=1;j*j<=i;j++){if(i%j==0) num++;if(num>1){f=1;break;}}if(f==1) continue;sum++;z[sum]=i;}ans[1]=1;v[1]=1;cin>>n;for(i=1;i<=n;i++) scanf(\"%d\",&a[i]);for(i=1;i<=n;i++)if(v[i]==0){v[i]=1;s[++tot]=1;for(j=a[i];j!=i;j=a[j])v[j]=1,s[tot]++;}for(i=1;i<=tot;i++)for(j=1;j<=sum;j++){int num=0;while(s[i]%z[j]==0) num++,s[i]/=z[j];aaa[j]=max(num,aaa[j]);m=max(j,m);}for(i=1;i<=m;i++)for(j=1;j<=aaa[i];j++)add(z[i]);for(i=l;i>=1;i--) cout<<ans[i]%10;return 0;}
.