zoukankan      html  css  js  c++  java
  • 数学计数原理(Pólya):POJ 1286 Necklace of Beads

    Necklace of Beads
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 7763   Accepted: 3247

    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
      
      公式是这样子的:

      p是颜色数,这里等于3,可以发现这2*n个置换形成了置换群,满足了群的封闭性。

      那么只要对于每个置换找不动点就好了…… http://www.cnblogs.com/TenderRun/p/5656038.html 循环的部分和这题类似

     1 #include <iostream>
     2 #include <cstring>
     3 #include <cstdio>
     4 using namespace std;
     5 long long pow[30],phi[30],n,ans;
     6 long long Gcd(long long a,long long b){
     7     return b?Gcd(b,a%b):a;
     8 }
     9 int main(){
    10     pow[0]=1;
    11     for(int i=1;i<=24;i++)
    12         pow[i]=pow[i-1]*3;
    13     for(int i=1;i<=24;i++)
    14         for(int j=i;j>=1;j--)
    15             if(Gcd(i,j)==1)phi[i]+=1;    
    16     while(scanf("%lld",&n)!=EOF&&n!=-1){
    17         if(n==0){printf("0
    ");continue;}
    18         for(int d=1;d<=n;d++)
    19             if(n%d==0)ans+=phi[n/d]*pow[d];
    20         if(n%2)ans=(ans+n*pow[(n+1)/2])/2/n;
    21         else ans=(ans+n/2*(pow[n/2+1]+pow[n/2]))/2/n;
    22         printf("%lld
    ",ans);ans=0;
    23     }
    24 }
    25     

  • 相关阅读:
    18_异常机制和File类
    20个简洁的 JS 代码片段
    在 Python 中实现延迟调用
    停止 Goroutine 有几种方法?
    图解Python中深浅copy
    Python 自制简单实用的日志装饰器
    Go 里的错误得这样写才优雅~
    推荐8个炫酷的 Python 装饰器!
    两个 Django 插件( django_extensions,django_toolbar)
    一文看懂Python系列之装饰器(decorator)
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5789609.html
Copyright © 2011-2022 走看看