[代码]POJ 2409 Let it Bead

Abstract

POJ 2409 Let it Bead

Polya定理

 

Body

#include <cstdio>
#include <cstring>
using namespace std;

typedef long long LL;

const int MaxN = 33;

LL C, S, G, Ans;

LL Pow(LL x, LL p)
{
LL Res = 1LL;
for (; p; p >>= 1)
{
if (p & 1) Res *= x;
x *= x;
}
return Res;
}

LL GCD(LL p, LL q)
{
if (q) return GCD(q, p%q);
return p;
}

int main()
{
while(scanf("%lld%lld", &C, &S), S||C)
{
G = S<<1;
Ans = 0LL;
if (S & 1)
Ans += S*Pow(C, (S>>1)+1);
else
{
Ans += Pow(C, S>>1)*(S>>1);
Ans += Pow(C, (S>>1)+1)*(S>>1);
}
for (int i = 1; i <= S; ++i)
Ans += Pow(C, GCD(S, i));
Ans /= G;
printf("%lld\n", Ans);
}
return 0;
}

 

Reference

你可能感兴趣的:(poj)