Poj 2888 magic bracelet has restrictions (conditional Polay problems)

Source: Internet
Author: User
Poj 2888 magic bracelet has restrictions (conditional Polay problems)

I learned a combination of mathematics, almost all of which were self-taught. I don't know how to do a lot of things, and it is difficult to find appropriate materials. I think this is the difficulty of a weak school, but I will continue to work hard, in fact, I now think that stupid blog is quite good, the blog address is as follows: http://hi.baidu.com/%B1%BF%D0%A1%BA%A2_shw/blog/category/%D1%A7%CF%B0%D0%A1%BD%E1

This is to say that two kinds of beads cannot be put together. There are several words in the combined mathematics:

There are often two difficulties in the issue of combined counting. The first is to find the general expression of the problem, and the second is to differentiate which problems should be considered the same. Putting this sentence here is that the knowledge of the replacement group and the polyA theorem solve the same difference in the solution. But how can we solve the counting problem? If there is no rotation or reversal, how can this problem be solved?

This article describes ten typical questions solved by matrix multiplication, that is, the number of loops with N in an undirected graph. A necklace is not a loop !!

This question can be said to be compiled using templates. Most of the Code is templates, matrix multiplication, rapid matrix power, prime screening, Euler's function, polyA, of course, I have never used these templates together, and I have encountered some problems. Fortunately, I finally changed this question to AC.

The Code is as follows:

# Include <iostream> # include <string. h> # include <stdio. h> using namespace STD; int n, m, K; # define pp 100010int prime [PP]; bool isprime [pp + 1]; int size; void getprime () {memset (isprime, true, sizeof (isprime); int I; for (I = 2; I <= PP/2; I ++) {If (isprime [I]) // I is a prime number for (Int J = I + I; j <= pp; j + = I) isprime [J] = 0 ;} for (I = 2; I <= pp; I ++) {If (isprime [I]) prime [size ++] = I ;}} # define N 11 // the size of the annotation matrix struct matrix {long Lo Ng matrix [N] [N]; // long row and coloumn for each element; // note that the long row and coloumn must correspond to each other before and after this place. // matrix () {// memset (matrix, 0, sizeof (matrix); //} thism, PW [33]; bool B = 0; # define M 9973 // matrix mutiply (matrix, matrix B) // returns a * B {long I, j, k; matrix ans; memset (ans. matrix, 0, sizeof (ans. matrix); ans. row =. row; ans. coloumn = B. coloumn; for (I = 0; I <. row; I ++) for (k = 0; k <. coloumn; k ++) if (. matrix [I] [k]) // optimized for (j = 0; j <B. c Oloumn; j ++) {ans. matrix [I] [J] = (ans. matrix [I] [J] + (. matrix [I] [k] * B. matrix [k] [J]) % m;} return ans;} // (Binary) matrix fast power // adjust matrix multiplication matrix matrix_mi (matrix P, int K) // (Binary) matrix fast power {Matrix T; T. coloumn = T. row = m; // Init (t); // note that memset (T. matrix, 0, sizeof (T. matrix); For (INT I = 0; I <n; I ++) T. matrix [I] [I] = 1; // t initial value is 1 initialization; n is the array length; Initialization is required when declaring; otherwise, other values are uncertain. Int TT = 0; PW [TT] = P; while (k) {If (B) {If (K & 1) t = mutiply (t, PW [TT]);} else PW [tt + 1] = mutiply (PW [TT], PW [TT]); k >>= 1; TT ++ ;} B = 1; return t;} int Euler (int n) // evaluate the Euler function {// cout <n; int I, L, t; L = N; for (I = 0; I <size; I ++) {T = 0; while (L % prime [I] = 0) {T ++; L = L/prime [I];} If (T> 0) n = N/prime [I] * (Prime [I]-1 ); if (L = 1) break; If (L/prime [I] <prime [I]) {n = N/L * (L-1); break ;}} // cout <"Euler:" <n <"\ N"; return n % m;} int geter (int I) {int TT = 0; matrix Ts = matrix_mi (thism, I ); for (Int J = 0; j <m; j ++) tt + = ts. matrix [J] [J]; // cout <I <"" <TT <"<m <" \ n "; return TT % m;} long polyA (int c, int N) {geter (n); long an = 0; For (INT I = 1; I * I <= N; I ++) if (N % I = 0) {An = (An + (geter (I) * Euler (N/I )) % m) % m; if (I * I! = N) An = (An + (geter (N/I) * Euler (I) % m;} int I; for (I = 1 ;; I ++) {If (long INT) (n) * I-an) % m = 0) break;} return I % m;} int main () {getprime (); int CAS; // CIN> CAS; scanf ("% d", & CAS); While (CAS --) {B = 0; // CIN> N> m> K; scanf ("% d", & N, & M, & K); thism. coloumn = m; thism. row = m; For (INT I = 0; I <m; I ++) for (Int J = 0; j <m; j ++) thism. matrix [I] [J] = 1; int A, B; For (INT I = 0; I <K; I ++) {scanf ("% d ", & A, & B); // CIN> A> B; A --; B --; thism. matrix [a] [B] = 0; thism. matrix [B] [a] = 0;} printf ("% d \ n", (INT) polyA (m, n);} return 0 ;}

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.