Fast Power of a replacement group

Source: Internet
Author: User
Fast Power of a replacement group

When I learned polyA, I used the number of cyclic knots that replaced the power of the group to find the power, and then I looked at the power.

Of course, that is, the article research and discussion on the Rapid power operation of the replacement group.

Well, it does write well. One question in this article is the date of the celebration, that is, poj 1282.

[Algorithm analysis] (from research and discussion on Rapid power operation of a replacement group)
Since the number on the turntable in each room is P and every priest is in different rooms every year, we can place the turntable in these rooms, to replace the length of P with N. Each year, the priest's position can also constitute a replacement with a length of N.
Apparently, the position of the priest of the year I is the position of the priest of the Year of the I-1, connected to the replacement represented by the number on the turntable of the year I. The problem now is to find the earliest year y so that the priest's position in that year is the unit replacement. Since the replacement on the turntable is in the p cycle, we enumerate y mod p = K. That is to say:
T1 then T2 then... t p then T1 then T2 then... then t p then... Then TK − 1 then TK = E
Because the Join Operation satisfies the combination law:
T1 then T2 then... using TK Merge (TK + 1 then TK + 2 then... when t p then T1 then T2 then... too tk) then... merge (TK + 1 then TK + 2 then... when t p then T1 then T2 then... required tk) = E
We can calculate thead = T1 then T2 before... required TK and tstep = TK + 1 required TK + 2 required... when t p then T1 then T2 then... merge TK, then the above formula is changed:
Thead increment (tstep) ^ (Y −k)/P = E (tstep) ^ (Y −k)/P = (thead) ^ −1

So when we enumerate y mod p = K, the problem becomes:

 

We know two replicas tdeah = (thead) ^-1, tstep, and calculate a minimum number x = (Y-k)/P to make tstep ^ x = tdeah.

 

If tstep and tdeah are both a loop, you can immediately obtain the minimum value of X according to the above algorithm. You can also know that all feasible answers are in the form of X + kN. If there are multiple loops, we can clearly list a series of modulus linear equations, such as X mod ni = xi. After solving the equations, you can get the answer.

# Include <iostream> # include <stdio. h> # include <string. h> using namespace STD; # define maxn 210int A [maxn] [maxn], B [maxn] [maxn], C [maxn] [maxn]; // C indicates the inverse of thead, that is, tdeahint D1 [maxn], D2 [maxn]; int N, P; bool find (INT W1 [], int W2, int K, int & Y, Int & X) {int T = K; y =-1; if (t = W2) y = 0; t = W1 [k]; X = 1; while (T! = K) {If (t = W2) y = x; t = W1 [T]; X ++;} If (y =-1) return false; return true;} int extend_gcd (int A, int B, Int & X, Int & Y) {If (B = 0) {x = 1; y = 0; return A;} int T = extend_gcd (B, A % B, Y, x); y-= A/B * X; return t ;} int solve (int r [], int A [], int N) // Extended Euclidean merge {int RR = R [0], AA = A [0], X, y; For (INT I = 1; I <n; I ++) {int TMP = R [I]-RR; int GCD = extend_gcd (AA, a [I], x, Y); If (TMP % GCD! = 0) Return-1; int temp = A [I]/GCD; X = (TMP/GCD * X) % TEMP + temp) % TEMP; RR = RR + AA * X; AA = AA * A [I]/GCD;} return RR;} int main () {int I, J; scanf ("% d", & N, & P); for (I = 0; I <n; I ++) {for (j = 0; j <p; j ++) {scanf ("% d", & A [J] [I]); A [J] [I] --;} for (I = 0; I <n; I ++) {B [0] [I] = A [0] [I] ;}for (I = 1; I <p; I ++) // obtain tstep T1 * t2 * · * TP; {for (j = 0; j <n; j ++) B [I] [J] = A [I] [B [I-1] [J];} for (I = 0; I <p; I ++) {for (j = 0; j <n; j ++) C [I] [B [I] [J] = J ;} Int ans = 100000000; for (I = 0; I <p; I ++) // enumerate y mod p = K; {bool flag = true; For (j = 0; j <n; j ++) {flag = find (B [P-1], C [I] [J], J, D1 [J], D2 [J]); if (! Flag) break;} If (! Flag) continue; int TT = solve (D1, D2, n); If (TT! =-1 & TT * P + I + 1 <ans) ans = TT * P + I + 1;} If (ANS! (= 100000000) printf ("% d \ n", ANS); else printf ("No one knows. \ n ");}

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.