POJ 2888 Magic Bracelet (polya+ matrix fast Power)

Source: Internet
Author: User

Magic Bracelet
Time Limit: 2000MS Memory Limit: 131072K
Total Submissions: 4990 Accepted: 1610

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 IS m kinds of different magic beads. Each kind of beads have its unique characteristic. Stringing many beads together a beautiful circular magic bracelet would be made. As Harry Potter's friend Hermione had pointed out, beads of certain pairs of kinds would interact with each other and Explo DE, Harry Potter must is very careful to make sure that beads of these pairs is not stringed next to all other.

There infinite beads of each kind. How many different bracelets can Harry do if repetitions produced by rotation around the center of the bracelet is NEGL Ected? 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≤ km(m − 1) ⁄2). The next k lines each contain, integers a and b (1≤a, bm), I ndicating 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

43 2 03 2 11 23 2 21 11 23 2 31 11 22 2

Sample Output

4210

Source

POJ monthly--2006.07.30, Cuiaoxiang

/*poj 2888 Magic bracelet (polya+ matrix fast Power) gives you m different beads to form a necklace of length n (personal understanding), considering only how many different necklaces can be in total rotation. And the rule that some beads can not be together roughly or Polya count, just to find a way to figure out the length of the circular section of I how many.   It is possible to consider the Matrix Fast power (a classic problem: solving M-step from A to B of the scheme number) is mainly always tle, know later to change Euler to pass the prime numbers to get through-. And too much mold seems to tlehhh-2016-04-19 22:31:42*/#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<functional>using namespacestd;#defineLson (i<<1)#defineRson ((i<<1) |//typedef long Long ll;using namespacestd;Const intMAXN =40010;structmatrix{intma[ A][ A];};intn,m;intISPRIME[MAXN];intPRIME[MAXN];intPnum;voidGet_prime () {; Pnum=0; memset (IsPrime,-1,sizeof(IsPrime));  for(inti =2; I <= maxn-Ten; i + +)        if(Isprime[i]) {Prime[pnum++] =i;  for(intj = i * I; J <= maxn-Ten; J + =i) isprime[j]=0; }}matrix mult (Matrix ta,matrix TB,intMoD)    {Matrix TC; memset (Tc.ma,0,sizeof(tc.ma));  for(inti =0; I < m; i + +)         for(intK =0; K < M; K + +)            if(Ta.ma[i][k]) { for(intj =0; J < M; J + +)                    if(Tb.ma[k][j]) tc.ma[i][j]= (Tc.ma[i][j] + ta.ma[i][k] * tb.ma[k][j])%MoD; }    returnTC;} Matrix Mat_pow (Matrix TA,intNintMoD)    {Matrix T; memset (T.ma,0,sizeof(t.ma));  for(inti =0; I < m; i++) T.ma[i][i]=1;  while(n) {if(N &1) T =mult (T,TA,MOD); Ta=mult (TA,TA,MOD); N>>=1; }    returnt;}intPow_mod (intAintNintMoD) {    intRET =1; A%=MoD;  while(n) {if(N &1) ret = ret*a,ret%=MoD; A= A *A; A%=MoD; N>>=1; }    returnret;}intMoD;intEulerintcur) {    intans, X; Ans= x =cur;  for(inti =0; I < Pnum && Prime[i] * prime[i] <= cur; i++)        if(x% prime[i] = =0) {ans= ans/prime[i] * (prime[i)-1);  while(x% prime[i] = =0) x/=Prime[i]; }    if(X >1) ans= ans/x * (X-1); returnans%MoD;} Matrix Mat;intCalintLen) {    intRET =0; Matrix T=Mat_pow (MAT,LEN,MOD);  for(inti =0; I < m; i++) ret= ret +T.ma[i][i]; RET%=MoD; returnret;}intPolya (intN) {    intAns =0;  for(intI=1; I*i <= N; i++)    {        if(n% i = =0)        {            if(I*i = =N) {ans= Ans+cal (i) *Euler (i); Ans%=MoD; }            Else{ans= (Ans+cal (i) *euler (n/i) +cal (n/i) *Euler (i)); Ans%=MoD; }}} ans= Ans*pow_mod (n,mod-2, MoD); returnAns%MoD;}intMain () {intT;    Get_prime (); MoD=9973; scanf ("%d",&T);  while(t--)    {        intK; scanf ("%d%d%d",&n,&m,&k); intu,v;  for(inti =0; I < m; i++)             for(intj =0; J < M; J + +) Mat.ma[i][j]=1;  for(inti =1; I <= K; i++) {scanf ("%d%d",&u,&v); Mat.ma[u-1][v-1] = mat.ma[v-1][u-1] =0; } printf ("%d\n", Polya (n)); }    return 0;}

POJ 2888 Magic Bracelet (polya+ matrix fast Power)

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.