文章目錄
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 and positive integer , let denote the remainder when is divided by . More formally, is the smallest non-negative integer suchthat is divisible by . It always holds that . For example, and .
Then the shuffling works as follows. There isan array of integers . Then for each integer , the guest in room is moved to room number .
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 — the number of test cases.
Next lines contain descriptions of test cases.The first line of each test case contains a single integer — the length of the array.
The second line of each test case contains 𝑛n integers .
It is guaranteed that the sum of over all test cases does not exceed .
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 rooms, so the assignment is still unique.
In the second test case, even guests move to the right by room, and odd guests move to the left by room. We can show that the assignment is still unique.
In the third test case, every fourth guest moves to the right by room, and the other guests move to the right by rooms. We can show that the assignment is still unique.
In the fourth test case, guests and are both assigned to room .
In the fifth test case, guests and are both assigned to room .
1.8. Source
CodeForces 1344 A Hilbert’s Hotel
2. 解讀
計算 ,,判斷其結果是否有重複,用 set
存儲後,比較 set.size()
和 的大小即可。
因爲 的範圍爲。
-
爲正數時,結果爲 。
-
爲負數時,結果爲 。
將正負數的情況統一起來,結果爲。
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,有問題歡迎通過郵箱交流。