poj 1286 和 2409 (polya 定理)

关于polya的定义网上可以搜到很多,我所了解的也是从网上搜到的,不过可以介绍几个博客,个人觉得讲的还不错。

1、关于polya的定义,http://blog.csdn.net/geniusluzh/article/details/6795412

2、将这两道题的,http://yzmduncan.iteye.com/blog/1402942

如果还不懂得话,推荐看一下吴文虎的《组合数学》里的置换群那一节,对于求循环节很有帮助。

1286代码:

View Code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <math.h>
using namespace std ;

typedef long long ll ;

int gcd ( int x , int y )
{
    if ( !y )
    return x ;
    else
    return gcd( y , x % y );
}

ll powx ( int a , int b )
{
    int i ;
    ll ss = 1 ;
    for ( i = 1 ; i <= b ; i++ )
    ss *= a ;
    return ss ;
}

ll polya( int c , int n )
{
    int  i ;
    ll sum = 0 ;

    for ( i = 1 ; i <= n ; i++ )
    sum += powx( c , gcd ( i , n ));

    if ( n & 1 )
    {
        sum += n * powx ( c , n / 2 + 1) ;
    }
    else
    {
        sum += ( n / 2 ) * powx ( c , n / 2 + 1 ) + ( n / 2 ) * powx ( c , n / 2 );
    }
    return sum / 2 / n ;
}

int main()
{
    int n ;

    while ( scanf( "%d" , &n ) != EOF )
    {
        if ( n == -1 )
        break ;
        if ( n == 0 )
        {
            printf ( "0\n" );
            continue ;
        }
        printf ( "%I64d\n" , polya( 3 , n ));
    }
    return 0 ;
}

2409和1286基本一样,就是把c改为输入的就行了。

posted @ 2012-08-18 19:35  Misty_1  阅读(393)  评论(0编辑  收藏  举报