///////////////////////////////////////////////////////////////////////////////////////////////////////
作者:stxy-ferryman
声明:本文遵循以下协议自由转载-非商用-非衍生-保持署名|Creative Commons BY-NC-ND 3.0
查看本文更新与讨论请点击:http://www.cnblogs.com/stxy-ferryman/
链接被删请百度:stxy-ferryman
///////////////////////////////////////////////////////////////////////////////////////////////////////
Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was.
Its most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers from 0 to n - 1 in the counter-clockwise order. When you push a button, the first gear rotates clockwise, then the second gear rotates counter-clockwise, the the third gear rotates clockwise an so on.
Besides, each gear has exactly one active tooth. When a gear turns, a new active tooth is the one following after the current active tooth according to the direction of the rotation. For example, if n = 5, and the active tooth is the one containing number 0, then clockwise rotation makes the tooth with number 1 active, or the counter-clockwise rotating makes the tooth number 4 active.
Andrewid remembers that the real puzzle has the following property: you can push the button multiple times in such a way that in the end the numbers on the active teeth of the gears from first to last form sequence 0, 1, 2, ..., n - 1. Write a program that determines whether the given puzzle is real or fake.
Input
The first line contains integer n (1 ≤ n ≤ 1000) — the number of gears.
The second line contains n digits a1, a2, ..., an (0 ≤ ai ≤ n - 1) — the sequence of active teeth: the active tooth of the i-th gear contains number ai.
Output
In a single line print "Yes" (without the quotes), if the given Stolp's gears puzzle is real, and "No" (without the quotes) otherwise.
Example
3
1 0 0
Yes
5
4 2 1 4 3
Yes
4
0 2 3 1
No
Note
In the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2.
题意:有n个齿轮,依次相接,齿轮示数为ai,按下某个不可名状的东西以后,奇数齿轮顺时针旋转(示数+1),偶数齿轮逆时针旋转(示数-1),如果能转出0,1,2....n-1则输出yes否则输出no
题解:转一遍,检验一遍,对则输出yes,如果转完n次仍不能成功,那就没有什么价值继续了,输出no
当然,有o(n)算法http://paste.ubuntu.com/11788578/
代码:
var b:boolean; n,i,j,k:longint; a:array[1..1001] of longint; function jy(n:longint):boolean; begin for i:=1 to n do if a[i]<>i-1 then exit(false); exit(true); end; begin readln(n); for i:=1 to n do read(a[i]); b:=true; while (not jy(n)) do begin for i:=1 to n do begin if i mod 2=1 then inc(a[i]) else dec(a[i]); if a[i]<0 then a[i]:=n-1; if a[i]>n-1 then a[i]:=0; end; inc(k); if k>n then begin b:=false; break; end; end; if b then writeln('Yes') else writeln('No'); end.