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

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 ;
}

poj 2049 Let it Bead(polya模板)的更多相关文章

  1. POJ 2409 Let it Bead (Polya定理)

    题意 用k种颜色对n个珠子构成的环上色,旋转翻转后相同的只算一种,求不等价的着色方案数. 思路 Polya定理 X是对象集合{1, 2, --, n}, 设G是X上的置换群,用M种颜色染N种对象,则不 ...

  2. poj 2409 Let it Bead Polya计数

    旋转能够分为n种置换,相应的循环个数各自是gcd(n,i),个i=0时不动,有n个 翻转分为奇偶讨论,奇数时有n种置换,每种有n/2+1个 偶数时有n种置换,一半是n/2+1个,一半是n/2个 啃论文 ...

  3. [ACM] POJ 2409 Let it Bead (Polya计数)

    参考:https://blog.csdn.net/sr_19930829/article/details/38108871 #include <iostream> #include < ...

  4. bzoj 1004 [HNOI2008]Cards && poj 2409 Let it Bead ——置换群

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1004 http://poj.org/problem?id=2409 学习材料:https:/ ...

  5. POJ 2406 Power Strings 简单KMP模板 strcmp

    http://poj.org/problem?id=2406 只是模板,但是有趣的是一个strcmp的字符串比较函数,学习到了... https://baike.baidu.com/item/strc ...

  6. bzoj 1004 Cards & poj 2409 Let it Bead —— 置换群

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1004 关于置换群:https://www.cnblogs.com/nietzsche-oie ...

  7. poj 2049(二分+spfa判负环)

    poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...

  8. POJ 2409 Let it Bead【Polya定理】(模板题)

    <题目链接> 题目大意:用k种颜色对n个珠子构成的环上色,旋转.翻转后相同的只算一种,求不等价的着色方案数. 解题分析: 对于这种等价计数问题,可以用polay定理来解决,本题是一道pol ...

  9. POJ 2409 Let it Bead(polya裸题)

    题目传送:http://poj.org/problem?id=2409 Description "Let it Bead" company is located upstairs ...

随机推荐

  1. 你需要知道的九大排序算法【Python实现】之快速排序

    五.快速排序 基本思想:  通过一趟排序将待排序记录分割成独立的两部分,其中一部分记录的关键字均比另一部分关键字小,则分别对这两部分继续进行排序,直到整个序列有序. 算法实现: ​ #coding: ...

  2. 坑爹的vector iterators incompatible错误(VS中属性页-->C/C++-->代码生成-->>运行库)

    之前一直被这个错误折磨着,就是不知道问题在那,后来找了很多资料,大概都是说这是因为多个线程同时操作vector的问题(参考这里).可是我这里的代码并没有问题,因为同样的代码在别的解决方案中已经成功运行 ...

  3. java开发webservice的几种方式

    webservice的应用已经越来越广泛了,下面介绍几种在Java体系中开发webservice的方式,相当于做个记录. 1.Axis2 Axis是apache下一个开源的webservice开发组件 ...

  4. 自定义控件(视图)2期笔记03:自定义控件之使用系统控件(优酷案例之广告条Viewpager)

    1.首先我们看看运行效果,如下: 2. 下面就是详细实现这个效果的过程: (1)新建一个Android工程,命名为"广告条的效果",如下: (2)这里用到一个控件ViewPager ...

  5. Java基础知识强化44:StringBuffer类之把数组拼接成指定格式的字符串的案例

    1. 先看案例代码如下: package cn.itcast_07; /* * 把数组拼接成一个字符串 */ public class StringBufferTest2 { public stati ...

  6. ubantu命令安装banner

    banner命令可以输出图形字符 在线yum安装 $ sudo apt-get update;sudo apt-get install sysvbanner

  7. pd的django To Do List教程-----3:模板的建立

    ---恢复内容开始--- 1:在app下建立static文件夹并且放入bootstrap文件包以及一个写好的css文件style.css.文件目录如下: style.css代码: .form-cont ...

  8. (转)在Repeater中嵌套使用Repeater

    在一般的网站中浏览类别的用户控件通常都位于大多数 ASP.NET 页的左边,它使用户能够按类别快速的查找产品.最近遇到一个客户,因为在他网站上展示的产品并不多,所以要求在原有类别浏览的基础上将产品也加 ...

  9. Objective-C 异常处理

    #import <UIKit/UIKit.h> #import "AppDelegate.h" int main(int argc, char * argv[]) { ...

  10. HC-05蓝牙模块基本使用

    1.进入AT模式 EN输入高电平+按住按键不放,然后上电,进入AT模式,不过AT指令只能输入一次,下次再输入AT需要重新进入 2.串口波特率设为38400,进行AT模式下的指令操作 3.基本AT指令 ...