目录

​一,波利亚计数​

​二,OJ实战​

​POJ - 1286 Necklace of Beads​

​POJ - 2409 Let it Bead​

​POJ - 2154 Color​


一,波利亚计数


二,OJ实战

POJ - 1286 Necklace of Beads

题目:

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? 

Input

The input has several lines, and each line contains the input data n. 

-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



公式:

波利亚计数POJ - 1286 Necklace of Beads_i++

代码:

#include <iostream>
using namespace std;

int phi(int n)
{
int r = n;
for (int i = 2; i*i <= n; i++)
{
if (n%i == 0)
{
while (n%i == 0)n /= i;
r = r / i*(i - 1);
}
}
if (n > 1)r = r / n*(n - 1);
return r;
}

long long mi(int a, int m)
{
if (m == 0)return 1;
long long b = mi(a, m / 2);
b = b*b;
if (m % 2)b *= a;
return b;
}

int main()
{
int n;
while (cin >> n)
{
if (n < 0)break;
if (n == 0)
{
cout << 0 << endl;
continue;
}
long long ans = 0, i = 0;
while (++i*i < n)
{
if (n%i)continue;
ans += phi(n / i)*mi(3, i) + phi(i) *mi(3, n / i);
}
if (i*i == n)ans += phi(i)*mi(3, i);
ans /= n;
ans += (mi(3, (n + 1) / 2) + mi(3, n / 2 + 1)) / 2;
cout << ans / 2 << endl;
}
return 0;
}


16ms AC

根据这个代码,还可以得到一个更快的代码:

#include <iostream>
using namespace std;

int main()
{
int n,l[24] = { 0, 3, 6, 10, 21, 39, 92, 198, 498, 1219, 3210, 8418, 22913, 62415, 173088, 481598, 1351983, 3808083, 10781954, 30615354, 87230157, 249144711, 713387076, 2046856566 };
while (cin >> n)if (n < 0)break; else cout << l[n] << endl;
return 0;
}



POJ - 2409 Let it Bead

题目:

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

这个题目和POJ - 1286 Necklace of Beads其实是一样的

所以那个代码只需要改一点点就得到本题的代码:

#include <iostream>
using namespace std;

int phi(int n)
{
int r = n;
for (int i = 2; i*i <= n; i++)
{
if (n%i == 0)
{
while (n%i == 0)n /= i;
r = r / i*(i - 1);
}
}
if (n > 1)r = r / n*(n - 1);
return r;
}

long long mi(int a, int m)
{
if (m == 0)return 1;
long long b = mi(a, m / 2);
b = b*b;
if (m % 2)b *= a;
return b;
}

int main()
{
int m, n;
while (cin >> m >> n)
{
if (n == 0)break;
long long ans = 0, i = 0;
while (++i*i < n)
{
if (n%i)continue;
ans += phi(n / i)*mi(m, i) + phi(i) *mi(m, n / i);
}
if (i*i == n)ans += phi(i)*mi(m, i);
ans /= n;
ans += (mi(m, (n + 1) / 2) + mi(m, n / 2 + 1)) / 2;
cout << ans / 2 << endl;
}
return 0;
}

POJ - 2154 Color

题目:

Description

Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of the necklace can be produced. You should know that the necklace might not use up all the N colors, and the repetitions that are produced by rotation around the center of the circular necklace are all neglected. 

You only need to output the answer module a given number P. 

Input

The first line of the input is an integer X (X <= 3500) representing the number of test cases. The following X lines each contains two numbers N and P (1 <= N <= 1000000000, 1 <= P <= 30000), representing a test case.

Output

For each test case, output one line containing the answer.

Sample Input

5

1 30000

2 30000

3 30000

4 30000

5 30000

Sample Output

1

3

11

70

629

波利亚计数POJ - 1286 Necklace of Beads_#include_02

在计算这个式子mod p的时候,因为有除n这个运算,这是无法用同余解决的,

除非确定n和p互质,可以找逆元

本题采用的是另外一种方法,直接取定m就是n

那么上式化为

波利亚计数POJ - 1286 Necklace of Beads_i++_03

代码:

#include <iostream>
using namespace std;

int phi(int n)
{
int r = n;
for (int i = 2; i*i <= n; i++)
{
if (n%i == 0)
{
while (n%i == 0)n /= i;
r = r / i*(i - 1);
}
}
if (n > 1)r = r / n*(n - 1);
return r;
}

int mi(int a, int m, int p)
{
if (m == 0)return 1;
int b = mi(a, m / 2, p);
b = b*b%p;
if (m % 2)b *= a;
return b%p;
}

int main()
{
int t, n, p, ans;
cin >> t;
while (t--)
{
cin >> n >> p;
ans = 0;
int i = 0;
while (++i*i < n)
{
if (n%i)continue;
ans += phi(n / i) % p*mi(n%p, i - 1, p) % p + phi(i) % p*mi(n%p, n / i - 1, p) % p;
}
if (i*i == n)ans += phi(i) % p*mi(n%p, i - 1, p) % p;
cout << ans%p << endl;
}
return 0;
}

如果对称也算一种的话,就变成了另外一个题目:POJ - 1286 Necklace of Beads