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 k k k and positive integer n n n , let k m o d    n k \mod n kmodn denote the remainder when k k k is divided by n n n. More formally, r = k m o d    n r=k \mod n r=kmodn is the smallest non-negative integer suchthat k − r k−r kr is divisible by n n n. It always holds that 0 ≤ k m o d    n ≤ n − 1 0 \le k \mod n \le n−1 0kmodnn1. For example, 100 m o d    12 = 4 100 \mod 12 = 4 100mod12=4 and ( − 1337 ) m o d    3 = 1 (−1337) \mod 3 = 1 (1337)mod3=1.

Then the shuffling works as follows. There isan array of n n n integers a 0 , a 1 , … , a n − 1 a_0,a_1,\dots,a_{n−1} a0,a1,,an1. Then for each integer k k k, the guest in room k k k is moved to room number k + a k m o d    n k+a_k \mod n k+akmodn.

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 ( 1 ≤ t ≤ 1 0 4 ) t(1 \le t \le 10^4) t(1t104) — the number of test cases.

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

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

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10^5 2105.


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 14 14 14 rooms, so the assignment is still unique.

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

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

In the fourth test case, guests 0 0 0 and 1 1 1 are both assigned to room 3 3 3 .

In the fifth test case, guests 1 1 1 and 2 2 2 are both assigned to room 2 2 2 .

1.8. Source

CodeForces 1344 A Hilbert’s Hotel


2. 解读

计算 ( ( a n + n ) % n + n ) % n ((a_n + n) \%n + n ) \% n ((an+n)%n+n)%n n ∈ [ 0 , n − 1 ] n \in [0, n - 1] n[0,n1],判断其结果是否有重复,用 set 存储后,比较 set.size() n n n 的大小即可。

因为 a i a_i ai 的范围为 ( − 1 0 9 ≤ a i ≤ 1 0 9 ) (−10^9 \le a_i \le 10^9) (109ai109)

  • a n + n a_n + n an+n为正数时,结果为 ( a n + n ) % n (a_n + n)\%n (an+n)%n

  • a n + n a_n + n an+n为负数时,结果为 n + ( n + a n ) % n n + (n + a_n)\%n n+(n+an)%n

将正负数的情况统一起来,结果为 ( ( a n + n ) % n + n ) % n ((a_n + n) \%n + n ) \% n ((an+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");
    }
}

联系邮箱:curren_wong@163.com

Github:https://github.com/CurrenWong

欢迎转载/Star/Fork,有问题欢迎通过邮箱交流。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值