Hdu 4187 Alphabet Soup (Math _ Polya (KMP ))

Source: Internet
Author: User

Given n points on a ring, the ring is divided into 360000 degrees, and the point position is determined by angle. Then fill these points with m materials. A scheme is the same as the scheme it rotates, and the number of homogeneous schemes is asked. M <= 1000, n <= 360000

Problem-solving ideas: the foreign guy's questions are obscure. After reading these questions many times, he can understand the question of % 70. Then, he knocked it over and handed it over to wa. Then I read it many times and finally realized it.
The core of Polya is to find replacement. This question is different from the ordinary necklace Count, the difficulty is how to rotate, because the given angle is not necessarily equal to the entire ring, then there will be n replicas and ordinary necklace Count, in some cases, there is only one replacement, that is, constant replacement, for example, rotate, 20000, and 40000. The above situation is the two most special cases. Another case is 45000 90000 225000 270000. At this time, there are two replicas, one is constant replicas and the other is 180 degrees of rotation.
By writing several sets of special data, we can find that these points can be divided into several tables as cnt Tuo (1 <= cnt <= n ), if the rotation energy of these hosts overlaps with the original ones, they are homogeneous. The number of each table is len = n/cnt, which has a total of m ^ len filling solutions. At this time, the problem is converted to a ring with cnt points and filled with m ^ len colors. After rotation, the solution is the same as the original solution. Ask the number of different solutions, for this solution, see the solution report Here.
The final problem is how to find cnt Tuo. If you think about it, you will find that this is the same as the problem of finding a string with the minimum overwrite and number of cycles. You can find the next array to determine the number of cycles, cnt = n-next [n]. This conclusion is very elegant.
This question was accidentally ran for 3 s, and it became Rank1.

Test data:
Input:
2 4
0
120000
180000
270000
2 4
0
90000
180000
270000
100 5
0
45000
90000
180000
270000
2 4
45000
90000
225000
270000

OutPut:
16
6
99999307
10

C producer code:
[Cpp]
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
Using namespace std;
# Define INF 360000
# Deprecision MAX 400000
# Define MOD 100000007.
# Define int64 long
// # Define int64 _ int64
 
 
Int64 ans;
Int n, m, angle [MAX]; www.2cto.com
Int dist [MAX], next [MAX];
 
 
Void Get_Next (){
// Obtain the next Array
Int I, j, k;
I = 0; j =-1;
Next [0] =-1;
 
 
While (I <n ){
 
If (j =-1 | dist [I] = dist [j])
I ++, j ++, next [I] = j;
Else j = next [j];
}
}
Int Get_NextLen (){
// Len indicates the length of the cyclic section, and cnt indicates the number of cyclic sections.
Int cnt = n;
Int len = n-next [n];
If (n % len = 0)
Cnt = n/len;
Return cnt;
}
Int Gcd (int x, int y ){
// Returns the minimum public approx.
Int r = x % y;
While (r ){
 
X = y, y = r;
R = x % y;
}
Return y;
}
Int64 Eular (int64 n ){
// Euler's function, returns the number of interclasses less than n greater than 0 and n
Int64 ans = n, I;
For (I = 2; I * I <= n; I ++ ){
 
If (n % I = 0 ){
 
Ans-= ans/I;
While (n % I = 0)
N/= I;
If (n = 1) break;
}
}
 
 
If (n! = 1) ans-= ans/n;
Return ans % MOD;
}
Int64 Cal (int64 n, int64 k ){
// Binary fast power
Int64 x = 1;
While (k ){
 
If (k & 1) x = (x * n) % MOD;
N = (n * n) % MOD, k> = 1;
}
Return x;
}
Int64 inv (int64 x ){
// Reverse yuan for the simplified version
If (x = 1) return 1;
Return inv (MOD % x) * (MOD-MOD/x) % MOD;
}
Int64 Polya_2B (int64 m, int64 n ){
// The optimized method, enumerating the number of cyclic nodes I
Int I, j, k;
 
Ans = 0;
For (I = 1; I * I <n; ++ I) // enumerate the number of cyclic nodes
If (n % I = 0 ){
 
Ans = (ans + Eular (n/I) * Cal (m, I) % MOD;
Ans = (ans + Eular (I) * Cal (m, n/I) % MOD;
}
 
 
If (I * I = n)
Ans = (ans + Eular (n/I) * Cal (m, I) % MOD;
Return ans * inv (n) % MOD;
}
Void input (int & ){
 
Char c, f;
While (c = getchar () <'0' | f> '9 '));
For (a = 0; c> = '0' & c <= '9'; c = getchar () a = a * 10 + c-'0 ';
}
 
Int main ()
{
Int I, j, k, t, cnt;
 
 
While (scanf ("% d", & m, & n), n + m> = 0 ){
 
For (I = 1; I <= n; ++ I)
Input (angle [I]); // scanf ("% d", & angle [I]);
Sort (angle + 1, angle + 1 + n );
For (I = 1; I <n; ++ I)
Dist [I-1] = angle [I + 1]-angle [I];
Dist [n-1] = INF-angle [n] + angle [1];
 
 
Get_Next ();
Cnt = Get_NextLen ();
Ans = Polya_2B (Cal (m, n/cnt), cnt );
Printf ("% I64d \ n", ans );
}
}

Related Article

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.