zoukankan      html  css  js  c++  java
  • POJ1286 Necklace of Beads

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 8263   Accepted: 3452

    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

    数学问题 统计 polya原理

    和POJ2409一样的套路

     1 /*by SilverN*/
     2 #include<algorithm>
     3 #include<iostream>
     4 #include<cstring>
     5 #include<cstdio>
     6 #include<cmath>
     7 #include<vector>
     8 #define LL long long
     9 using namespace std;
    10 int read(){
    11     int x=0,f=1;char ch=getchar();
    12     while(ch<'0' || ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    13     while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
    14     return x*f;
    15 }
    16 LL phi(int x){
    17     int m=sqrt(x+0.5);
    18     LL res=x;
    19     for(int i=2;i<=m;i++)
    20         if(x%i==0){
    21             res=res/i*(i-1);
    22             while(x%i==0)x/=i;
    23         }
    24     if(x>1)res=res/x*(x-1);
    25     return res;
    26 }
    27 int n;
    28 int gcd(int a,int b){
    29     return (!b)?a:gcd(b,a%b);
    30 }
    31 LL ksm(LL c,LL k){
    32     LL res=1;
    33     while(k){
    34         if(k&1)res=res*c;
    35         c*=c;
    36         k>>=1;
    37     }
    38     return res;
    39 }
    40 int main(){
    41     int i,j;
    42     while(1){
    43         n=read();
    44         if(n==-1)break;
    45         if(!n){
    46             cout<<0<<endl;
    47             continue;
    48         }
    49         LL ans=0;
    50         for(i=1;i<=n;i++){
    51             if(n%i==0)ans+=ksm(3,i)*phi(n/i);
    52         }
    53         if(!(n&1)){
    54             ans+=ksm(3,n/2)*n/2;
    55             ans+=ksm(3,n/2+1)*n/2;
    56         }
    57         else ans+=ksm(3,(n+1)/2)*n;
    58         ans/=2*n;
    59         cout<<ans<<endl;
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    using 关键字在 C++ 中的几种用法
    Chromium 修改图片资源
    SAM&广义SAM
    Burnside和Polya
    笔记:杜教筛
    笔记:莫比乌斯反演
    Miller-Rabin
    点分治
    虚树
    计算几何
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6691929.html
Copyright © 2011-2022 走看看