Invoker
On of Vance’s favourite hero is Invoker, Kael. As many people knows Kael can control the elements and combine them to invoke a powerful skill. Vance like Kael very much so he changes the map to make Kael more powerful.
In his new map, Kael can control n kind of elements and he can put m elements equal-spacedly on a magic ring and combine them to invoke a new skill. But if a arrangement can change into another by rotate the magic ring or reverse the ring along the axis, they will invoke the same skill. Now give you n and m how many different skill can Kael invoke? As the number maybe too large, just output the answer mod 1000000007.
Input
The first line contains a single positive integer T( T <= 500 ), indicates the number of test cases.
For each test case: give you two positive integers n and m. ( 1 <= n, m <= 10000 )
Output
For each test case: output the case number as shown and then output the answer mod 1000000007 in a line. Look sample for more information.
Sample Input
2
3 4
1 2
Sample Output
Case #1: 21
Case #2: 1
Hint
For Case #1: we assume a,b,c are the 3 kinds of elements.
Here are the 21 different arrangements to invoke the skills
/ aaaa / aaab / aaac / aabb / aabc / aacc / abab /
/ abac / abbb / abbc / abcb / abcc / acac / acbc /
/ accc / bbbb / bbbc / bbcc / bcbc / bccc / cccc /
题意:
有n种颜色和一条有m个珠子的项链,问考虑旋转(围绕中心)和翻转(围绕对称轴)之后为同一条项链,那么一共可以有多少种不同的项链。
思路:
裸的polya定理:
m个旋转置换,每个旋转置换的循环节是
GCD(m,i
G
C
D
(
m
,
i
),那么这里的不动点总数就是
∑mk=1nGCD(m,i)
∑
k
=
1
m
n
G
C
D
(
m
,
i
)
。
对称置换就要根据m的奇偶性来讨论,m为奇数时,m个置换,每个置换有1个长度为1的循环和(m-1)/2个长度为2的循环,总数是
m∗n(m+1)/2
m
∗
n
(
m
+
1
)
/
2
。
m为偶数时,m/2个置换,每个置换长度均为2;另m/2个置换,每个置换都是2个长度为1的循环加上(n-2)/2个长度为2的循环,总数是
nm/2+n/2∗nm/2+1
n
m
/
2
+
n
/
2
∗
n
m
/
2
+
1
。
最后总数除以2*n即可,因为一共有2*n个置换,注意这个除实际是乘逆元,因为题目是模p意义下的。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
ll qp(ll a,ll n)
{
if (n == 0) return 1;
ll ans = qp(a,n/2) % mod;
ans *= ans;
ans %= mod;
if (n&1) ans *= a;
return ans % mod;
}
ll gcd(ll a,ll b)
{
if (b == 0) return a;
return gcd(b,a%b);
}
int main()
{
int t;
scanf("%d",&t);
int kase = 0;
while (t--)
{
printf("Case #%d: ",++kase);
int n,m;
scanf("%d%d",&m,&n);
ll sum1 = 0;
for (int i = 1;i <= n;i++)
{
ll tmp = gcd(i,n);
sum1 += qp(m,tmp);
sum1 %= mod;
}
ll sum2 = 0,ans = 0;
if (n&1)
{
ll tmp = n * qp(m,(n+1)/2) % mod;
sum2 += tmp;
}
else
{
ll tmp = qp(m,n/2+1) * n / 2 % mod;
sum2 += tmp;
sum2 %= mod;
tmp = qp(m,n/2) * n / 2 % mod;
sum2 += tmp;
sum2 %= mod;
}
ans = sum1 + sum2;
ans %= mod;
ans = ans * qp(n*2,mod-2) % mod;
cout << ans << endl;
}
return 0;
}