1. 程式人生 > >CodeForces ~ 987E ~ Petr and Permutations (思維,有趣)

CodeForces ~ 987E ~ Petr and Permutations (思維,有趣)

題意:有n個數字,原始序列為1~n,給你該序列經過Petr或Um_nik操作後序列,問你是誰操作的?

Petr:隨機交換兩個數的位置,交換3n次。

Um_nik: 隨機交換兩個數的位置,交換7n+1次。

思路:可以發現3n和7n+1一定是一奇一偶的,那麼我看考慮這個序列跟操作次數奇偶的性質,然後我就想不出來啦,哈哈哈。第二天看了別人的題解才會。

考慮兩個數字的時候我們交換 2x 次跟沒交換一樣,交換2x+1次才有效。n個數字的時候,交換3n或7n+1次同樣有很多沒有用的操作。假設有效操作為cnt,那麼剩下的都是無效操作,無效操作一定為2x次(即偶數次),因為奇數次操作一定會引起兩個數位置的變化。如果(3n-cnt)為偶數就是Petr操作的,如果(7n+1-cnt)為偶數那麼就是Um_nik操作的。

所以我們計算cnt就好了,其實cnt就是我們把這個序列每次交換兩個數字,變為原序列,有效交換的次數。

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000005;
int n, a[MAXN], cnt;
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)
        {
            swap(a[i], a[a[i]]);
            cnt++;
        }
    }
    if ((3*n-cnt)&1) printf("Um_nik\n");
    if ((7*n+1-cnt)&1) printf("Petr\n");
    return 0;
}
/*
5
2 4 5 1 3
*/