您的位置:首页 > 其它

CodeForces - 668B Little Artem and Dance(规律题)

2017-05-04 19:06 405 查看

Little Artem and Dance

Little Artem is fond of dancing. Most of all dances Artem likes rueda — Cuban dance that is danced by pairs of boys and girls forming a circle and dancing together.

More detailed, there are n pairs of boys and girls standing in a circle. Initially, boy number 1 dances with a girl number 1, boy number 2 dances with a girl number 2 and so on. Girls are numbered in the clockwise order. During the dance different moves are announced and all pairs perform this moves. While performing moves boys move along the circle, while girls always stay at their initial position. For the purpose of this problem we consider two different types of moves:

Value x and some direction are announced, and all boys move x positions in the corresponding direction.

Boys dancing with even-indexed girls swap positions with boys who are dancing with odd-indexed girls. That is the one who was dancing with the girl 1 swaps with the one who was dancing with the girl number 2, while the one who was dancing with girl number 3 swaps with the one who was dancing with the girl number 4 and so one. It’s guaranteed that n is even.

Your task is to determine the final position of each boy.

Input

The first line of the input contains two integers n and q (2 ≤ n ≤ 1 000 000, 1 ≤ q ≤ 2 000 000) — the number of couples in the rueda and the number of commands to perform, respectively. It’s guaranteed that n is even.

Next q lines contain the descriptions of the commands. Each command has type as the integer 1 or 2 first. Command of the first type is given as x ( - n ≤ x ≤ n), where 0 ≤ x ≤ n means all boys moves x girls in clockwise direction, while  - x means all boys move x positions in counter-clockwise direction. There is no other input for commands of the second type.

Output

Output n integers, the i-th of them should be equal to the index of boy the i-th girl is dancing with after performing all q moves.

Examples

input

6 3

1 2

4000

2

1 2

output

4 3 6 5 2 1

input

2 3

1 1

2

1 -2

output

1 2

input

4 2

2

1 3

output

1 4 3 2

分析:根据题意,有两种操作,一种是前移或后移,一种是奇偶位交换。模拟一下可以发现:

奇数的排列永远是这样的,X X 1 X 3 X 5 X 7 X…

偶数的排列也永远是这样的,X X 2 X 4 X 6 X 8 X…

因为,操作一肯定是不可能改变的,而操作二呢,是所有的奇数位和偶数位互换,也不会改变这种次序

因此,可以直接用两个数a=1和b=2来模拟所有的操作,最后位置a上的数就是1,位置b上的数就是2,然后按照规律写入其他数字就好了

代码:

#include<stdio.h>
int num[1000010];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
int a=1,b=2;
int op,val;
for(int i=0; i<m; ++i)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d",&val);
a=(a+(val+n)%n)%n;
if(a==0)
a=n;
b=(b+(val+n)%n)%n;
if(b==0)
b=n;
}
else
{
if(a&1)
++a;
else
--a;
if(a==0)
a=n;
else if(a>n)
a=a-n;
if(b&1)
++b;
else
--b;
if(b==0)
b=n;
else if(b>n)
b=b-n;
}
}
//    int cnt=1;
//    for(int i=a; i<=n; i+=2,cnt+=2)
//        num[i]=cnt;
//    cnt=n-1;
//    for(int i=a-2; i>0; i-=2,cnt-=2)
//        num[i]=cnt;
//    cnt=2;
//    for(int i=b; i<=n; i+=2,cnt+=2)
//        num[i]=cnt;
//    cnt=n;
//    for(int i=b-2; i>0; i-=2,cnt-=2)
//        num[i]=cnt;
for(int i=1; i<=n; ++i)
{
if(i&1)
{
int k=(a+i-1)%n;
if(k==0)
k=n;
num[k]=i;
}
else
{
int k=(b+i-2)%n;
if(k==0)
k=n;
num[k]=i;
}
}
for(int i=1; i<=n; ++i)
printf("%d ",num[i]);
return 0;
}


这里优化一下,令a=0,b=1

代码:

#include<stdio.h>
int num[1000010];

int main()
{
int n,Q;
scanf("%d%d",&n,&Q);
int op,val;
int a=0,b=1;
for(int i=0; i<Q; ++i)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d",&val);
a=(a+(val+n)%n)%n;
b=(b+(val+n)%n)%n;
}
else
a^=1,b^=1;
}
for(int i=0; i<n; ++i)
{
if(i&1)
num[(i+b-1)%n]=i;
else
num[(i+a)%n]=i;
}
for(int i=0; i<n; ++i)
printf("%d ",num[i]+1);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: