#639 (Div. 2)C. Hilbert's Hotel

题目描述:

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

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

题目大意:

有一个大小为 n 的数组 a,将整数集 Z 中的每个整数 k 移至 k+a[k%n] 的位置,问同一位置是否只有一个数。

题目分析:

这个题的难点在于怎么将转移后的数组进行判重。这个我昨天做的时候也没想出来(太菜了)。今天看了大佬的题解之后才知道:可以用map存储转移后数k的位置,以及该位置上有几个数(即是否有重合)
并且记录位置时用 %n 将转移后的数的范围限定在1-n之间,这样就能查找到这些数是否有重合了。

代码如下:

一:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <unordered_set> 
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
int const N=2e5+5;
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		map<int,int> ma;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			int x;
			scanf("%d",&x);
			ma[((i+x)%n+n)%n]++;    //因为转移后的数可能为负数,所以要再加n变正
		}   //转移前的数即为i,i+x为转移后的数
		
		bool st=true;
		for(auto it:ma)     //范围for循环,遍历map中的所有元素
		if(it.second>=2)    //如果某个位置的数大于1,则有重合
		{
			st=false;
			break;
		}
		if(st) puts("YES");
		else puts("NO");
	}
    return 0;
}

二:
转移后的数的位置也可以用数组来存储,a[i]表示数i转移后的位置。这样的话判重就要进行排序,排好序之后遍历一遍,看看是否有相等的数即可,基本上和上一个代码是一样的。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <map>
#include <queue>
#include <vector>
#include <unordered_set> 
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
int const N=2e5+5;
int a[N];
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		for(int i=1;i<=n;i++)
		{
			int x;
			scanf("%d",&x);
			a[i]=((i+x)%n+n)%n;
		}
		sort(a+1,a+1+n);
		
		bool st=true;
		for(int i=1;i<n;i++)
		if(a[i]==a[i+1]) {st=false; break;} 
		
		if(st) puts("YES");
		else puts("NO");
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/li_wen_zhuo/article/details/105965662