1. 程式人生 > >Zju1961 Let it Bead

Zju1961 Let it Bead

inf put inpu while -m mes air 方案 rtm

Description
"Let it Bead" company is located upstairs at 700 Cannery Row in Monterey, CA. As you can deduce from the company name, their business is beads. Their PR department found out that customers are interested in buying colored bracelets. However, over 90 percent of the target audience insists that the bracelets be unique. (Just imagine what happened if two women showed up at the same party wearing identical bracelets!) It‘s a good thing that bracelets can have different lengths and need not be made of beads of one color. Help the boss estimating maximum profit by calculating how many different bracelets can be produced.A bracelet is a ring-like sequence of s beads each of which can have one of cdistinct colors. The ring is closed, i.e. has no beginning or end, and has no direction. Assume an unlimited supply of beads of each color. For different values of s and c, calculate the number of different bracelets that can be made.
給定顏色種數和環上的珠子總數,問有多少種染色方案(通過旋轉和翻轉相同的算同一種)。

Input
Every line of the input defines a test case and contains two integers:
the number of available colors c followed by the length of the bracelets s.
Input is terminated by c = s = 0.
Otherwise, both are positive, and, due to technical difficulties in the bracelet-fabrication-machine, cs <= 32,
i.e. their product does not exceed 32.

Output
For each test case output on a single line the number of unique bracelets.
The figure below shows the 8 different bracelets that can be made with 2 colors and 5 beads.

Sample Input
1 1
2 1
2 2
5 1
2 5
2 6
6 2
0 0

Sample Output
1
2
3
5
8
13
21

polya置換的裸題了,考慮旋轉,我們枚舉所有可能的旋轉方式,所以得到的循環節個數為gcd(i,n),因此答案為\(\sum\limits_{i=1}^{n} c^{gcd(n,i)}\)

再考慮一下翻轉,我們分奇數和偶數進行討論,如果是奇數,那麽不論如何找對稱軸,都必定會形成\(\frac{n}{2}+1\)個循環節,再乘上\(n\)即可;如果是偶數,那麽就會有\(\frac{n}{2}\)\(\frac{n-2}{2}+2\)兩種循環節情況,然後每種情況各占\(\frac{n}{2}\)條對稱軸

最後把答案除一下置換總數\(2*n\)即可

/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
    int x=0,f=1;char ch=getchar();
    for (;ch<‘0‘||ch>‘9‘;ch=getchar())    if (ch==‘-‘)    f=-1;
    for (;ch>=‘0‘&&ch<=‘9‘;ch=getchar())  x=(x<<1)+(x<<3)+ch-‘0‘;
    return x*f;
}
inline void print(int x){
    if (x>=10)     print(x/10);
    putchar(x%10+‘0‘);
}
int gcd(int a,int b){return !b?a:gcd(b,a%b);}
int mlt(int a,int b){
    int res=1;
    for (;b;b>>=1,a=a*a)  if (b&1)    res=res*a;
    return res;
}
int main(){
    while (true){
        int m=read(),n=read(),ans=0;
        if (!n&&!m) break;
        for (int i=1;i<=n;i++)   ans+=mlt(m,gcd(n,i));
        if (n&1)    ans+=n*mlt(m,n/2+1);
        else    ans+=(mlt(m,n/2+1)+mlt(m,n/2))*(n>>1);
        printf("%d\n",ans/(2*n));
    }
    return 0;
}

Zju1961 Let it Bead