2014
11-30

# Draw Regular Polygon

A regular polygon is a polygon whose all angles are equal in measure and all sides have the same length. Great mathematician Carl Friedrich Gauss had drawn a regular heptadecagon with only ruler and compass when he was 19. But today, we will not draw regular polygons that way. You had a circle of unit radius and some rulers, each can only measure certain distance, which equals to 2 sin (k*PI/n) , where n and k are known for each ruler. Thus you can measure an arc of 2k*PI/n on that circle with a ruler of parameters
n and k. Rulers can be used only to measure arcs on that circle, othe uses are forbidden. Each ruler can be used arbitrary many times. Now give you the parameters of r rulers, how many different regular polygons can you draw? Two regular polygons are said to be the same if they had the same number of sides, but may be differ in orientations. And a regular polygon should have at least 3 sides.

Each test case begins with an integer r(1 <= r <=30) the number of rulers, on the first line. Then r lines followed, each line describes a ruler with two positive integers n(1 <= n <= 104) and k(1 <= k <= 104), indicate that ruler can measure a distance of 2 sin k*PI/n .

Each test case begins with an integer r(1 <= r <=30) the number of rulers, on the first line. Then r lines followed, each line describes a ruler with two positive integers n(1 <= n <= 104) and k(1 <= k <= 104), indicate that ruler can measure a distance of 2 sin k*PI/n .

2
2 1
3 1
1
4 2
1
5 2

2
3 6
0
1
5
Pr

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define LL __int64
const int N = 1e9;
using namespace std;

int r, d[10005], n[40], pr[10005], qpr[10005], tq;

void Pr()
{
int i, j;
memset( pr, 1, sizeof(pr) );
pr[1] = 0;
for( i = 2; i <= 10000; i ++ )
if( pr[i] )
{
j = i+i;
while( j <= 10000 )
{
pr[j] = 0;
j += i;
}
qpr[tq++] = i;
}
}
int Gcd( int a, int b )
{
if( !b )
return a;
else
return Gcd( b, a%b );
}
void Op()
{
memset( d, 0, sizeof(d) );
int t, i, j, tt;
for( j = 0; j < r; j ++ )
{
t = n[j];
for( i = 0; i < tq && t > 1; i ++ )
if( !( t % qpr[i] ) )
{
tt = 1;
t/=qpr[i];
while( !( t%qpr[i] ) )
{
tt ++;
t/=qpr[i];
}
d[ qpr[i] ] = d[ qpr[i] ] > tt ? d[ qpr[i] ] : tt;
}
}
}

int main()
{
int i, j, k, ti, qu[10004], top, na, mu[30];
LL qa[30], t, ans;
Pr();

while( ~scanf( "%d", &r ) )
{
for( i = 0; i < r; i ++ )
{
scanf( "%d%d", n+i, &k );
k = Gcd( n[i], k );
n[i] /= k;
}
Op();
ans = 1;
top = 0;
for( i = 0; i < tq; i ++ )
if( d[ qpr[i] ] > 0)
{
ans *= d[ qpr[i] ]+1;
qu[ top++ ] = qpr[i];
}
if( top > 0 && qu[0] == 2 )
na = 2;
else
na = 1;
printf( "%I64d\n", ans-na );
ans = ans < 16+na ? ans : 16+na;
qa[0] = 1;
mu[0] = 0;
for( k = 1; k < ans; k ++ )
{
t = N*(LL)N;
for( i = 0; i < k; i ++ )
if( mu[i] < top && qu[ mu[i] ] * qa[i] < t )
t = qu[ mu[i] ]*qa[i];
qa[k] = t;
mu[k] = -1;
for( j = 0; j <= k; j ++ )
if( mu[j] < 0 || ( mu[j] < top && qu[ mu[j] ] * qa[j] == qa[k] ) )
{
for( i = mu[j]+1; i < top; i ++ )
{
ti = 0;
t = qa[j];
while( !( t % qu[i] ) )
{
ti ++;
t /= qu[i];
}
if( ti < d[ qu[i] ] )
break;
}
mu[j] = i;
}
}
if( na < ans )
{
printf( "%I64d", qa[na] );
na ++;
for( j = 1; na < ans && j < 16; na ++, j ++ )
printf( " %I64d", qa[na] );
printf( "\n" );
}
}

//out
return 0;
}

1. 这道题目的核心一句话是：取还是不取。
如果当前取，则index+1作为参数。如果当前不取，则任用index作为参数。