CodeForces 1344 A Hilberts Hotel 數學

1. 題目描述

1.1. Limit

Time Limit: 1000 ms

Memory Limit: 131,072 kB

1.2. Problem Description

Hilbert’s Hotel is a very unusual hotel since the number of rooms is infinite! In fact, there is exactly one room for every integer, including zero and negative integers. Even stranger, the hotel is currently at full capacity, meaning there is exactly one guest in every room. The hotel’s manager, David Hilbert himself, decides he wants to shuffle the guests around because he thinks this will create a vacancy (a room without a guest).

For any integer kk and positive integer nn , let kmod  nk \mod n denote the remainder when kk is divided by nn. More formally, r=kmod  nr=k \mod n is the smallest non-negative integer suchthat krk−r is divisible by nn. It always holds that 0kmod  nn10 \le k \mod n \le n−1. For example, 100mod  12=4100 \mod 12 = 4 and (1337)mod  3=1(−1337) \mod 3 = 1.

Then the shuffling works as follows. There isan array of nn integers a0,a1,,an1a_0,a_1,\dots,a_{n−1}. Then for each integer kk, the guest in room kk is moved to room number k+akmod  nk+a_k \mod n.

After this shuffling process, determine if there is still exactly one guest assigned to each room. That is, there are no vacancies or rooms with multiple guests.


1.3. Input

Each test consists of multiple test cases. The first line contains a single integer t(1t104)t(1 \le t \le 10^4) — the number of test cases.

Next 2t2t lines contain descriptions of test cases.The first line of each test case contains a single integer (1n2105)(1\le n \le2 \cdot 10^5) — the length of the array.

The second line of each test case contains 𝑛n integers a0,a1,,an1a_0,a_1,\dots,a_{n-1} (109ai109)(−10^9 \le a_i \le 10^9).

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.


1.4. Output

For each test case, output a single line containing “YES” if there is exactly one guest assigned to each room after the shuffling process, or “NO” otherwise. You can print each letter in any case (upper or lower).


1.5. Sample Input

6
1
14
2
1 -1
4
5 5 5 1
3
3 2 1
2
0 1
5
-239 -2 -100 -3 -11

1.6. Sample Output

YES
YES
YES
NO
NO
YES

1.7. Notes

In the first test case, every guest is shifted by 1414 rooms, so the assignment is still unique.

In the second test case, even guests move to the right by 11 room, and odd guests move to the left by 11 room. We can show that the assignment is still unique.

In the third test case, every fourth guest moves to the right by 11 room, and the other guests move to the right by 55 rooms. We can show that the assignment is still unique.

In the fourth test case, guests 00 and 11 are both assigned to room 33 .

In the fifth test case, guests 11 and 22 are both assigned to room 22 .

1.8. Source

CodeForces 1344 A Hilbert’s Hotel


2. 解讀

計算 ((an+n)%n+n)%n((a_n + n) \%n + n ) \% nn[0,n1]n \in [0, n - 1],判斷其結果是否有重複,用 set 存儲後,比較 set.size()nn 的大小即可。

因爲 aia_i 的範圍爲(109ai109)(−10^9 \le a_i \le 10^9)

  • an+na_n + n爲正數時,結果爲 (an+n)%n(a_n + n)\%n

  • an+na_n + n爲負數時,結果爲 n+(n+an)%nn + (n + a_n)\%n

將正負數的情況統一起來,結果爲((an+n)%n+n)%n((a_n + n) \%n + n ) \% n

3. 代碼

#include <iostream>
#include <set>
#include <string.h>
using namespace std;

// 集合存儲
set<long long> st;

int main()
{
    // test case
    int t;
    // 個數
    long long n;
    long long buffer;
    scanf("%d", &t);
    // test case
    for (int i = 0; i < t; i++) {
        scanf("%lld", &n);
        // 初始化
        st.clear();
        // 輸入
        for (long long j = 0; j < n; j++) {
            scanf("%lld", &buffer);
            st.insert(((j + buffer) % n + n) % n);
        }

        printf("%s\n", (long long)st.size() == n ? "YES" : "NO");
    }
}

聯繫郵箱:[email protected]

Github:https://github.com/CurrenWong

歡迎轉載/Star/Fork,有問題歡迎通過郵箱交流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章