CodeForces 1344 A Hilbert's Hotel#
目录#
1. 题目描述#
1.1. Limit#
Time Limit: 1000 ms
Memory Limit: 256 MB
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");
}
}
联系邮箱:curren_wong@163.com
Github:https://github.com/CurrenWong
欢迎转载/Star/Fork,有问题欢迎通过邮箱交流。