Codeforces 986B - Petr and Permutations

Description\text{Description}

Given an array a[], swap random 2 number of them for 3n or (7n+1) times.\text{Given an array }a[],\text{ swap random 2 number of them for }3n\text{ or }(7n+1)\text{ times.}
Please compute how many times for swaping.\text{Please compute how many times for swaping.}

Solution\text{Solution}

It’s easy to know that if we keep swaping a[i] and a[a[i]], they’ll always\text{It's easy to know that if we keep swaping }a[i]\text{ and }a[a[i]],\text{ they'll always}
goes a[i]=i at last.\text{goes }a[i]=i\text{ at last.}
Counting times we’ve done that so that i,a[i]=i. Check its parity.\text{Counting times we've done that so that }\forall i,a[i]=i.\text{ Check its parity.}

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>

int n;
int a[1000010];
int ans=0;

int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
		scanf("%d",&a[i]);
	for(int i=1;i<=n;++i)
		while(a[i]!=i){
			std::swap(a[i],a[a[i]]);
			++ans;
		}
	if(ans%2==n%2) puts("Petr");
	else puts("Um_nik");
}
posted @ 2019-05-01 09:45  TeacherDai  阅读(99)  评论(0)    收藏  举报