poj 2049 Let it Bead(polya模板)

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.

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

Ulm Local 2000

非暴力,其实暴力和非暴力时间差不多

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<map>
 5 #include<set>
 6 #include<vector>
 7 using namespace std;
 8 #define ll long long
 9 ll pow_mod(ll a,ll i){
10     if(i==0)
11         return 1;
12     ll t=pow_mod(a,i/2);
13     ll ans=t*t;
14     if(i&1)
15         ans=ans*a;
16     return ans;
17 }
18
19 vector<ll> divisor(ll n){
20     vector<ll> res;
21     for(ll i=1;i*i<=n;i++){
22         if(n%i==0){
23             res.push_back(i);
24             if(i*i!=n){
25                 res.push_back(n/i);
26             }
27         }
28     }
29     return res;
30 }
31
32 ll eular(ll n){
33     ll res=1;
34     for(ll i=2;i*i<=n;i++){
35         if(n%i==0){
36             n/=i,res*=i-1;
37             while(n%i==0){
38                 n/=i;
39                 res*=i;
40             }
41         }
42     }
43     if(n>1) res*=n-1;
44     return res;
45 }
46
47 ll polya(ll m,ll n){
48     vector<ll> divs = divisor(n);
49     ll res=0;
50     for(ll i=0;i<divs.size();i++){
51         ll euler=eular(divs[i]);
52         res+=euler*pow_mod(m,n/divs[i]);
53     }
54     res/=n;
55     return res;
56 }
57
58 int main()
59 {
60     ll n,m;
61     while(scanf("%I64d%I64d",&m,&n)==2 && n+m!=0){
62         ll ans=polya(m,n)*n;//旋转情况
63         if(n&1){//奇数
64             ans+=n*pow_mod(m,n/2+1);//翻转情况
65         }
66         else{//偶数
67             ans += (pow_mod(m, n / 2 + 1) + pow_mod(m, n / 2)) * (n / 2);//翻转情况
68         }
69         ans/=2*n;
70         printf("%I64d\n",ans);
71     }
72     return 0;
73 }

暴力枚举k

 1 #include <iostream>
 2 using namespace std;
 3
 4 #define LL long long
 5
 6 int gcd(int a, int b)
 7 {
 8     return b == 0 ? a : gcd(b, a % b);
 9 }
10
11 LL power(LL p, LL n)
12 {
13     LL sum = 1;
14     while (n)
15     {
16         if (n & 1)
17             sum *= p;
18         p *= p;
19         n /= 2;
20
21     }
22     return sum;
23 }
24
25 ///////////////////////////SubMain//////////////////////////////////
26 int main()
27 {
28
29     LL n; LL m;
30     while (~scanf("%I64d%I64d", &m,&n) && n+m!=0)
31     {
32         LL count = 0;
33         for (int i = 1; i <= n; ++i)
34             count += power(m, gcd(i, n));
35         if (n & 1)
36             count += n * power(m, n / 2 + 1);
37         else
38             count += n / 2 * (power(m, n / 2 + 1) + power(m, n / 2));
39         count /= n * 2;
40         printf("%lld\n", count);
41     }
42
43     return 0;
44 }

时间: 2024-10-08 06:46:32

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

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

Let it Bead Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4434   Accepted: 2916 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 b

NYOJ 280 LK的项链 &amp;&amp;POJ 2409 Let it Bead(polya 定理)

NYOJ 280 LK的项链  :click here POJ 2409 Let it Bead:click here 题意:一盒有红.蓝.绿三种颜色的珠子,每种颜色珠子的个数都大于24,现在LK想用这一盒珠子穿出一条项链,项链上的珠子个数为n(0<=n<=24),请你帮她计算一下一共可以用这一盒珠子可以穿出多少条不同的项链.通过旋转.翻转达到同一种状态的被认为是相同的项链. poj 上是c种颜色,s个珠子组成,数据比24小. 思路:今天刚接触到polya 定理: Polya定理:设G是n个对

poj 2409 Let it Bead Polya计数

旋转能够分为n种置换,相应的循环个数各自是gcd(n,i),个i=0时不动,有n个 翻转分为奇偶讨论,奇数时有n种置换,每种有n/2+1个 偶数时有n种置换,一半是n/2+1个,一半是n/2个 啃论文,PPT,各种书好久才看懂Polya定理,近期做数学题做的严重怀疑自己的智商. #include <iostream> #include <cstdio> #include <cstdlib> #include<algorithm> #include<ma

POJ 2409 Let it Bead(Polya简单应用)

Let it Bead 大意:给你m种颜色,n个珠子串起来,旋转跟反转相同算相同,问有多少种不同的涂色组合方式. 思路:Polya的简单应用. 1 /************************************************************************* 2 > File Name: POJ2409.cpp 3 > Author: GLSilence 4 > Created Time: 2014年07月29日 星期二 22时56分58秒 5 **

poj 1286 Necklace of Beads &amp; poj 2409 Let it Bead(初涉polya定理)

http://poj.org/problem?id=1286 题意:有红.绿.蓝三种颜色的n个珠子,要把它们构成一个项链,问有多少种不同的方法.旋转和翻转后相同的属于同一种方法. polya计数. 搜了一篇论文Pólya原理及其应用看了看polya到底是什么东东,它主要计算全部互异的组合的个数.对置换群还是似懂略懂.用polya定理解决问题的关键是找出置换群的个数及哪些置换群,每种置换的循环节数.像这种不同颜色的珠子构成项链的问题可以把N个珠子看成正N边形. Polya定理:(1)设G是p个对象

POJ 2409 Let it Bead

这个题和POJ 1286 是一个题,只不过那是一个颜色数量固定的题而这个不固定. 这是链接:POJ 1286 Necklace of Beads   下面是代码: #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #

POJ 2049 Finding Nemo 优先队列 STL

题目链接:http://poj.org/problem?id=2049 题目利用了<海底总动员>的情节,小丑鱼尼莫迷路了,他老爸去营救他便是题意. 题目给出了这样的地图,说是假设地图由墙和门组成,忽略墙的厚度,地图上有门,没有墙的地方是可以自由行动的问可以经过最少多少道门便可以营救到尼莫. 这个题给的数据是墙的交点为整数点,但鱼爸爸实在非墙的地方自由移动. 因此,这个题有两个难点: 1.如果建图保存地图 2.如何在地图上遍历 由于题目是给出一个点(x,y),来表示一段墙 我便用一对X,Y来表示

poj 1975 Median Weight Bead(传递闭包 Floyd)

链接:poj 1975 题意:n个珠子,给定它们之间的重量关系,按重量排序,求确定肯定不排在中间的珠子的个数 分析:因为n为奇数,中间为(n+1)/2,对于某个珠子,若有至少有(n+1)/2个珠子比它重或轻,则它肯定不排在中间 可以将能不能确定的权值初始化为0,能确定重量关系的权值设为1 #include<stdio.h> #include<string.h> int a[110][110]; int main() { int T,n,m,i,j,k,d,x,sum; scanf(

poj 3692 Kindergarten (最大团模板题)

题目链接:http://poj.org/problem?id=3692 Kindergarten Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5156   Accepted: 2512 Description In a kindergarten, there are a lot of kids. All girls of the kids know each other and all boys also know e