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.
For each test case: give you two positive integers n and m. ( 1 <= n, m <= 10000 )
2 3 4 1 2
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 /
//
#include <cstdio>
const int mod=1000000007;
int fun(int a,int n)
{
if(n==1) return a%mod;
if(n==0) return 1;
long long t=fun(a,n/2);
t=(t*t)%mod;//important
if(n&1) return (t*a)%mod;
else return t%mod;
}
int euler(int n)
{
int ans = n;
for (int i = 2; i <= n; i++) if (n % i == 0)
{
ans -= ans / i;
while (n % i == 0) n /= i;
}
if (n > 1) ans -= ans / n;
return ans;
}
//求(a/b)%p,gcd(B,p)=1
__int64 exgcd(__int64 a,__int64 b,__int64& x,__int64& y)
{
if(b==0) return x=1,y=0,a;
__int64 r=exgcd(b,a%b,x,y);
__int64 t=x;
x=y;
y=t-(a/b)*y;
return r;
}
__int64 calc(__int64 a,__int64 b,__int64 mod)
{
__int64 x,y;
__int64 r=exgcd(b,mod,x,y);
x*=a;
x=(x%mod+mod)%mod;
return x;
}
int main()
{
int n, c;
int cas = 1, T;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &c, &n);
if (n == 0)
{
printf("0\n");
continue;
}
__int64 ans = 0;
for (int i = 1; i <= n; i++)
if (n % i == 0)
{
ans += (long long)fun(c, i) * euler(n / i);
ans %= mod;
}//枚举个数
if (n & 1)
{
ans += (long long)n * fun(c, n / 2 + 1);
ans %= mod;
}
else
{
ans += (long long)n / 2 * (fun(c, n / 2) + fun(c, n / 2 + 1));
ans %= mod;
}
// ans /= (2 * n);
// ans %= mod;
ans=calc(ans,(long long)2*n,(long long)mod);
printf("Case #%d: %I64d\n",cas++, ans);
}
return 0;
}