COJ 1163 寒衣调(校赛D题)

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1163

a[i - 1]表示i的前缀积,b[i + 1]表示i的后缀积,两者相乘取模就是所需的结果

#include<cstdio>

#define MAXN 100005

int a[MAXN], b[MAXN];

int n, m;

int main()

{

    while(scanf( "%d%d", &n, &m) == 2){

        for(int i = 0; i < n; i ++)

            scanf("%d",&a[i]);

        b[n] = 1;

        for(int i = n - 1; i >= 0; i --)

            b[i] = (long long)b[i + 1] * a[i] % m;

        for(int i = 1; i < n; i ++)

            a[i] = (long long)a[i - 1] * a[i] % m;

        printf( "%lld", (long long)b[1]);

        for(int i = 1; i < n; i ++)

            printf(" %lld", (long long)a[i - 1] * b[ i + 1] % m);

        printf("\n");

    }

    return 0;

}

 

 

你可能感兴趣的:(OJ)