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.
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 )
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: 1HintFor 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 /
Source
Recommend
题目:由n个元素组成长度为m的魔法环,可以由旋转和翻转互相转化的为同一种,求方案数
也是模板Polya,只不过这里答案要取模,当求ans=ans/(2*n)时由于ans已经是取过模的了所以不能直接进行除法,可以由(1/x)%mod=(x^(mod-2))%mod来求分母,转化为乘法。
代码:
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<math.h>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define M 55
#define mod 1000000007
ll n,m;
ll gcd(ll a,ll b)
{
return b==0?a:gcd(b,a%b);
}
ll pow_mod(ll n, ll k) //快速幂求(n^k)%mod
{
if(n>=mod)
n=n%mod;
ll res=1;
while(k>0)
{
if(k&1)
res=res*n%mod;
n=n*n%mod;
k>>=1;
}
return res;
}
ll Polya(ll n,ll c)
{
if(n==0)
return 0;
ll i;
ll sum=0;
for(i=1;i<=n;i++)
sum=(sum+pow_mod(c,gcd(n,i)))%mod;
if(n&1)
{
sum=(sum+n*pow_mod(c,n/2+1))%mod;
}else{
sum=(sum+n/2*(pow_mod(c,n/2)+pow_mod(c,n/2+1))%mod)%mod;
}
sum=sum*pow_mod(2*n,mod-2)%mod;
return sum;
}
int main()
{
int T,cas=0;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&m,&n); //注意n,m顺序。。。
ll ans=Polya(n,m);
printf("Case #%d: %I64d\n",++cas,ans);
}
return 0;
}