POJ 1286 解题报告

这道题是Polya题。我第一次接触到这个定理。看了别人的解题报告做的这道题:

看过的最好的解释:http://www.cnblogs.com/mcflurry/archive/2012/06/20/2556071.html

ACM中的数学问题:http://www.doc88.com/p-910707819004.html (莫名其妙的打不开50页之后的PPT)http://wenku.baidu.com/view/b1eb7cfdaef8941ea76e05f4.html

还是不清楚轮换个数(循环节)的求法,再遇到再深入吧。

这里,long long的表示空间比double大,因为我们没有小数部分。

thestoryofsnow1286Accepted208K0MSC++1317B
/* 
ID: thestor1 
LANG: C++ 
TASK: poj1286 
*/
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <limits>
#include <string>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cassert>

using namespace std;

int gcd(int a, int b) {
	while (b != 0) {
		int r = a % b;
		a = b;
		b = r;	
	}
	return a;
}

// Polya: http://wenku.baidu.com/view/b1eb7cfdaef8941ea76e05f4.html
// S = (m^c_1 + m^c_2 + ... + m^c_G) / |G|

// Analysis: http://www.cnblogs.com/mcflurry/archive/2012/06/20/2556071.html
int main()
{
	int n;	
	while (scanf("%d", &n) > 0 && n >= 0) {
		// edge case
		if (n == 0) {
			printf("0\n");
			continue;
		}

		long long ans = 0;
		
		// rotation
		for (int i = 0; i < n; ++i) {
			ans += pow(3.0, (double)gcd(i, n));
		}

		// reflection
		if (n & 1) {
			// if odd
			ans += n * pow(3.0, (double) (n - 1) / 2 + 1);
		} else {
			// if even
			// the axis of symmetry is the line between a point and its opposite point
			ans += n / 2 * pow(3.0, (double) (n - 2) / 2 + 2);
			// the axis of symmetry is the line between the middle of two adjcent points and its opposite
			ans += n / 2 * pow(3.0, (double) n / 2);
		}
		
		printf("%lld\n", ans / (2 * n));
	
	}
	return 0; 
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值