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 }
  • 相关阅读:
    从跳频技术聊CDMA/WIFI之母海蒂·拉玛传奇的一生
    echarts图表X轴文字过长解决解决方案:根据文字长度自动旋转
    kafka-connect-kudu-sink插件
    mysql8远程连接问题
    zookeeper-3.4.5修改存储1M大小限制
    Java并发之CompletionService详解
    傲视Kubernetes(六):Pod管理及控制器
    傲视Kubernetes(五):注解和命名空间
    Es使用kibana增删改查以及复杂查询
    测试Ik分词器以及增加自己的词汇
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/6691929.html
Copyright © 2011-2022 走看看