poj-2888-矩阵+polya

Time Limit: 2000MS   Memory Limit: 131072K
Total Submissions: 6195   Accepted: 1969

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 ≤ 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

Source

  用 inv[i]=(mod-mod/i)*inv[mod%i]的时候要保证i<mod才可,我一直WA最后换了种求逆元的方法就好了。
   题意是给出n个珠子组成一个环,m种颜色,k种限制,问不同的涂色方法(旋转可达的算同一种),还是找不动点,等价于求
d个珠子组成一个环的所有方案(重复的也算),m很小考虑矩阵,把可达矩阵A建立起来,res=A^(d) 表示选完d+1个珠子后的矩阵,
res[i][j]即第一个珠子是i最后一个珠子是j的方案个数,我们统计所有的res[i][i]就好了,等价于把最后一个珠子看做第一个珠子了。
其实就等价于求从i出发回到i的方案个数,这么说好理解点。
  
  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<map>
  5 #include<set>
  6 #include<vector>
  7 #include<algorithm>
  8 #include<cmath> 
  9 using namespace std;
 10 #define LL long long 
 11 #define PI acos(-1.0)
 12 LL mod=9973;
 13 LL N,M,K;
 14 vector<LL>prime;
 15 bool isp[33333];
 16 struct matrix{
 17     LL a[11][11];
 18     matrix(){
 19         memset(a,0,sizeof(a));
 20     }
 21     matrix operator*(matrix &tmp){
 22         matrix ans;
 23         for(int i=1;i<=M;++i){
 24             for(int j=1;j<=M;++j){
 25                 for(int k=1;k<=M;++k){
 26                     (ans.a[i][j]+=a[i][k]*tmp.a[k][j]);
 27                 }
 28                 ans.a[i][j]%=mod;
 29             }
 30         }
 31         return ans;
 32     }
 33 }A,U;
 34 matrix qpow(matrix A,int b){
 35     matrix ans=U;
 36     while(b){
 37         if(b&1) ans=ans*A;
 38         A=A*A;
 39         b>>=1;
 40     }
 41     return ans;
 42 }
 43 void init(){
 44     for(int i=2;i<33333;++i){
 45         if(!isp[i]) prime.push_back(i);
 46         for(int j=0;j<prime.size()&&prime[j]*i<33333;++j){
 47             isp[i*prime[j]]=1;
 48             if(i%prime[j]==0)break;
 49         }
 50     }
 51 }
 52 LL phi(int n){
 53     LL ans=n,m=sqrt(n+0.5);
 54     for(int i=0;prime[i]<=m;++i){
 55         if(n%prime[i]==0){
 56             ans=ans/prime[i]*(prime[i]-1);
 57             while(n%prime[i]==0)n/=prime[i];
 58         }
 59     }
 60     if(n>1) ans=ans/n*(n-1);
 61     return ans%mod;
 62 }
 63 LL _qpow(LL a,LL b){
 64     LL r=1;
 65     while(b){
 66         if(b&1) r=r*a%mod;
 67         a=a*a%mod;
 68         b>>=1;
 69     }
 70     return r;
 71 }
 72 LL solve(int n){
 73     matrix res=qpow(A,n);
 74     LL ans=0;
 75     for(int i=1;i<=M;++i) ans+=res.a[i][i];
 76     return ans%mod;
 77 }
 78 int main()
 79 {
 80     int t,i,j,k,u,v;
 81     init();
 82     for(i=0;i<11;++i)U.a[i][i]=1;
 83     scanf("%d",&t);
 84     while(t--){
 85         scanf("%lld%lld%lld",&N,&M,&K);
 86         for(i=1;i<=M;++i)
 87         for(j=1;j<=M;++j)A.a[i][j]=1;
 88         for(i=1;i<=K;++i){
 89             scanf("%d%d",&u,&v);
 90             A.a[u][v]=A.a[v][u]=0;
 91         }
 92         LL ans=0;
 93         for(i=1;i*i<=N;++i){
 94             if(N%i==0){
 95                 ans=(ans+phi(N/i)*solve(i)%mod)%mod;
 96                 if(i*i!=N) ans=(ans+phi(i)*solve(N/i)%mod)%mod;
 97             }
 98         }
 99         ans=ans*_qpow(N,mod-2)%mod;
100         printf("%lld\n",ans);
101     }
102     return 0;
103 }

 

posted @ 2018-08-10 14:34  *zzq  阅读(236)  评论(0编辑  收藏  举报