HDU birthday toy special restricted polyA

Source: Internet
Author: User

Q: Color n beads in K. Note that two adjacent beads cannot use the same color. This is not equivalent to mod 10000000007. In addition, there is another condition for this question. N small beads must be surrounded by a large bead, so you must select a color from K to color the large beads.

Because the data for this question is large 1 <= k, n <= 10 ^ 9, it is clear that the matrix cannot be used as the result of poj2888. However, to facilitate analysis, we should first start with the matrix. Because of the characteristics of the question itself: two adjacent beads cannot be the same color.

Then the element on the main diagonal line is all 0, and the other is all 1. What we need is the matrix to evaluate the value of the primary pair and the angle element after the power. · The analysis is very simple, but it is difficult to draw it out. You can directly borrow it from: http://blog.csdn.net/wukonwukon/article/details/7215467kids shoes:


Analysis; first of all, because the middle of a large circle and each small circle are connected, so the big circle with a color, only the K-1 color.
Set the K-1 color to dye n beads with different numbers of M, and finally calculate m x K mod 1000000007.
The method is the same as PKU 2888, but this time the matrix size is very large, so you cannot use the Matrix to store this image.
However, because the color of the adjacent beads is different, the element on the diagonal line of the adjacent array is 0, and the other elements are all 1.
The power trace of the matrix. you can deduce the formula (p-1) ^ N + (-1) ^ N * (p-1) where p is the order of the matrix, that is, K-1.
How is this formula obtained ??????
This formula is used in almost all logs, but I have not seen any explanation for this formula. Let me talk about my thoughts:
Assume that the n-1 power of A is:

X_n indicates the value on the diagonal line. Multiply by the number of zeros on the diagonal line, and the rest are after the 1 matrix.
Then 1) x_n = y_n-1 P-1 );
2) Y_n = x_n-1 + y_n-1 * (P-2 );
The above two formulas can be obtained from x_n = (P-2) * x_n-1 + (p-1) * x_n-2;
In fact, this step can solve the problem. We can use the multiplication of the Matrix to quickly obtain the x_n value and then find the trace of the matrix power.
Of course, at this step, the previous formula has not been deduced. How can we solve the above recursive formula?
Note: x_n + x_n-1 = (p-1) * (x_n-1 + x_n-2); (equation 1)
In this way, the x_n + x_n-1 can be solved;
The next step is to solve x_n.
Supplement: We can obtain from equation 1 (X_2 + x_1) = p-1 (this is obvious and should be X_1 = 0)
(X_3 + x_2) = (p-1) ^ 2
(X_4 + X_3) = (p-1) ^ 3
· (X_n + x_n-1) = (n-1) ^ (n-1) iterate over x_n = (p-1) ^ (n-1)-(p-1) ^ (n-2) + (p-1) ^ (n-3)-(p-1) ^ (n-4) ·-(p-1) ^ 2
+ (P-1) OK, sum of the proportional series. Therefore, (p-1) ^ N + (-1) ^ N * (p-1) is the sum of all primary and diagonal elements.
# Include <cmath> # include <cstring> # include <algorithm> # include <iostream> # include <cstdio> using namespace STD; # define lint _ int64const int maxn = 1000009; const int modulo = 1000000007; int A [maxn], p [maxn], PN; int Eul [maxn]; lint N, K, ans; void prime () {int I, j; Pn = 0; memset (A, 0, sizeof (a); for (I = 2; I <maxn; I ++) {If (! A [I]) P [pn ++] = I; for (j = 0; j <PN & I * P [J] <maxn & (P [J] <= A [I] | A [I] = 0 ); j ++) A [I * P [J] = P [J] ;}} void Euler () {Eul [1] = 1; for (INT I = 2; I <maxn; I ++) {if (a [I] = 0) Eul [I] = I-1; else {lint K = I/A [I]; if (K % A [I] = 0) Eul [I] = Eul [k] * A [I]; else Eul [I] = Eul [k] * (a [I]-1) ;}} int Euler (INT N) {If (n <maxn) return Eul [N] % modulo; int I, ret = N; for (I = 0; I <PN & P [I] * P [I] <= N; I ++) {If (N % P [I] = 0) {ret = ret-RET/P [I]; while (N % P [I] = 0) N/= P [I] ;}} if (n> 1) ret = ret-RET/N; return ret % modulo;} lint mod_exp (lint A, lint B) {lint ret = 1; A = A % modulo; while (B> = 1) {If (B & 1) ret = RET * A % modulo; A = A * A % modulo; B >>= 1 ;} return ret;} lint loop (lint K, lint Len) {lint ret = mod_exp (K-1, Len ); If (LEN & 1) ret = (Ret + Modulo-(k-1) % modulo; else ret = (Ret + k-1) % modulo; return ret ;} // ferma's small Theorem A ^ P-1 = 1 (mod p) lint inverse (lint N) {return mod_exp (n, modulo-2);} struct factor {int B, E ;} f [1000]; int fnum; void split (int n) {fnum = 0; For (INT I = 0; I <PN & P [I] * P [I] <= N; I ++) {If (N % P [I]) continue; F [fnum]. B = P [I]; F [fnum]. E = 0; while (N % P [I] = 0) {f [fnum]. E ++; N/= P [I];} fnum ++;} If (n> 1) f [fnum]. B = N, F [fnum ++]. E = 1 ;}// L is the level void DFS (int dep, int L) of the rotation decomposed by the loop {If (DEP = fnum) {ans = (ANS + Euler (l) * loop (K-1, N/L) % modulo; return;} For (INT val = 1, I = 0; I <= f [Dep]. e; I ++, Val * = f [Dep]. b) DFS (DEP + 1, L * val);} int main () {Prime (); Euler (); While (scanf ("% i64d % i64d ", & N, & K )! = EOF) {ans = 0; split (n); DFS (0, 1); ans = K * ans % modulo; ans = inverse (n) * ans % modulo; printf ("% i64d \ n", ANS);} return 0;}/* evaluate inverse lint ext_gcd (lint A, lint B, lint & X, lint & Y) by extending Euclidean) {lint ret, TMP; If (B = 0) {x = 1, y = 0; return a;} ret = ext_gcd (B, A % B, X, y); TMP = x, x = Y, y = TMP-A/B * Y; return ret;} lint inverse (lint N) {lint X, Y; ext_gcd (n, modulo, x, y); X = x % m Odulo; return x> = 0? X: x + modulo;} // calculate the number of non-equivalent colors lint polyA (lint N, lint K) {lint ret = 0; For (lint L = 1; L * l <= N; l ++) {If (N % L) continue; ret = (Ret + Euler (l) * loop (K-1, N/L )) % modulo; If (L * l = N) break; ret = (Ret + Euler (N/L) * loop (K-1, L) % modulo ;} return ret * inverse (n) % modulo ;}*/

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.