Problem I. Vier
Input file: vier.in
Output file: vier.out
Time limit: 2 seconds
Memory limit: 256 mebibytes
The online game you’ve built in problem “Oha” involves n types of monsters. The monster types are
numbered from 1 to n, and a monster of i-th type has strength i and magical ability πi
. All magical
abilities are different integers between 1 and n, inclusive, in other words, π is a permutation. For
simplicity, you have generated this permutation uniformly at random.
Now you need to choose the starting teams of monsters for the two sides of the game. Each side must
have exactly two monsters in the starting team (they can be of the same type), and the starting teams
must be different. However, in order for the game to be balanced, the starting teams must have the same
total strength modulo n and the same total magical ability modulo n.
More formally, you need to find four integers a, b, c and d between 1 and n such that:
1. a + b ≡ c + d (mod n), and
2. πa + πb ≡ πc + πd (mod n).
Note that the above statements are trivially true when a = c and b = d, or when a = d and b = c. You
need to find any other solution, or report that there isn’t any. Note that it’s allowed for some of the four
integers to coincide — the only restriction is that they can’t coincide in the two ways described in the
first sentence of this paragraph.
Input
The first line of the input file contains one integer n, 2 ≤ n ≤ 106
. The second line of the input file
contains n distinct integers, each between 1 and n. The i-th of those integers gives the value of πi
.
It is guaranteed that the permutation was picked uniformly at random out of all permutations of n
integers.
Output
If a non-trivial solution exists, print Ja on the first line of the output file, otherwise print Nein. In case
you printed Ja, on the second line print four integers between 1 and n: a, b, c and d.
Example
vier.in vier.out
5
2 4 3 5 1
Ja
5 5 1 4
Note
There are 50 non-sample testcases in this problem.
Input file: vier.in
Output file: vier.out
Time limit: 2 seconds
Memory limit: 256 mebibytes
The online game you’ve built in problem “Oha” involves n types of monsters. The monster types are
numbered from 1 to n, and a monster of i-th type has strength i and magical ability πi
. All magical
abilities are different integers between 1 and n, inclusive, in other words, π is a permutation. For
simplicity, you have generated this permutation uniformly at random.
Now you need to choose the starting teams of monsters for the two sides of the game. Each side must
have exactly two monsters in the starting team (they can be of the same type), and the starting teams
must be different. However, in order for the game to be balanced, the starting teams must have the same
total strength modulo n and the same total magical ability modulo n.
More formally, you need to find four integers a, b, c and d between 1 and n such that:
1. a + b ≡ c + d (mod n), and
2. πa + πb ≡ πc + πd (mod n).
Note that the above statements are trivially true when a = c and b = d, or when a = d and b = c. You
need to find any other solution, or report that there isn’t any. Note that it’s allowed for some of the four
integers to coincide — the only restriction is that they can’t coincide in the two ways described in the
first sentence of this paragraph.
Input
The first line of the input file contains one integer n, 2 ≤ n ≤ 106
. The second line of the input file
contains n distinct integers, each between 1 and n. The i-th of those integers gives the value of πi
.
It is guaranteed that the permutation was picked uniformly at random out of all permutations of n
integers.
Output
If a non-trivial solution exists, print Ja on the first line of the output file, otherwise print Nein. In case
you printed Ja, on the second line print four integers between 1 and n: a, b, c and d.
Example
vier.in vier.out
5
2 4 3 5 1
Ja
5 5 1 4
Note
There are 50 non-sample testcases in this problem.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int vis[1000006];
int num[1000005];
int main()
{
freopen("vier.in","r",stdin);
freopen("vier.out","w",stdout);
int n;
cin>>n;
for(int i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
memset(vis,-1,sizeof(vis));
printf("Ja\n");
for(int i=0;i<n;i++)
{
int now=(num[(i+1)%n]-num[i]+n)%n;
//cout<<i+1<<' '<<(i+1)%n+1<<endl;
//cout<<now<<endl;
if(vis[now]==-1)
vis[now]=i;
else if(vis[now]>=0)
{
printf("%d %d %d %d\n",i+1,(vis[now]+1)%n+1,vis[now]+1,(i+1)%n+1);
return 0;
}
}
//cout << "Hello world!" << endl;
return 0;
}