HDU 2481 Toy (08 Chengdu site Polya, recursion, matrix, number theory ......)

Source: Internet
Author: User

Question: There are N nodes in the circle, and a node in the center is connected to N nodes. There are 2 * N edges in total and N edges are deleted, to connect N + 1 points, the same rotation is considered equivalent. How many situations are there.
Http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 2481
It is said that there was only one Tsinghua team in the competition. Very comprehensive, mainly because the recursive part is hard to think about.
Good questions, difficult !!!!!!!!!!!
Practice from AC blog: http://hi.baidu.com/aekdycoin/item/517784ec0bf4450b560f1dd1
The following is a reference:
PS. This question uses:
Prime Number filtering, solving Euler's function, BurnSide theorem, binary simulation multiplication, recursive construction, matrix binary power, replacement group, enumeration... in a word, it is a good question. Basically, I have investigated the basic knowledge of number theory once.
You can imagine ....
Let's take a look at the number of possibilities before considering rotation. I sorted out the recurrence in the AC blog and reviewed it again.
Here we can take any two nodes to discuss a and B. The sum is the sum of the disconnections between a and B and that between a and B.
F (n) indicates n knots in the outer ring, and a and B indicate the number of disconnections.
G (n) indicates that there are n knots in the outer ring, and a and B are connected together.
If a and B are disconnected, if k (plus a itself) is directly connected to a, it is clear that these k must be connected to other, there must be an edge with the center. If there are multiple sides, the ring will be formed. Obviously, the spanning tree is not satisfied. In addition, n-k is f [n-k]. If we can enumerate k, f [n] = sigma (I * f [n-I]) (n-1> = I> = 0)

If a and B are connected together, if k (including a and B) are connected to a and B, then, B is adjacent IN THE k Position Selection there is a K-1, and the k and the center connected to the choice of k kinds, the rest and this part is separated, then f [n-k], so k can be enumerated, the final result g [n] = sigma (I * (I-1) * f [n-I])
(N-1> = I> = 2)

Then the final number is T [n] = f [n] + g [n].

