[ACM] hdu 3923 Invoker (Poyla counting, fast power operation, Extended Euclidean or ferma's theorem)

Source: Internet
Author: User

[ACM] hdu 3923 Invoker (Poyla counting, fast power operation, Extended Euclidean or ferma's theorem)

 

Invoker


Problem Description On of Vance's favorite hero is Invoker, Kael. as your people knows Kael can control the elements and combine them to invoke a powerful skill. vance like Kael very much so he changes the map to make Kael more powerful.

In his new map, Kael can control n kind of elements and he can put m elements equal-spacedly on a magic ring and combine them to invoke a new skill. but if a arrangement can change into another by rotate the magic ring or reverse the ring along the axis, they will invoke the same skill. now give you n and m how does different skill can Kael invoke? As the number maybe too large, just output the answer mod 1000000007.
Input The first line contains a single positive integer T (T <= 500), indicates the number of test cases.
For each test case: give you two positive integers n and m. (1 <= n, m <= 10000)

Output For each test case: output the case number as shown and then output the answer mod 1000000007 in a line. Look sample for more information.
Sample Input
23 41 2

Sample Output
Case #1: 21Case #2: 1HintFor Case #1: we assume a,b,c are the 3 kinds of elements.Here are the 21 different arrangements to invoke the skills/ aaaa / aaab / aaac / aabb / aabc / aacc / abab / / abac / abbb / abbc / abcb / abcc / acac / acbc // accc / bbbb / bbbc / bbcc / bcbc / bccc / cccc / 

Source 2011 Multi-University Training Contest 9-Host by BJTU

 

 

 

Solution:

Polya count. The question can be converted to the c color to dye n beads on the necklace and asked how many colors there are.Modulo 1000000007 for the result

 

1. rotate.

After the ring is rotated clockwise, the number of cycle segments is gcd (n, I), and the dyeing scheme is Σ c ^ gcd (n, I) Where I =, 3, 4 ,.... n

2. Flip.

We also need to consider two cases here.

When n is an odd numberA total of nNumber of cyclic nodesIt is a round robin group (n/2 + 1). In other materials, the number of rings is (n/2 + 1). Note that this is the representation on the computer, integer n/2 is an integer obtained by dividing computers. In fact, it should be written as (n + 1)/2 .,The dyeing scheme is n * c ^ (n/2 + 1)

Why is the number of n cyclic nodes (n/2 + 1) in a cyclic group? I understand this, maybe not quite...

Take a positive triangle as an example to color three vertices. The axis of symmetry is the straight line of the link between a vertex and its peer endpoint. Such a straight line has 3 (n = 3, that is, n vertices) A total of 3 (n) Cyclic groups. Assuming that the first vertex is on the symmetric axis, the second vertex will surely overlap with the third vertex after being flipped through the symmetric axis, so (2, 3) is a cyclic Section, (1) itself is a cyclic section, the number of cyclic nodes is 2 (n + 1/2 ).

When n is an even numberThere are n cycle groups, where n/2 are the number of cycle nodes (n/2 + 1 ), the number of cycle nodes with n/2 is (n/2 ).

Take a square as an example. The four vertices are numbered 1, 2, 3, and 4 clockwise from the upper left corner.

When the line connecting 1 and 3 vertices is the symmetric axis (two vertices on the diagonal corner), the symmetric axis has two (n/2), after turning, 2, 4 coincidence, 1 and 1 coincidence, 3 and 3 overlap, so the number of cyclic nodes is 3 (2, 4) (1) (3), that is, (n/2 + 1 ).The dyeing scheme is (n/2) * c ^ (n/2 + 1)

When the straight line of the midpoint line of two relatively parallel edges is the symmetric axis, for example, the line of the midpoint line of line 1, 2 and 3 is the symmetric axis, such symmetric axes have two (n/2), after being flipped, 1, 2 coincidence, 3, 4 coincidence, the number of cyclic knots is 2, (1, 2) (3, 4 ), that is, (n/2 )., That is, who and who overlap, who and who are in a loop section.The dyeing scheme is (n/2) * c ^ (n/2)

 

Finally, the sum-up scheme is used to get ans, and then divided by the number of replacement groups 2 * n, that is, ans/(2 * n) % mod is the final answer. However, it should be noted that ans is the number obtained by constantly modulo in the calculation process. ans and 2 * n are all in the modulus remainder, and they cannot be directly involved in Division calculation, because there is a formula a * B % mod = (a % mod * B % mod) % mod, Division does not meet the Union law for the remainder. a/B! = (A % mod)/(B % mod) % mod, when calculating ans/(2 * n) % mod, it can be converted to ans * inv (2 * n) % mod. inv (2 * n) is the inverse element of 2 * n about mod, Which is multiplied by inv (2 * n) the remainder mod for the last answer is the same as dividing by 2 * n.

So the question is how to calculate the inverse element of the number of modulo P.

 

Method 1: Extend Euclidean. Ax = 1 (mod P), gcd (a, p) = 1, where x is the reverse element of a, which is what we want, ax = PY + 1, ax-Py = 1, therefore, we can use Extended Euclidean to obtain x.

Method 2: ferma's small theorem: If the modulo P is a prime number, inv (a) = pow (a, P-2) % p; the right side of the equation can be obtained through a fast power operation.

Reference: http://www.xuebuyuan.com/1394391.html

 

Code:

 

# Include
 
  
Using namespace std; typedef long LL; const LL mod = 1000000007; LL c, n; LL gcd (LL a, LL B) {return B = 0? A: gcd (B, a % B);} LL power (LL p, LL n) // Fast power Operation {LL ans = 1; while (n) {if (n & 1) ans = ans * p % mod; p = p * p % mod; n/= 2;} return ans;} LL exgcd (LL, LL B, LL & x, LL & y) // Extended Euclidean algorithm, returns the maximum common divisor of a and B, ax + by = gcd (a, B), x, y is a group of solutions of the equation {if (B = 0) {x = 1; y = 0; return a;} long d = exgcd (B, a % B, x, y); long t = x; x = y; y = t-a/B * y; return d;} int main () {int t; cin> t; int cas = 1; while (t --) {cin> c> n; int ans = 0; for (LL I = 1; I <= n; I ++) {ans + = power (c, gcd (n, I); ans % = mod;} if (n & 1) ans + = (n * power (c, n/2 + 1) % mod; else ans + = (n/2 * power (c, n/2 + 1) % mod + (n/2 * power (c, n/2) % mod; // note the mod location ans % = mod; LL x, y; exgcd (2 * n, mod, x, y); // x = power (2 * n, mod-2) % mod; // method 2 x = (x + mod) % mod; cout <"Case #" <
  
 

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.