D - Magic Bracelet解题报告(来自网络)

D - Magic Bracelet
Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u

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 nmagic 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 ≤ 109gcd(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 ≤ ab ≤ 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

题目大意:给定n(n <= 10^9)颗珠子和m种颜色,这n颗珠子可以组成一串项链,每颗珠子可以用m中颜色来填充,如果两种涂色方法通过旋转项链可以得到,则两种涂色方法等价。 现在再给定K组限制,每组限制a、b代表颜色a和颜色b不能涂在相邻的珠子上面。问一共有多少种涂色方法。
解题思路:这题和POJ2154 题目大致相同,不同的是这里多了一个限制条件,即可能两种颜色不能涂在相邻的珠子上面。对于这种情况我们可以通过矩阵连乘得到,先初始化矩阵 array[i][j]为1.如果颜色a和颜色b不能涂在相邻的珠子,那么array[a][b] = array[b][a] = 0; 对于具有n/L个循环节的置换(L为每个循环节的长度)。先求出array[][]的n/L次幂,然后将这个矩阵的array[i][i] (1<=i<=m)全部加起来即为这种置换下涂色不变的方法数。对于为什么这样做,仔细想一想就清楚了,其余的算法基本和POJ 2154相同,这里就不解释了。


#include <cstdlib>
#include <iostream>
#include <cstdio>
#include <cstring>
 
using namespace std;
 
const int N=11,mod=9973;
int n,m;
struct Matrix
{
     int a[N][N];       
     int n;
     Matrix operator*(Matrix l)
     {
         Matrix temp;
         temp.n=n;
         for(int i=0;i<n;i++)
          for(int j=0;j<n;j++)
          {
                temp.a[i][j]=0;  
             for(int k=0;k<n;k++)
             {
                temp.a[i][j]+=a[i][k]*l.a[k][j];     
                temp.a[i][j]%=mod;
             } 
          } 
          return temp;      
     }
}M;
 
Matrix pow(Matrix t,int x)
{
    Matrix temp=t,ans;
    while(x)
    {
       if(x&1)  ans=ans*temp;
       temp=temp*temp;
       x>>=1;        
    } 
    return ans;      
}
 
int phi(int n)
{
    int ans=1;
    for(int i=1;i*i<=n;i++)
    {
        if(n%i==0)
        {
           ans*=(i-1);          
           n/=i;
           while(n%i==0)
           {
                ans*=i;
                n/=i;            
           }  
           ans%=mod;     
        }
    }    
    if(n>1) ans*=(n-1);
    return ans%mod;
}
 
int gettr(int x)
{
    Matrix t=pow(M,x);
    int res=0;
    for(int i=0;i<t.n;i++)
      res=(res+t.a[i][i])%mod;
    return res;      
}
int main(int argc, char *argv[])
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
         scanf("%d%d",&n,&m);
         int k;
         for(int i=0;i<n;i++)
          for(int j=0;j<n;j++)
            M.a[i][j]=1;          
         scanf("%d",&k);   
         for(int i=0;i<k;i++)
         {
             int x,y;
             scanf("%d%d",&x,&y);
             M.a[x-1][y-1]=M.a[y-1][x-1]=0;        
         }
         int ans=0;
         for(int i=2;i*i<=n;i++)
           if(n%i==0)
           {
               ans=(ans+gettr(i)*phi(n/i)%mod)%mod;          
               if(i*i<n)
                 ans=(ans+gettr(n/i)*phi(i)%mod)%mod; 
           }
          cout<<ans<<endl; 
              int tt;
         for(tt=1;;tt++) 
              if(((long long)n*tt-ans)%mod==0)
                   break;
         printf("%d\n",tt%mod);  
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值