T1 连珠风暴
题目
【题目描述】
给定M种颜色的珠子,每种颜色珠子的个数均不限,将这些珠子做成长度为N的项链。
问能做成多少种不重复的项链。两条项链相同,当且仅当两条项链通过旋转或是翻转后能重合在一起,且对应珠子的颜色相同。
【输入格式】
一行两个整数分别表示M,N。
【输出格式】
一行一个整数表示答案。
【输入样例】
2 5
【输出样例】
8
【数据规模】
对于30%的数据,n,m≤4;
对于60%的数据,n,m≤5;
对于100%的数据,nm≤32。
解析
nm≤32,可以直接暴力AC。
正解貌似是Polya定理。
Code
#include <algorithm> #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <cmath> using namespace std; int m,n,ans; int gcd(int x,int y) { if(y==0) return x; return gcd(y,x%y); } int main() { //freopen("necklace.in","r",stdin); //freopen("necklace.out","w",stdout); cin>>m>>n; for(int i=1;i<=n;i++) ans+=pow(m,gcd(n,i)); if(n&1) ans+=n*pow(m,(n+1)/2); else ans+=(n/2)*pow(m,(n+2)/2)+(n/2)*pow(m,n/2); cout<<ans/(n*2); return 0; }