Codeforces 487C Prefix Product Sequence (modulo inverse + construction)

Source: Internet
Author: User
Tags modulus cmath

Transfer from http://blog.csdn.net/houserabbit/article/details/41513745

It's really good to write.

Title Link: Http://codeforces.com/problemset/problem/487/C

The main idea: construct a 1~n arrangement so that n-prefix product pair n is a 0~n-1 arrangement

Title Analysis: Good problem, first of all we can get a simple analysis of n is definitely the last number, because if n in front, the prefix product must be more than one is a multiple of n, that is, n modulo at least two 0, obviously do not meet the arrangement, that is, modulo to get the last number is 0, then to consider the number of n-1 If it is 1 2 3 4...n-1 is not satisfied with the condition, it is clear that the first number to let it is 1, is always correct, we have constructed two numbers and then look at the middle, for a set of sequence A1 A2 A3 A4 ... an-1,a1=1, if A2 to n take-up modulo to prefix product and a1*a2* A3 to the value of n modulus and a1*a2 different, we might as well in the A3 A2 to the prefix product of the effect of elimination, and so on, then we need to use the concept of modulo inverse element.

For positive integers and, if so, the solution of the smallest positive integer in the congruence equation is called the inverse of the modulo. If it is a prime number, then the inverse can also be obtained according to the Fermat theorem. The derivation is as follows:

Back to the subject, we can construct ai= (i+1) *inv[i], (Inv[i] represents the modulo inverse of i)

This gets the prefix product A1a2a3...an-1=1*2*inv[1]*3*inv[2]*...*n*inv[n-1] because inv[2]*2≡1 (mod n) is equivalent to eliminating the effect of A2 on A3 further analysis, if n is composite, It must be able to represent X*y, where x, y is other factors except 1 and n, so for the prefix product (n-1)! must be divisible by n, there is a special case that can be constructed 1 3 2 4, because the reason is very simple, because 4 does not count as the last item of the prefix product is 2*3 obviously 6 cannot be divisible by 4, But the smallest composite of 4 is 6, 6 is not satisfied, because 5!=2*3*4*5 is obviously a multiple of 6, when N is expanding, more and more factors can be divisible by n, so we get other than 4 other than the composite can not construct such a sequence, the next is how to find the problem of modulo inverse element.

According to the above conclusion, the Fermat theorem can be used to solve the modulo inverse element by the fast power modulus, but there is a simpler recursive method.

inv[i]= (n-n/i) *inv[n%i]%n, the initial value inv[1]=1 this recursive derivation is as follows

a=n/i

b=n%i =

A*i+b≡0 (mod n) (divisible part + remainder part equals n) = =

-a*i≡b (mod n) (except b*i on both sides) =

-a*inv[b]≡inv[i] (mod n) (A/b is replaced) =

-(n/i) *inv[n%i]≡inv[i] (mod n) (change the left to a number greater than 0, add n directly, because then nmodn=0) =

(n-n/i) *inv[n%i]≡inv[i] (mod n) =

Inv[i] = (n-n/i) *inv[n%i]%n

Here are two ways to solve the equation:

1. Recursive type:

1#include <cstdio>2#include <cmath>3 #definell Long Long4 int ConstMAX = 1e5 +Ten;5 ll Inv[max];6 intN;7 8 BOOLIsPrime (intx)9 {Ten     if(x = =1) One         return false; A     if(x = =2) -         return true; -     if(x%2==0) the         return false; -      for(inti =3; I <= sqrt (n); i + =2)  -         if(n% i = =0)  -             return false; +     return true; - } +  A intMain () at { -scanf"%d", &n); -     if(n = =4)  -     {   -printf"yes\n1\n3\n2\n4\n");  -         return 0;  in     }   -     Else if(n = =1) to     { +printf"yes\n1\n"); -         return 0; the     } *     Else if(!IsPrime (n)) $     {Panax Notoginsengprintf"no\n"); -         return 0; the     } +printf"yes\n1\n"); Ainv[1] =1; the      for(inti =2; I < n; i++)  +     { -Inv[i] = (N-n/i) * inv[n% i]%N; $printf"%lld\n", (LL) I * inv[i-1] %n)); $     } -printf"%d\n", n); -}
code June

2. Fermat theorem + fast power modulus (speed is 20 times times of the 1th solution)

1#include <cstdio>2#include <cmath>3 #definell Long Long4 int ConstMAX = 1e5 +Ten;5 ll Inv[max];6 intN;7 8 BOOLIsPrime (intx)9 {Ten     if(x = =1) One         return false; A     if(x = =2) -         return true; -     if(x%2==0) the         return false; -      for(inti =3; I <= sqrt (n); i + =2)  -         if(n% i = =0)  -             return false; +     return true; - } +  A ll multi (ll A, ll b) at {   -ll ans =0;  -A%=N;  -      while(b) -     {   -         if(B &1)   in         {   -Ans = (ans + a)%N;  tob--;  +         }   -b >>=1;  theA = (a + a)%N;  *     }   $     returnans; Panax Notoginseng }  -  the ll Quick_mod (ll A, ll b) + {   All ans =1;  theA%=N;  +      while(b) -     {   $         if(B &1)   $         {   -Ans =multi (ans,a);  -b--;  the         }   -b >>=1; WuyiA =multi (a,a);  the     }   -     returnans;  Wu }   -  About intMain () $ { -scanf"%d", &n); -     if(n = =4)  -     {   Aprintf"yes\n1\n3\n2\n4\n");  +         return 0;  the     }   -     Else if(n = =1) $     { theprintf"yes\n1\n"); the         return 0; the     } the     Else if(!IsPrime (n)) -     { inprintf"no\n"); the         return 0; the     } Aboutprintf"yes\n1\n"); the      for(inti =2; I < n; i++)  theprintf"%lld\n", I * QUICK_MOD (i-1N2) %n); theprintf"%d\n", n); +}
View Code

Codeforces 487C Prefix Product Sequence (modulo inverse + construction)

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.