poj 2888 Magic Bracelet

8 篇文章 0 订阅
3 篇文章 0 订阅

【题目】

Magic Bracelet

Time Limit: 2000MS Memory Limit: 131072K
Total Submissions: 5534 Accepted: 1789

Description

Ginny’s birthday is coming soon. Harry Potter is preparing a birthday present for his new girlfriend. The present is a magic bracelet which consists of n magic beads. The are m kinds of different magic beads. Each kind of beads has its unique characteristic. Stringing many beads together a beautiful circular magic bracelet will be made. As Harry Potter’s friend Hermione has pointed out, beads of certain pairs of kinds will interact with each other and explode, Harry Potter must be very careful to make sure that beads of these pairs are not stringed next to each other.

There infinite beads of each kind. How many different bracelets can Harry make if repetitions produced by rotation around the center of the bracelet are neglected? Find the answer taken modulo 9973.

Input

The first line of the input contains the number of test cases.

Each test cases starts with a line containing three integers n (1 ≤ n ≤ 109, gcd(n, 9973) = 1), m (1 ≤ m ≤ 10), k (1 ≤ k ≤ m(m − 1) ⁄ 2). The next k lines each contain two integers a and b (1 ≤ a, b ≤ m), indicating beads of kind a cannot be stringed to beads of kind b.

Output

Output the answer of each test case on a separate line.

Sample Input
4
3 2 0
3 2 1
1 2
3 2 2
1 1
1 2
3 2 3
1 1
1 2
2 2

Sample Output
4
2
1
0

Source

POJ Monthly–2006.07.30, cuiaoxiang


【分析】

burnside引理+dp矩阵乘法优化+欧拉函数优化
是不是感觉很毒瘤
http://blog.sina.com.cn/s/blog_d760eb2e0101do5j.html


【代码】

//poj 2888
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int p=9973;
int n,m,K,T,sum;
struct matrix
{
    int a[15][15];
    matrix operator * (const matrix &x) const
    {
        int i,j,k;
        matrix res;
        fo(i,1,m)
          fo(j,1,m)
          {
              res.a[i][j]=0;
              fo(k,1,m)
                res.a[i][j]=(res.a[i][j]+a[i][k]*x.a[k][j])%p;
          }
        return res;
    }
}ans,base;
inline int ksm(int x,int k)
{
    x=x%p;
    if(k==0) return 1;
    if(k==1) return x;
    int t=ksm(x,k>>1);
    if(k&1) return t*t%p*x%p;
    else return t*t%p;
}
inline int phi(int x)
{
    int r=x;
    for(int i=2;i*i<=x;i++)
      if(x%i==0)
      {
          r-=r/i;
          while(x%i==0) x/=i;
      }
    if(x>1) r-=r/x;
    return r%p;
}
inline int calc(int x)
{
    int i,j,now=0;
    matrix res=base,tmp=ans;
    while(x)
    {
        if(x&1) tmp=tmp*res;
        res=res*res;x>>=1;
    }
    fo(i,1,m) now=(now+tmp.a[i][i])%p;
    return now;
}
int main()
{
    int i,j,k,u,v;
    scanf("%d",&T);
    fo(i,1,10) ans.a[i][i]=1;
    while(T--)
    {
        sum=0;
        scanf("%d%d%d",&n,&m,&K);
        fo(i,1,m) fo(j,1,m) base.a[i][j]=1;
        while(K--)
        {
            scanf("%d%d",&u,&v);
            base.a[u][v]=base.a[v][u]=0;
        }
        for(i=1;i*i<=n;i++)
          if(n%i==0)
          {
              sum=(sum+phi(n/i)*calc(i)%p)%p;
              if(i*i!=n) sum=(sum+phi(i)*calc(n/i)%p)%p;
          }
        sum=sum*ksm(n,p-2)%p;
        printf("%d\n",sum);
    }
    return 0;
}
/*
1 3 2 0
*/
//浙江温州,浙江温州,江南皮革厂倒闭啦 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值