codeforces 487C Prefix Product Sequence (模逆元+構造),codeforces487c

來源:互聯網
上載者:User

codeforces 487C Prefix Product Sequence (模逆元+構造),codeforces487c


C. Prefix Product Sequencetime limit per test1 secondmemory limit per test256 megabytes

Consider a sequence [a1, a2, ... , an]. Define its prefix product sequence .

Now given n, find a permutation of[1, 2, ..., n], such that its prefix product sequence is a permutation of[0, 1, ..., n - 1].

Input

The only input line contains an integer n (1 ≤ n ≤ 105).

Output

In the first output line, print "YES" if such sequence exists, or print "NO" if no such sequence exists.

If any solution exists, you should output n more lines. i-th line contains only an integerai. The elements of the sequence should be different positive integers no larger thann.

If there are multiple solutions, you are allowed to print any of them.

Sample test(s)Input
7
Output
YES1436527
Input
6
Output
NO
Note

For the second sample, there are no valid sequences.


題目連結:http://codeforces.com/problemset/problem/487/C


題目大意:構造一個1~n的排列  使得n個首碼積對n取餘是一個0~n-1的排列


題目分析:好題,首先我們通過簡單的分析可以得到n肯定是最後一個數,因為如果n在前面,首碼積肯定不止1個是n的倍數,也就是說對n模數至少有兩個0,顯然不滿足排列,也就是說模數得到排列最後一個數是0,再來考慮前n-1個數,如果就是1 2 3 4...n-1是不是滿足條件呢,顯然第一個數就讓它是1,是始終正確的,我們已經構造出來兩個數了再來看中間的,對於一組序列a1 a2 a3 a4 ... an-1,a1=1,如果a2對n取完模要首碼積及a1*a2*a3對n模數的值與a1*a2的不同,我們不妨在a3中把a2對首碼積的影響消除,後面以此類推,這時就要用到模逆元的概念。

對於正整數和,如果有,那麼把這個同餘方程中的最小正整數解叫做模的逆元。如果為素數,那麼還可以根據費馬小定理得到逆元為。推導如下:

回到本題,我們可以構造ai=(i+1)*inv[i],(inv[i]表示i的模逆元)

這樣得到的首碼積a1a2a3...an-1=1*2*inv[1]*3*inv[2]*...*n*inv[n-1]因為inv[2]*2≡1(mod n)相當於前面說的消除了a2對a3的影響進一步分析,如果n是合數,則其必能表示成x*y,這裡x,y是除了1和n的其他因子,因此對於首碼積(n-1)!必然能被n整除,這裡有個特例4,4可以構造出1 3 2 4,原因很簡單,因為4不算最後一項的首碼積為2*3顯然6不能被4整除,但是比4大的最小的合數為6,6就不滿足了,因為5!=2*3*4*5顯然是6的倍數,當n不斷擴大的時候,因子越來越多則更加能夠被n整除,所以我們得到除了4以外的其他合數都無法構造出這樣的序列,接下來就是怎樣求模逆元的問題。

根據上面的結論了,可以用費馬小定理通過快速冪模數求解模逆元,不過還有更簡便的遞推式

inv[i]= (n-n/i)*inv[n%i]%n,初始值inv[1]=1這個遞推式的推導如下

a=n/i

b=n%i  =>

a*i+b≡0(mod n) (整除部分+餘數部分就等於n)  =>

-a*i≡b(mod n) (兩邊同除b*i)   =>

-a*inv[b]≡inv[i](mod n) (將a,b替換)  =>

-(n/i)*inv[n%i]≡inv[i](mod n) (將左邊變為大於0的數,直接加上n即可,因為這時nmodn=0)  =>

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

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

下面給出兩種解法:

1.遞推式:

#include <cstdio>#include <cmath>#define ll long longint const MAX = 1e5 + 10;ll inv[MAX];int n;bool isprime(int x){    if(x == 1)        return false;    if(x == 2)        return true;    if(x % 2 == 0)        return false;    for(int i = 3; i <= sqrt(n); i += 2)         if(n % i == 0)             return false;    return true;}int main() {    scanf("%d", &n);    if(n == 4)     {          printf("YES\n1\n3\n2\n4\n");          return 0;      }      else if(n == 1)    {        printf("YES\n1\n");        return 0;    }    else if(!isprime(n))    {        printf("NO\n");        return 0;    }    printf("YES\n1\n");    inv[1] = 1;    for(int i = 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);}


2.費馬小定理+快速冪模數 (速度是第1種解法的20倍)

#include <cstdio>#include <cmath>#define ll long longint const MAX = 1e5 + 10;ll inv[MAX];int n;bool isprime(int x){    if(x == 1)        return false;    if(x == 2)        return true;    if(x % 2 == 0)        return false;    for(int i = 3; i <= sqrt(n); i += 2)         if(n % i == 0)             return false;    return true;}ll multi(ll a, ll b)  {      ll ans = 0;      a %= n;      while(b)      {          if(b & 1)          {              ans = (ans + a) % n;              b--;          }          b >>= 1;          a = (a + a) % n;      }      return ans;  } ll quick_mod(ll a, ll b)  {      ll ans = 1;      a %= n;      while(b)      {          if(b & 1)          {              ans = multi(ans,a);              b--;          }          b >>= 1;          a = multi(a,a);      }      return ans;  }  int main() {    scanf("%d", &n);    if(n == 4)     {          printf("YES\n1\n3\n2\n4\n");          return 0;      }      else if(n == 1)    {        printf("YES\n1\n");        return 0;    }    else if(!isprime(n))    {        printf("NO\n");        return 0;    }    printf("YES\n1\n");    for(int i = 2; i < n; i++)         printf("%lld\n", i * quick_mod(i - 1, n - 2) % n);    printf("%d\n", n);}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.