hdu 1817 Necklace of Beads (polya)

Necklace of Beads

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1049    Accepted Submission(s): 378

Problem Description

Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < 40 ). 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

help

C/C++:

 1 #include <map>
 2 #include <queue>
 3 #include <cmath>
 4 #include <vector>
 5 #include <string>
 6 #include <cstdio>
 7 #include <cstring>
 8 #include <climits>
 9 #include <iostream>
10 #include <algorithm>
11 #define INF 0x3f3f3f3f
12 #define LL long long
13 using namespace std;
14 const int MAX = 1e5 + 10;
15
16 __int64 sum, n;
17
18 __int64 gcd(__int64 a, __int64 b)
19 {
20     if (b == 0) return a;
21     return gcd(b, a%b);
22 }
23
24 __int64 my_pow(__int64 a, __int64 m)
25 {
26     __int64 ans = 1;
27     while (m)
28     {
29         if (m & 1) ans *= a;
30         a *= a;
31         m >>= 1;
32     }
33     return ans;
34 }
35
36 int main()
37 {
38     while (scanf("%I64d", &n), n != -1)
39     {
40         sum = 0;
41         if (n <= 0)
42         {
43             printf("0\n");
44             continue;
45         }
46         for (__int64 i = 1; i <= n; ++ i)
47         {
48             __int64 temp = gcd(i, n);
49             sum += my_pow(3, temp);
50         }
51         if (n & 1)
52             sum += n * my_pow(3, (n + 1) >> 1);
53         else
54         {
55             sum += (n >> 1) * my_pow(3, (n + 2) >> 1);
56             sum += (n >> 1) * my_pow(3, n >> 1);
57         }
58         printf("%I64d\n", sum / 2 / n);
59     }
60     return 0;
61 }

原文地址:https://www.cnblogs.com/GetcharZp/p/9557245.html

时间: 2024-10-09 15:02:27

hdu 1817 Necklace of Beads (polya)的相关文章

hdu 1817 Necklace of Beads(Polya定理)

题目链接:hdu 1817 Necklace of Beads 这题的弱化版:传送门 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;++i) 3 using namespace std; 4 typedef long long ll; 5 6 int t,n,m,cas; 7 8 ll mypow(ll a,ll k){ 9 ll an=1; 10 while(k){ 11 if(k&1)an=an*a

hdu 1817 Necklace of Beads(Polya定理)

Necklace of Beads Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 630    Accepted Submission(s): 232 Problem Description Beads of red, blue or green colors are connected together into a circular

[ACM] POJ 1286 Necklace of Beads (Polya计数,直接套公式)

Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6547   Accepted: 2734 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 pro

poj 1286 Necklace of Beads (polya(旋转+翻转)+模板)

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 ar

POJ 1286 Necklace of Beads(Polya简单应用)

Necklace of Beads 大意:3种颜色的珠子,n个串在一起,旋转变换跟反转变换如果相同就算是同一种,问会有多少种不同的组合. 思路:正规学Polya的第一道题,在楠神的带领下,理解的还算挺快的,代码没什么好说的,裸的Polya,也不需要优化. 1 /************************************************************************* 2 > File Name: POJ1286.cpp 3 > Author: GLSil

POJ 题目1286 Necklace of Beads(Polya定理)

Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7061   Accepted: 2942 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 pro

poj1286 Necklace of Beads【裸polya】

很裸的polya,不过我看polya看了很久 吉大ACM模板里面也有 #include <cstdio> #include <cmath> #include <iostream> using namespace std; long long gcd(long long a,long long b) { return b==0?a:gcd(b,a%b); } int main() { #ifndef ONLINE_JUDGE //freopen("G:/1.tx

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 1286】Necklace of Beads(polya定理)

[POJ 1286]Necklace of Beads(polya定理) Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7550   Accepted: 3145 Description Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n <