problem

A. Hilbert’s Hotel
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 and positive integer n, let kmodn denote the remainder when k is divided by n. More formally, r=kmodn is the smallest non-negative integer such that k−r is divisible by n. It always holds that 0≤kmodn≤n−1. For example, 100mod12=4 and (−1337)mod3=1.

Then the shuffling works as follows. There is an array of n integers a0,a1,…,an−1. Then for each integer k, the guest in room k is moved to room number 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.

Input
Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. Next 2t lines contain descriptions of test cases.

The first line of each test case contains a single integer n (1≤n≤2⋅105) — the length of the array.

The second line of each test case contains n integers a0,a1,…,an−1 (−109≤ai≤109).

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

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).

Example
inputCopy
6
1
14
2
1 -1
4
5 5 5 1
3
3 2 1
2
0 1
5
-239 -2 -100 -3 -11
outputCopy
YES
YES
YES
NO
NO
YES
Note
In the first test case, every guest is shifted by 14 rooms, so the assignment is still unique.

In the second test case, even guests move to the right by 1 room, and odd guests move to the left by 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 room, and the other guests move to the right by 5 rooms. We can show that the assignment is still unique.

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

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

solution

/*
+ 题意:给出一个长为n的数组。现在将每个数k移至(k+a[k])%n,判断新产生的n个数是否会冲突
+ 思路:用set模拟,每次都加进去,最后既没有重复个数也相同就行。
*/
#include<bits/stdc++.h>
using namespace std;
int main(){int T;  cin>>T;while(T--){int n;  cin>>n;set<int>se;for(int i = 0; i < n; i++){int x;  cin>>x;se.insert(((i+x)%n+n)%n);}if(se.size()==n)cout<<"YES\n";else cout<<"NO\n";}return 0;
}

【Codeforces 1344 A】Hilbert‘s Hotel,哈希表,set维护相关推荐

  1. C. Hilbert's Hotel

    链接:https://codeforces.ml/contest/1345/problem/C Hilbert's Hotel is a very unusual hotel since the nu ...

  2. 【CodeForces】961 F. k-substrings 字符串哈希+二分

    [题目]F. k-substrings [题意]给定长度为n的串S,对于S的每个k-子串$s_ks_{k+1}...s_{n-k+1},k\in[1,\left \lceil \frac{n}{2} ...

  3. PHP内核中的哈希表结构

    https://github.com/HonestQiao/tipi/commit/17ca680289e490763a6a402f79afa2a13802bb36 下载:https://github ...

  4. 【C#】集合_哈希表_字典_泛型_文件

    数组能做到:存放同种类型数据,且数据个数确定 object类型的数组能满足:放各种类型的数据,确定放多少个,但是随意插入元素,数组做不到 集合能做到:存放各种数据类型,且不确定存放多少个,能做到随意插 ...

  5. PHP哈希表碰撞攻击原理

    哈希表碰撞攻击(Hashtable collisions as DOS attack)的话题不断被提起,各种语言纷纷中招.本文结合PHP内核源码,聊一聊这种攻击的原理及实现. 哈希表碰撞攻击的基本原理 ...

  6. 在C#中应用哈希表(Hashtable)

    一,哈希表(Hashtable)简述 在.NET Framework中,Hashtable是System.Collections命名空间提供的一个容器,用于处理和表现类似key/value的键值对,其 ...

  7. 自己写的哈希表以及解决哈希冲突

    哈希表就是键值key-value对,使用hash函数让key产生哈希值,当不同的key产生相同的哈希值时就是哈希冲突了,产生哈希冲突可以使用拉链法. hash.c #include <stdio ...

  8. 用开放地址法中的线性探查法解决冲突实现哈希表的运算

    为了更深的理解哈希算法,自己写了用开放地址法中的线性探查法解决冲突实现哈希表的运算. /*** Created by lirui on 14-8-13.* 用开放地址法中的线性探查法解决冲突实现哈希表 ...

  9. 散列表查找(哈希表)

    散列函数构造经常要考虑: 1.散列表的长度 2.关键字的长度 3.关键字的分布情况 4.计算散列函数所需的时间 5.记录的查找频率 一个"好"的散列函数应遵循一下两条原则 (1)函 ...

最新文章

  1. Android -- 短信
  2. lua-nginx-module directives 中文版
  3. clickhouse原理解析与应用实践_Hybrid App (混合应用) 技术全解析 方案原理篇
  4. redis之 zadd、zremrangebyscore、zremrangebyrank、zscore、zcard、zcount、zrangebyscore、zinczrevrankzrevrange
  5. 抖音下拉框中的下拉词是怎么出来的?
  6. 鸿蒙移植stm32,韦东山老师要移植鸿蒙的那个STM32MP157具体是什么?
  7. php ckeditor 使用教程,织梦实现使用默认编辑器ckeditor上传视频功能( 详细)
  8. 冲刺第一天 11.23 FRI
  9. oracle数据布尔,Oracle中的布尔字段
  10. strongswan与sangfor的ikev2配置
  11. 极值点偏移问题的处理策略及探究(作业帮的毕冶老师总结)
  12. 关于“TCP segment of a reassembled PDU”
  13. 士兵杀敌(coduck)
  14. 数据服务器怎么进行日常维护
  15. android 获取 meid 代码,Android 各个版本获取IMEI、MEID
  16. 4.16 使用可选颜色命令调整图像色彩 [原创Ps教程]
  17. pes2020服务器维护中,【重磅】PES2021删卡时刻/传奇及国际服补偿一览
  18. 20元充电宝 身边的炸弹?选购留意电芯
  19. 常见的USB接口种类以及区别
  20. 写字机器人制作教程2.0

热门文章

  1. 机器学习基础(四十四)—— 优化
  2. http status code —— http 状态码
  3. 搜索——二分搜索实现及细节
  4. java反射获取泛型_如何通过Java反射获取泛型类型信息
  5. sql where中用case_一份虐你千百遍的SQL语句面试题,请笑纳
  6. python是什么类型的编程语言-Python是一种什么样的编程语言?解释?编译?汇编?机械?...
  7. python从入门到精通需要多久-Python 从入门到精通:一个月就够了!
  8. python画条形图-python使用Matplotlib画条形图
  9. 普强“千语”语音识别引擎应用场景
  10. 语音自训练平台技术详解,快速训练专属语音识别模型