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++:

 #include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
const int MAX = 1e5 + ; __int64 sum, n; __int64 gcd(__int64 a, __int64 b)
{
if (b == ) return a;
return gcd(b, a%b);
} __int64 my_pow(__int64 a, __int64 m)
{
__int64 ans = ;
while (m)
{
if (m & ) ans *= a;
a *= a;
m >>= ;
}
return ans;
} int main()
{
while (scanf("%I64d", &n), n != -)
{
sum = ;
if (n <= )
{
printf("0\n");
continue;
}
for (__int64 i = ; i <= n; ++ i)
{
__int64 temp = gcd(i, n);
sum += my_pow(, temp);
}
if (n & )
sum += n * my_pow(, (n + ) >> );
else
{
sum += (n >> ) * my_pow(, (n + ) >> );
sum += (n >> ) * my_pow(, n >> );
}
printf("%I64d\n", sum / / n);
}
return ;
}

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

  1. hdu 1817 Necklace of Beads(Polya定理)

    Necklace of Beads Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  2. Necklace of Beads(polya计数)

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7451   Accepted: 3102 ...

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

      Description Beads of red, blue or green colors are connected together into a circular necklace of ...

  4. POJ1286 Necklace of Beads(Polya定理)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9359   Accepted: 3862 Description Beads ...

  5. poj1286 Necklace of Beads—— Polya定理

    题目:http://poj.org/problem?id=1286 真·Polya定理模板题: 写完以后感觉理解更深刻了呢. 代码如下: #include<iostream> #inclu ...

  6. Necklace of Beads(polya定理)

    http://poj.org/problem?id=1286 题意:求用3种颜色给n个珠子涂色的方案数.polya定理模板题. #include <stdio.h> #include &l ...

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

    Necklace of Beads 大意:3种颜色的珠子,n个串在一起,旋转变换跟反转变换假设同样就算是同一种,问会有多少种不同的组合. 思路:正规学Polya的第一道题,在楠神的带领下,理解的还算挺 ...

  8. POJ 1286 Necklace of Beads(项链的珠子)

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7874   Accepted: 3290 ...

  9. 数学计数原理(Pólya):POJ 1286 Necklace of Beads

    Necklace of Beads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7763   Accepted: 3247 ...

随机推荐

  1. Qt 找不到rc.exe

    Qt在window下出现编译错误: LINK : fatal error LNK1158: 无法运行“rc.exe” 解决: 找到rc.exe的放置路径,比如我的在下面: C:\Program Fil ...

  2. 一次看懂 Https 证书认证

    TLS 传输层安全性协定 TLS(Transport Layer Security),及其前身安全套接层 SSL(Secure Sockets Layer)是一种安全协议,目的是为网际网路通信,提供安 ...

  3. MOOC python笔记(三) 序列容器:字符串、列表、元组

    容器概念 容器是Python中的重要概念,分为有序与无序. 有序容器也称为序列类型容器,如:字符串.列表. 通用序列容器操作 容器连接+ 加号可以把两个序列连接成一个更大的容器,相加后两个序列的值并不 ...

  4. PHP代码审计基础-高级篇

    高级篇主要讲 1. 熟知各个开源框架历史版本漏洞. 2. 业务逻辑漏洞 3. 多线程引发的漏洞 4. 事务锁引发的漏洞 在高级篇审计中有很多漏洞正常情况下是不存在的只有在特殊情况下才有 PHP常用框架 ...

  5. httprunner-1-linux下搭建hrun(上)

    前言 相信不少小伙伴对开源项目 httprunner 都很感兴趣,我们来看下它的有哪些特点吧: 项目管理:新增项目.列表展示及相关操作,支持用例批量上传(标准化的HttpRunner json和yam ...

  6. 无情的Java 8 之 Stream和lambda表达式篇

    不好意思,最近刷小视频刷的有点上头 看到这图就不自觉的要来一句:"卧槽,无情" 好了,我要开始正经了 JAVA 8 已经推出有一段时间了, 相比之前, 我们操作集合的方式应该是这样 ...

  7. 你不知道的DIV+CSS的命名规则

    搜索引擎优化(seo)有很多工作要做,其中对代码的优化是一个很关键的步骤.为了更加符合SEO的规范,下面是目前比较好的CSS+DIV的命名规则 1DIV CLASS或者ID 页头:header 登录条 ...

  8. GStreamer基础教程11 - 与QT集成

    摘要 通常我们的播放引擎需要和GUI进行集成,在使用GStreamer时,GStreamre会负责媒体的播放及控制,GUI会负责处理用户的交互操作以及创建显示的窗口.本例中我们将结合QT介绍如何指定G ...

  9. deepin15.7挂载/home到单独的分区:

    1.首先打开Gpart分区编辑器,找一个空闲的分区,调整好分区大小,格式化成ext4格式. 具体步骤为首先unmount所用到的盘,然后右击该盘选择'format to ext4',最后点击apply ...

  10. WARNING: The host 'WeiLei' could not be looked up with resolveip.

    [root@WeiLei data]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysq ...