码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2409 polya定理

时间:2015-04-24 00:55:23      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

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 }

poj 2409 polya定理

标签:

原文地址:http://www.cnblogs.com/huoxiayu/p/4452130.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!