HDU 3923 Invoker

Invoker

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 122768/62768 K (Java/Others)
Total Submission(s): 2013    Accepted Submission(s): 928


Problem Description

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 /
 

题意

用m种颜色给n个珠子染色,两个方案不同当且仅当满足旋转或者翻转后不同,求一共有多少不同的方案。

 

分析

polya计数原理,和poj1286差不多。

答案对1e9+7取模,最后在除以总的置换数时,需要用到逆元。

code

 1 #include<cstdio>
 2 
 3 typedef long long LL;
 4 const long long mod = 1e9+7; //-
 5 
 6 LL gcd(LL a,LL b) {
 7     if (b==0) return a;
 8     return gcd(b,a%b);
 9 }
10 LL ksm(LL a,LL b) {
11     LL ans = 1;
12     while (b) {
13         if (b & 1) ans = (ans * a) % mod;
14         b >>= 1;
15         a = (a * a) % mod;
16     }
17     return ans;
18 }
19 LL Polya(LL n,LL m) {
20     LL ans = 0;
21     for (LL i=1; i<=n; ++i) 
22         ans = (ans + ksm(m,gcd(n,i))) % mod;
23     if (n & 1) ans = (ans + n * ksm(m,(n-1)/2+1)) % mod;
24     else {
25         ans = (ans + n/2*ksm(m,(n-2)/2+2)) % mod;
26         ans = (ans + n/2*ksm(m,n/2)) % mod;    
27     }
28     ans = (ans * ksm(n*2,mod-2)) % mod;
29     return ans;
30 }
31 int main () {
32     int t;LL n,m;
33     scanf("%d",&t);
34     for (int c=1; c<=t; ++c) {
35         scanf("%lld%lld",&m,&n); // -
36         printf("Case #%d: %lld\n",c,Polya(n,m));
37     }
38     return 0;
39 }

 

 

posted @ 2018-02-06 20:26  MJT12044  阅读(283)  评论(0编辑  收藏  举报