zoukankan      html  css  js  c++  java
  • Necklace of Beads(polya计数)

    Necklace of Beads
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 7451   Accepted: 3102

    Description

    Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < 24 ). If the repetitions that are produced by rotation around the center of the circular necklace or reflection to the axis of symmetry are all neglected, how many different forms of the necklace are there? 

    Input

    The input has several lines, and each line contains the input data n.  -1 denotes the end of the input file. 

    Output

    The output should contain the output data: Number of different forms, in each line correspondent to the input data.

    Sample Input

    4
    5
    -1
    

    Sample Output

    21
    39
    题解:给出红,绿,蓝3种颜色 的n个珠子,求能够组成多少个不同的项链。 (旋转 和 翻转后 相同的属于同一个项链)
    看了好久大神的代码还是不太理解;暂时的理解是,对于每个循环节里的元素都有k中染色方案,所以是k^c(f);
    现在只需要找出所有循环节的种数就好了;当然翻转和旋转循环节是不同的,翻转和旋转均有n种;所有种数加完要除以2n

    Polya定理:

    (1)设G是p个对象的一个置换群,用k种颜色给这p个对象,若一种染色方案在群G的作用下变为另一种方案,则这两个方案当作是同一种方案,这样的不同染色方案数为

     

    (2)对于N个珠子的项链,共有n种旋转置换和n种翻转置换。

    对于旋转置换:每种置换的循环节数c(fi) = gcd(n,i),(i为一次转过多少个珠子)

    对于翻转置换:如果n为奇数,共有n种翻转置换,每种置换的循环节数均为c(f) = n/2 + 1;              如果n为偶数,分两种情况 <1> 从空白处穿对称轴,则轴两边各有n/2个对象,得到c(f) = n/2;

    <2> 从两个对象上穿对称轴,则轴两边各有n/2-2个对象,得到c(f) = n/2 + 1。

    代码:

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<queue>
    using namespace std;
    const int INF=0x3f3f3f3f;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    typedef long long LL;
    int gcd(int a,int b){return b==0?a:gcd(b,a%b);}
    int main(){
        int n;
        while(~SI(n),n!=-1){
            LL ans=0;
            if(!n){
                puts("0");continue;
            }
            for(int i=1;i<=n;i++)
                ans+=pow(3,gcd(i,n));
            ans+=n*(n&1?pow(3,n/2+1):(pow(3,n/2)/2+pow(3,n/2+1)/2));
            printf("%lld
    ",ans/(2*n));
        }
        return 0;
    }
  • 相关阅读:
    effective C++ 条款 46:需要类型转换时请为模板定义非成员函数
    effective C++ 条款 42:了解typename的双重意义
    effective C++ 条款 38:通过复合塑模出hasa或“根据某物实现出”
    effective C++ 条款 44:将与参数无关的代码抽离templates
    职场女性工作的六大禁忌
    正试图在 os 加载程序锁内执行托管代码。不要尝试在 DllMain 或映像初始化函数内运行托管代码,这样做会导致应用程序挂起
    C# 截图 窗体截图 屏幕截图
    C# 创建DataSet 实例
    c# WinForm 中鼠标控制窗体的移动
    访问用户控件里面的控件的两种方法
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5233812.html
Copyright © 2011-2022 走看看