poj 2049 Let it Bead(polya模板)

 

Description

"Let it Bead" company is located upstairs at  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  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.

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=. Otherwise, both are positive, and, due to technical difficulties in the bracelet-fabrication-machine, cs<=, i.e. their product does not exceed .

Output

poj 2049 Let it Bead(polya模板)
For each test case output on a single line the number of unique bracelets. The figure below shows the  different bracelets that can be made with  colors and  beads.

Sample Input


Sample Output


Source

 
非暴力,其实暴力和非暴力时间差不多
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<set>
#include<vector>
using namespace std;
#define ll long long
ll pow_mod(ll a,ll i){
if(i==)
return ;
ll t=pow_mod(a,i/);
ll ans=t*t;
if(i&)
ans=ans*a;
return ans;
} vector<ll> divisor(ll n){
vector<ll> res;
for(ll i=;i*i<=n;i++){
if(n%i==){
res.push_back(i);
if(i*i!=n){
res.push_back(n/i);
}
}
}
return res;
} ll eular(ll n){
ll res=;
for(ll i=;i*i<=n;i++){
if(n%i==){
n/=i,res*=i-;
while(n%i==){
n/=i;
res*=i;
}
}
}
if(n>) res*=n-;
return res;
} ll polya(ll m,ll n){
vector<ll> divs = divisor(n);
ll res=;
for(ll i=;i<divs.size();i++){
ll euler=eular(divs[i]);
res+=euler*pow_mod(m,n/divs[i]);
}
res/=n;
return res;
} int main()
{
ll n,m;
while(scanf("%I64d%I64d",&m,&n)== && n+m!=){
ll ans=polya(m,n)*n;//旋转情况
if(n&){//奇数
ans+=n*pow_mod(m,n/+);//翻转情况
}
else{//偶数
ans += (pow_mod(m, n / + ) + pow_mod(m, n / )) * (n / );//翻转情况
}
ans/=*n;
printf("%I64d\n",ans);
}
return ;
}

暴力枚举k

 #include <iostream>
using namespace std; #define LL long long int gcd(int a, int b)
{
return b == ? a : gcd(b, a % b);
} LL power(LL p, LL n)
{
LL sum = ;
while (n)
{
if (n & )
sum *= p;
p *= p;
n /= ; }
return sum;
} ///////////////////////////SubMain//////////////////////////////////
int main()
{ LL n; LL m;
while (~scanf("%I64d%I64d", &m,&n) && n+m!=)
{
LL count = ;
for (int i = ; i <= n; ++i)
count += power(m, gcd(i, n));
if (n & )
count += n * power(m, n / + );
else
count += n / * (power(m, n / + ) + power(m, n / ));
count /= n * ;
printf("%lld\n", count);
} return ;
}
上一篇:linux 命令 第一波


下一篇:css 初始化