F [n] = sigma (I * f (n-I) (n-1 => I> = 0)
F [n] = f (n-1) + 2 * f (n-2) + 3 * f (n-3 )...... (N-1) * f (1) + n * f [0]
= F (n-1) + f (n-2) + f (n-3 )...... F (1) + f (0) + (f (n-2) + 2 * f (n-3 )...... + (N-1) * f (0 ))
If s [n] is the first n sum of f [I], the preceding formula can be written
F [n] = s [n-1] + f (n-2) + 2 * f (n-3 )...... (N-2) f (1)
= S [n-1] + sigma (I * f (n-1)-I) (n-2 => I> = 0)
= S [n-1] + f [n-1] (1) s [n-1] = f [n]-f [n-1]
= S [N-2] + f [n-1] + f [n-1]
= S [N-2] + 2 * f [n-1]
= F [n-1]-f [N-2] + 2 * f [n-1] deformation of s [N-2] according to (1) Type
= 3 * f [n-1]-f [N-2] Where f [0] = 1, f [1] = 1, f [2] = 3, f [3] = 8

G (n) = sigma [I (I-1) f (n-I)] (1 <= I <n)
= 1*2 * f [N-2] + 2*3 * f [n-3] + 3*4 * f [n-4]... (N-1) * (n-2) * f [1]
Then g (n-1) = 1*2 * f [n-3] + 2*3 * f [n-4]... + (N-2) * (n-3) * f [1]
Then g (n)-g (n-1) = 2 * f [N-2] + 4 * f [n-3]... (2 * (n-2) * f [1]
= 2 * (f [N-2] + 2 * f [n-3]... + F [1])
= 2 * f [n-1]

This is the most basic recursive formula ..
G [n] = 2 * (f [1] + f [2] + f [3]… F [n-1]) = 2 * (s [n-1]-f [0])
= 2 * (f [n]-f [n-1]-1) Where f [0] = 1
AC introduced f [0] to solve a small problem of g (). However, he wrote some questions in his blog, if s [n] includes f [0], then g [n] is not equal to 2 * s [n-1]. The Great God has completed the important derivation, which should be a mistake.

For the method of f [n], it can be solved by using a matrix Rapid power multiplication.
{F [n], f [n-1] }={ f [1], f [0]} * | 3 1 | ^ (n-1)
|-1 0 |
And g [n] can be obtained by the way, T [n] will be processed.

Then there is the Burnside theorem. N is also relatively large. It must be optimized using the Euler's function to enumerate the number of loops.
.
At the beginning, I thought that MOD was at 10 ^ 9. As long as I use a 64-bit integer, there should be no problem in the middle part. I used Extended Euclidean to find the inverse element, but I couldn't even get through the example, it was found that n pairs of MOD are very likely not against the yuan, completely speechless.
Only (a/B) % c = (a % (B * c)/B can be used. In this way, the modulo is changed to MOD * N, and the range is 10 ^ 18. In this way, the multiplication in the middle will overflow the 64-bit integer.
All the big integers are multiplied by two digits...
In addition, the 64-bit integer input and output pose is a headache .....
[Cpp]
# Include <iostream>
# Include <cstring>
# Include <queue>
# Include <cstdio>
# Include <cmath>
# Include <algorithm>
# Include <vector>
# Include <map>
# Define N 1000000000
# Define inf 1 <29
# Define LL long
# Define eps 1e-7
# Define pb (a) push_back ()
# Define ub (v, a) upper_bound (v. begin (), v. end (),)
Using namespace std;
Struct Matrix {
LL m [2] [2];
} Init;
Ll mod;
Int n;
Bool flag [40000] = {0 };
Int prime [40000], cnt = 0;
// Because the range of a and B is 10 ^ 18, binary simulation calculates a * B
LL MultMod (LL a, LL B ){
A % = MOD;
B % = MOD;
If (B <0) B + = MOD;
If (a <0) a + = MOD;
LL ret = 0;
While (B ){
If (B & 1 ){
Ret + =;
If (ret> = MOD) ret-= MOD;
}
A = a <1;
If (a> = MOD) a-= MOD;
B = B> 1;
}
Return ret;
}
Matrix operator * (Matrix m1, Matrix m2 ){
Matrix ans;
For (int I = 0; I <2; I ++)
For (int j = 0; j <2; j ++ ){
Ans. m [I] [j] = 0;
For (int k = 0; k <2; k ++)
Ans. m [I] [j] = (ans. m [I] [j] + MultMod (m1.m [I] [k], m2.m [k] [j]) % MOD;
}
Return ans;
}
Matrix operator ^ (Matrix m1, int B ){
Matrix ans;
For (int I = 0; I <2; I ++)
For (int j = 0; j <2; j ++)
Ans. m [I] [j] = (I = j );
While (B ){
If (B & 1)
Ans = ans * m1;
M1 = m1 * m1;
B> = 1;
}
Return ans;
}
// The above is the matrix's fast power multiplication.
Void Prime (){
For (int I = 2; I <= sqrt (N + 1.0); I ++ ){
If (flag [I]) continue;
Prime [cnt ++] = I;
For (int j = 2; j * I <= sqrt (N + 1.0); j ++)
Flag [I * j] = true;
}
}
Int Eular (int n ){
Int ret = 1;
For (int I = 0; I <cnt & prime [I] * prime [I] <= n; I ++ ){
If (n % prime [I] = 0 ){
N/= prime [I]; ret * = prime [I]-1;
While (n % prime [I] = 0) {n/= prime [I]; ret = (ret * prime [I]) % MOD ;}
}
}
If (n> 1) ret * = n-1;
Return ret % MOD;
}
// The above is the prime number table. Evaluate the Euler's function.
LL Get_T (int k ){
If (k = 1) return 1;
Else if (k = 2) return 5;
Matrix temp = init ^ (K-2 );
LL f = 3 * temp. m [0] [0] + temp. m [1] [0];
LL g = 2*(f-(3 * temp. m [0] [1] + temp. m [1] [1])-1 );
Return (g + f) % MOD;
}
// Calculate the tvalue
LL Polya (){
LL sum = 0;
Int I;
// Burnside theorem, enumerative Number of loops
For (I = 1; I * I <n; I ++)
If (n % I = 0 ){
Sum = (sum + MultMod (Eular (I), Get_T (n/I) % MOD;
Sum = (sum + MultMod (Eular (n/I), Get_T (I) % MOD;
}
If (I * I = n) sum = (sum + MultMod (Get_T (I), Eular (I) % MOD;
Return sum/n;
}
Int main (){
Prime (); www.2cto.com
// Construct a Matrix
Init. m [0] [0] = 3; init. m [0] [1] = 1; init. m [1] [0] =-1; init. m [1] [1] = 0;
While (scanf ("% d % I64d", & n, & MOD )! = EOF ){
MOD = (LL) n * MOD;
Printf ("% I64d \ n", Polya () % (MOD/n ));
}
Return 0;
}
By ACM_cxlove

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.