zoukankan      html  css  js  c++  java
  • POJ1286:Necklace of Beads(POLYA定理应用) java程序员

    Necklace of Beads
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 5410   Accepted: 2241

    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
    

    Source


    MYCode:

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    typedef long long ll;
    using namespace std;
    int gcd(int a,int b)
    {
        if(a%b==0)
        return b;
        return gcd(b,a%b);
    }
    int main()
    {
        int n;
        while(scanf("%d",&n))
        {
            if(n==-1)
            break;
            if(n==0)//note
            {
                printf("0\n");
                continue;
            }
            ll ans=0;
            int i;
            for(i=1;i<=n;i++)
            {
                ans+=(ll)(pow(3.0,gcd(n,i)));
            }
            if(n&1)
            {
                ans+=(ll)(n*pow(3.0,n/2+1));
            }
            else
            {
                ans+=(ll)(n/2*pow(3.0,n/2)+n/2*pow(3.0,n/2+1));
            }
            printf("%lld\n",ans/(2*n));
        }
    }
    //

    POLYA定理的应用

    注意:

    1特殊考虑n==0的情况,否则会runtime error

    1结果要用long long.

  • 相关阅读:
    18_09_05 textarea高度自适应 和 Z-index
    18_08_20 jQuery 获取URL中的参数
    18_08_20 jQuery 光标聚焦文字之后
    18_08_20 jQuery 将前台 多张图片 和 多个附件 转化为 附件 向后台请求
    18_8_20 Bootstrap ul标题太多的处理方式
    Http (java)的post和get方式
    18_8_20 java 时间延后的通用写法
    iOS设计模式-组合
    iOS设计模式-迭代器
    iOS设计模式-观察者
  • 原文地址:https://www.cnblogs.com/java20130725/p/3215897.html
Copyright © 2011-2022 走看看