Necklace of Beads
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 7315 | Accepted: 3042 |
Description
Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < 24 ). If the repetitions that are produced by rotation around the center of the circular necklace or reflection to the axis of symmetry are all neglected, how many different forms of the necklace are there?
![](https://i-blog.csdnimg.cn/blog_migrate/ef14d94bd1c6c38e0f0bb54a7967d6e1.jpeg)
Input
The input has several lines, and each line contains the input data n.
-1 denotes the end of the input file.
-1 denotes the end of the input file.
Output
The output should contain the output data: Number of different forms, in each line correspondent to the input data.
Sample Input
4 5 -1
Sample Output
21 39
题意:c(本题为3)种颜色,s个珠子,穿成项链的方案数,旋转和翻转视为同一方案。
旋转的方案很好算,旋转i次的方案数为c^(gcd(i,s)),
对于翻转的就需要分奇偶两种情况了,
(1).n若为奇数,则方案数恒为c^(s/2+1);
(2).n若为偶数,则当i为奇数方案数为c^(s/2+1),当i为偶数方案数为c^(s/2),将所有的方案数加起来再除以2s就可以了。。
code:
#include<cstdio>
#include<cmath>
using namespace std;
typedef __int64 ll;
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int c=3,s;
ll polya(){
int i,j;
ll ans=0;
for(i=0;i<s;i++)
ans+=(ll)pow(1.0*c,gcd(s,i));
if(s%2)
ans+=s*(ll)pow(1.0*c,s/2+1);
else
{
ans+=s/2*(ll)pow(1.0*c,s/2);
ans+=s/2*(ll)pow(1.0*c,s/2+1);
}
return ans/2/s;
}
int main(){
ll ans;
while(scanf("%d",&s)){
if(s==-1)break;
if(s==0)ans=0;
else ans=polya();
printf("%I64d\n",ans);
}
return 0;
}
Let it Bead
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5033 | Accepted: 3343 |
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
![](https://i-blog.csdnimg.cn/blog_migrate/d7fedfc9cfc7b0452c2168c09b64f88e.jpeg)
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
c种颜色,s个珠子,穿成项链的方案数,旋转翻转视为同种
code:
#include<cstdio>
#include<cmath>
using namespace std;
typedef __int64 ll;
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int c,s;
ll polya()
{
int i,j;
ll ans=0;
for(i=0;i<s;i++)
ans+=(ll)pow(1.0*c,gcd(s,i));
if(s%2)
ans+=s*(ll)pow(1.0*c,s/2+1);
else
{
ans+=s/2*(ll)pow(1.0*c,s/2);
ans+=s/2*(ll)pow(1.0*c,s/2+1);
}
return ans/2/s;
}
int main(){
ll ans;
while(scanf("%d%d",&c,&s)){
if(s==0&&c==0)break;
ans=polya();
printf("%I64d\n",ans);
}
return 0;
}
上个大数模板:
#include <cmath>
#include <cstdio>
#include <cstring>
#include<iostream>
using namespace std;
typedef long long ll;
ll pow(ll a, ll b, ll mod) //快速幂
{
ll res = 1;
while(b){
if(b & 1)
res = (res * a) % mod;
a = (a * a) % mod;
b = b >> 1;
}
return res;
}
ll gcd(ll n, ll m){
return m ? gcd(m, n%m) : n;
}
void Euild(ll a, ll b, ll &x, ll &y) //欧几里得扩展求a 关于 b 的逆元
{
if(0 == b){
x = 1, y = 0;
return ;
}
Euild(b, a%b, x, y);
ll flag = x;
x = y;
y = flag - a/b * y;
}
ll Polya(int num, int color, ll mod) //长度为 num 环, 颜色有color种
{
ll res = 0;
for(int i = 1; i <= num; i++){
res = (res + pow(color, gcd(num, i), mod)) % mod;
}
if(num & 1){
res = (res + (num * pow(color, (num+1)/2, mod)) % mod) % mod;
}
else{
res = (res + (pow(color, num/2+1, mod) + pow(color, num/2, mod)) * num/2) % mod;
}
ll x, y;
Euild(num<<1, mod, x, y);
x = (x % mod + mod) % mod;
return (res * x) % mod;
}
int main()
{
int cas, tt = 1;
int c, s;
while(cin>>c>>s){
if(c==0 && s==0)
break;
cout<<(s ? Polya(s, c, 1000000007) : 0)<<endl;
}
return 0;
}
太菜 只会套模板!!!