poj-2888 Magic Bracelet

Source: Internet
Author: User

Test instructions

Give the necklace of n beads and the M-type beads;

There is a K-pair relationship between the beads, these beads can not be adjacent;

Cannot be turned into the same necklace by rotation as essentially different;

To find the essence of the number of different necklaces, the answer to 9973 modulo;

N<=10^9,GCD (n,9973) =1,m<=10;


Exercises

This is obviously an issue of permutation counting;

Burnside lemma or choose POI?

The Burnside lemma, because the poi theorem on the color limit to be very broad!

First consider a displacement posture, rotate x beads;

The necklace is then divided into segments of the GCD (x,n) size, and the required number of invariant permutations requires that these segments be identical;

The problem is asking how many kinds of pendulum the beads can have in the chain of GCD (x,n) +1 length;

Here can be recursive to obtain, matrix optimization;

The specific matrix is related to the relationship between beads, not detailed;

At this point, if we enumerate 1-n's posture must be timed out, then enumeration n of the approximate;

For each approximate p of N, consider several numbers with N's gcd as p;

The number here is not greater than N, so here the number/p must be less than n/p;

And GCD is p so it must be coprime with n/p, then it is Euler function;

The implementation is not too complicated, but the details are very much?

Anyway, I did not add the opposite side WA a lot of a lot of hair (laughter);

Orz wzq god Ben;

Time complexity O (√N*M^3LOGN);



Code:


#include <stdio.h> #include <string.h> #include <algorithm> #define N 10#define mod 9973using namespace Std;struct matrix{int a[n][n];int val (int m) {int ret=0;for (int i=0;i<m;i++) Ret+=a[i][i];return ret%mod;} Friend Matrix operator * (Matrix X,matrix y) {matrix ret;memset (&ret,0,sizeof (Matrix)); for (int. i=0;i<n;i++) for ( int j=0;j<n;ret.a[i][j]%=mod,j++) for (int k=0;k<n;k++) ret.a[i][j]+=x.a[i][k]*y.a[k][j];return ret;}} Temp,t,in;matrix pow (Matrix X,int y) {matrix ret=in;while (y) {if (y&1) ret=ret*x;x=x*x;y>>=1;} return ret;} int pow (int x,int y) {int ret=1;while (y) {if (y&1) ret=ret*x%mod;x=x*x%mod;y>>=1;} return ret;} int phi (int x) {int ret=x;for (int i=2;i*i<=x;i++) {if (x%i==0) {ret/=i,ret*=i-1;while (x%i==0) x/=i;}} if (x!=1) ret/=x,ret*=x-1;return ret;} int main () {int c,tc,n,m,i,j,k,x,y,ans;scanf ("%d", &AMP;TC), for (i=0;i<n;i++) in.a[i][i]=1;for (c=1;c<=tc;c++) { scanf ("%d%d%d", &n,&m,&k); memset (&t,0,sizeof (T)); for (i=0;i<m;i++) for (j=0;j<m; j + +) T.a[i][j]=1;for (i=1;i<=k;i++) {scanf ("%d%d", &x,&y); x--, y--; t.a[x][y]=t.a[y][x]=0;} for (i=1,ans=0;i*i<=n;i++) {if (n%i==0) {Temp=pow (t,n/i); Ans+=phi (i)%mod*temp.val (m)%mod;ans%=mod;if (i*i==n) Continue;temp=pow (t,i); Ans+=phi (n/i)%mod*temp.val (M)%mod;ans%=mod;}} printf ("%d\n", Ans*pow (n%mod,mod-2)%mod);} return 0;}






poj-2888 Magic Bracelet

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.