CodeForces 487C Prefix Product Sequence,codeforces487c

來源:互聯網
上載者:User

CodeForces 487C Prefix Product Sequence,codeforces487c

題意:

構造一個1~n的排列  使得n個首碼積%n是一個0~n-1的排列

思路:

首先確定n一定放最後  要不然會有%n會有多個0  這時n-1位置的首碼積為(n-1)!

接著討論n  n為合數時只有n=4有解  因為如果n為合數一定可以拆成p*q的形式  明顯pq|(n-1)!

然後構造ai=(i+1)*inv[i]  因為(i+1)*inv[i] == (j+1)*inv[j]時一定有i==j  所以這樣構造滿足ai是唯一的  也就是說是一個排列

而且這樣構造使得首碼積 a1*a2*a3... = 1 * 2 * inv[1] * 3 * inv[2]...  那麼%n的結果也是一個排列

最後輸出答案即可  1~n的逆元可以打表求出  遞推公式為 (mod-mod/i)*inv[mod%i]%mod

PS:隊友說構造的想法來自於 “這種題%n後的排列一定很特殊  所以嘗試 1 2 3 4 5 ....  即可”

代碼:

#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<map>#include<set>#include<vector>#include<queue>#include<cstdlib>#include<ctime>#include<cmath>using namespace std;typedef long long LL;#define N 100010int inv[N];int n;int main() {    scanf("%d", &n);    if (n == 1) {        puts("YES");        printf("1\n");        return 0;    } else if (n == 4) {        puts("YES");        printf("1\n3\n2\n4\n");        return 0;    }    for (int i = 2; i * i <= n; i++) {        if (n % i == 0) {            puts("NO");            return 0;        }    }    puts("YES");    puts("1");    inv[1] = 1;    for (int i = 2; i < n; i++) {        inv[i] = (LL) (n - n / i) * inv[n % i] % n;        printf("%d\n", (int) ((LL) (i) * inv[i - 1] % n));    }    printf("%d\n", n);    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.