HDU 3293 Invoker

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的循环,总数是 mn(m+1)/2 m ∗ n ( m + 1 ) / 2
m为偶数时,m/2个置换,每个置换长度均为2;另m/2个置换,每个置换都是2个长度为1的循环加上(n-2)/2个长度为2的循环,总数是 nm/2+n/2nm/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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值