POJ 2888 Magic Bracelet (Burnside lemma + matrix)

Source: Internet
Author: User

Test instructions: A necklace of length n, each bead dyed in M color. Some restrictions give some color beads that cannot be adjacent. The same is considered the same after rotation. How many different kinds of necklaces are there?

Idea: This problem is a bit comprehensive, first of all, we consider the factor I of each n, the number of invariant permutations under this factor I, and then multiplied by (n/i) Euler function added to the ANS, and then the ANS multiplied by N in the inverse of modulo p. As to how to find the number of invariant permutations under factor I, I believe we have done no restrictions, as for the restrictions, we can consider this: the initial array a[m][m] are 1, for each limit x, Y, make a[x][y]=a[y][x]=0, we have a series: B1,B2,B3, B4...BM, so for each i∈[1,m-1], there are a[bi][bi+1]=1, so think of what? is the matrix multiplication, for just the matrix A We let s=a^i, and then put each a[i][i] add up, so that means: each I start and return to I the number of methods, so it becomes the number of invariant permutation under factor i!

1#include <algorithm>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <iostream>6 Const intMod=9973;7 inta[ the][ the],b[ the][ the],c[ the][ the],s[ the][ the],n,m,k;8 intRead () {9     CharCh=GetChar ();Ten     intt=0, f=1; One      while(ch<'0'|| Ch>'9'){ A         if(ch=='-') f=-1; -Ch=GetChar (); -     } the      while('0'<=ch&&ch<='9'){ -t=t*Ten+ch-'0'; -Ch=GetChar (); -     } +     returnt*F; - } + voidEXGCD (intAintBint&x,int&y) { A     if(b==0){ atx=1; -y=0; -         return; -     } -EXGCD (b,a%b,x,y); -     intt=x; inx=y; -Y= (t-a/b*y); to } + intInvintN) { -     inta,b,x,y; theA=n; b=Mod; * EXGCD (a,b,x,y); $     return(x+mod)%Mod;Panax Notoginseng } - intEulerintN) { the     intRes=n,a=N; +      for(intI=2; i*i<=n;i++) A         if(a%i==0){ theres=res/i* (I-1); +              while(a%i==0) a/=i; -         } $     if(a>1) res=res/a* (A-1); $     returnres%Mod; - } - voidmult_matrix1 () { the      for(intI=1; i<=m;i++) -       for(intj=1; j<=m;j++)Wuyic[i][j]=0; the      for(intI=1; i<=m;i++) -       for(intj=1; j<=m;j++) Wu        for(intk=1; k<=m;k++) -C[i][j]= (C[i][j]+s[i][k]*b[k][j])%Mod; About      for(intI=1; i<=m;i++) $       for(intj=1; j<=m;j++) -s[i][j]=C[i][j];  - } - voidmult_matrix2 () { A      for(intI=1; i<=m;i++) +       for(intj=1; j<=m;j++) thec[i][j]=0; -      for(intI=1; i<=m;i++) $       for(intj=1; j<=m;j++) the        for(intk=1; k<=m;k++) theC[i][j]= (C[i][j]+b[i][k]*b[k][j])%Mod; the      for(intI=1; i<=m;i++) the       for(intj=1; j<=m;j++) -b[i][j]=C[i][j];  in } the intWorkintx) { the      for(intI=1; i<=m;i++) About       for(intj=1; j<=m;j++) theb[i][j]=a[i][j],s[i][j]=0; the     intans=0;  the      for(intI=1; i<=m;i++) +s[i][i]=1;  -      while(x) { the         if(%2) mult_matrix1 ();Bayi mult_matrix2 (); theX/=2; the     }   -      for(intI=1; i<=m;i++) ans= (Ans+s[i][i])%Mod; -     returnans; the } the intMain () { the     intt=read (); the      while(t--){ -N=read (); M=read (); k=read (); the         for(intI=1; i<=m;i++) the           for(intj=1; j<=m;j++) thea[i][j]=1;94         for(intI=1; i<=k;i++){ the             intX=read (), y=read (); thea[x][y]=a[y][x]=0; the        }98        intans=0; About         for(intI=1; i*i<=n;i++) -         if(n%i==0){101Ans= (Ans+work (i) *euler (n/i))%Mod;102             if(i*i!=N)103Ans= (Ans+work (n/i) *euler (i))%Mod;104         } theAns= (ANS*INV (n%mod))%Mod;106printf"%d\n", ans);107     }108}

POJ 2888 Magic Bracelet (Burnside lemma + matrix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.