poj 2409 polya定理

polya定理的入门题

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 
 6 int pow( int a, int n )
 7 {
 8   int r = 1;
 9   while ( n-- )
10   {
11     r = r * a;
12   }
13   return r;
14 }
15 
16 int gcd( int a, int b )
17 {
18   if ( b )
19   {
20     return gcd( b, a % b );
21   }
22   return a;
23 }
24 
25 int main ()
26 {
27   int c, s;
28   while ( scanf("%d%d", &c, &s), c + s )
29   {
30     int sum = 0;
31     for ( int i = 1; i <= s; i++ )
32     {
33       int tmp = gcd( i, s );
34       sum += pow( c, tmp );
35     }
36     if ( s & 1 )
37     {
38       sum += s * pow( c, ( s + 1 ) / 2 );
39     }
40     else
41     {
42       sum += ( s / 2 ) * ( pow( c, ( s / 2 ) + 1 ) + pow( c, ( s / 2 ) ) );
43     }
44     sum /= s * 2;
45     printf("%d\n", sum);
46   }
47     return 0;
48 }
posted @ 2015-04-23 23:14  hxy_has_been_used  阅读(125)  评论(0编辑  收藏  举报