There are n knights sitting at the Round Table at an equal distance from each other. Each of them is either in a good or in a bad mood.
Merlin, the wizard predicted to King Arthur that the next month will turn out to be particularly fortunate if the regular
polygon can be found. On all vertices of the polygon knights in a good
mood should be located. Otherwise, the next month will bring
misfortunes.
A convex polygon is regular if all its sides have
same length and all his angles are equal. In this problem we consider
only regular polygons with at least 3 vertices, i. e. only
nondegenerated.
On a picture below some examples of such polygons
are present. Green points mean knights in a good mood. Red points mean
ones in a bad mood.
King Arthur knows the knights' moods. Help him find out if the next month will be fortunate or not.
Output
Print "YES" without the quotes if the following month will turn out to be lucky. Otherwise, print "NO".
水之。。。
1 #include <iostream>
2 #include <cstdio>
3
4 using namespace std;
5
6 #define L 100009
7 int n, a[ L ], s[ L ];
8
9 int yes() {
10 int i, j, m = n/3, k;
11 for ( i = 1; i <= m; ++i ) {
12 if ( n % i == 0 ) {
13 for ( j = 1; j <= i; ++j ) {
14 s[ j ] = a[ j ];
15 }
16 for ( j = i+1; j <= n; ++j ) {
17 s[ j ] = s[ j - i ] + a[ j ];
18 }
19 k = n / i;
20 for ( j = n-i+1; j <= n; ++j ) {
21 if ( s[ j ] == k ) {
22 return 1;
23 }
24 }
25 }
26 }
27 return 0;
28 }
29
30 int main() {
31 int i;
32 while ( scanf( "%d", &n ) == 1 ) {
33 for ( i = 1; i <= n; ++i ) {
34 scanf( "%d", a+i );
35 }
36 if ( yes() ) {
37 puts( "YES" );
38 }
39 else {
40 puts( "NO" );
41 }
42 }
43 return 0;
44 }
45