POJ 2409-Let it Bead(Polya定理-旋轉+翻轉 串項鍊)
Let it Bead
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5895 | Accepted: 3950 |
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 c distinct 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.
A bracelet is a ring-like sequence of s beads each of which can have one of c distinct 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 file 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
Source
題目意思:
給定m種顏色的珠子,每種顏色的珠子個數不限,將它們串成長度為n的項鍊,計算一共能做成多少種不重複的項鍊。
兩條項鍊相同,當且僅當兩條項鍊能通過旋轉或者翻轉後能重合在一起。
解題思路:
①旋轉,將項鍊順時針旋轉i格之後,其迴圈節數是gcd(n,i),計算出所有不同的著色方案;
②翻轉,不同的方法數=迴圈群個數*pow(不同顏色數,迴圈節數)之和,然後再除以所有的置換數之和(2*n),其中:
當n為奇數,共有n個迴圈節數為(n+1)/2的迴圈群;
當n為偶數,共有n/2個迴圈節數為(n+2)/2的迴圈群和n/2個迴圈節數為n/2的迴圈群。
#include<cstdio>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
int gcd(int a,int b)
{
return (a%b!=0?(gcd(b,a%b)):b);
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("G:/cbx/read.txt","r",stdin);
//freopen("G:/cbx/out.txt","w",stdout);
#endif
int n,m;
while(scanf("%d%d",&m,&n))
{
if(m==0&&n==0) break;
int ans=0;
for(int i=1; i<=n; ++i)//旋轉,計算出著色方案
{
int t=gcd(n,i);
ans+=(int)(pow(m,t));
}
//翻轉,計算出迴圈群個數
if(n&1)//奇數
ans+=(int)(n*pow(m,(n+1)/2));
else//偶數
{
ans+=(int)(n/2*pow(m,(n+2)/2));
ans+=(int)(n/2*pow(m,(n)/2));
}
ans/=2*n;//置換群個數是2*n
printf("%d\n",ans);
}
return 0;
}
相關文章
- POJ 2409 Let it Bead:置換群 Polya定理
- 一串字串的翻轉字串
- Flutter 圖片裁剪旋轉翻轉編輯器Flutter
- canvas 影象旋轉與翻轉姿勢解鎖Canvas
- C#+OpenCV基礎(四)_旋轉翻轉透視C#OpenCV
- 轉爐鍊鋼防翻包安全技術研究WBI
- C# 簡易影像處理(包括平移,旋轉,翻轉, 裁切)C#
- 旋轉變換(一)旋轉矩陣矩陣
- 【翻譯】影像到Base64字串轉換字串
- 3D旋轉效果程式碼例項3D
- CSS3旋轉效果程式碼例項CSSS3
- 旋轉字串字串
- 旋轉相簿
- CSS 例項之翻轉圖片CSS
- 將圖片旋轉(這裡不是旋轉imageView)View
- iOS螢幕旋轉之Apple Document(蘋果文件翻譯)iOSAPP蘋果
- [Python影象處理] 六.影象縮放、影象旋轉、影象翻轉與影象平移Python
- 三維座標系旋轉——旋轉矩陣到旋轉角之間的換算矩陣
- | / - 的旋轉效果實現(轉)
- 旋轉矩陣矩陣
- 矩陣旋轉矩陣
- Activity的旋轉
- 三維旋轉:旋轉矩陣,尤拉角,四元數矩陣
- javascript元素3D旋轉效果程式碼例項JavaScript3D
- css控制div元素旋轉指定角度程式碼例項CSS
- js判斷螢幕是否旋轉程式碼例項JS
- div前後翻轉效果程式碼例項
- CSS3動畫(360度旋轉、旋轉放大、放大、移動)CSSS3動畫
- 利用三維旋轉矩陣在空間中旋轉平面矩陣
- 旋轉演算法演算法
- SVG矩形旋轉動畫SVG動畫
- 卡片旋轉動畫效果動畫
- ubuntu螢幕旋轉Ubuntu
- 旋轉連結串列
- 目標設定理論(轉載)
- css實現的div旋轉簡單程式碼例項CSS
- 按住滑鼠可以拖動箭頭旋轉程式碼例項
- 《Cracking the Coding Interview程式設計師面試金典》----原串翻轉View程式設計師面試