답안 #22414

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
22414 2017-04-30T04:29:27 Z 카시코이(#958, xdoju, ntopia, pps789) Polynomial Quine (KRIII5_PQ) C++11
0 / 7
0 ms 1636 KB
#include <cstdio>

int p[210][210];
int inv[210];

int a[210][210], b[210][210];
int x[210], y[210];

int main(){
  int N; scanf("%d", &N);

  // for 2 point: N = prime
  inv[1] = 1;
  for(int i = 2; i < N; i++){
    inv[i] = N - N / i * inv[N % i];
  }

  for(int i = 0; i < N; i++){
    int w = 1, v = N - 1 - i;
    for(int j = N - 1; j >= 0; j--){
      p[i][j] = w; w *= v; w %= N;
    }
  }

  for(int i = 0; i < N; i++){
    if(p[i][i] == 0) p[i][i] = N - 1;
    else p[i][i]--;
  }

  /*
     for(int i = 0; i < N; i++){
     for(int j = 0; j < N; j++) printf("%d ", p[i][j]);
     printf("\n");
     }
   */

  // get inverse array
  for(int i = 0; i < N - 1; i++){
    for(int j = 0; j < N - 1; j++){
      a[i][j] = p[i][j + 1];
    }
  }

  int n = N - 1;

  /*
     for(int i = 0; i < n; i++){
     for(int j = 0; j < n; j++) printf("%d ", a[i][j]);
     printf("\n");
     }
   */

  for(int i = 0; i < n; i++) b[i][i] = 1;

  for(int i = 0; i < n; i++){
    if(a[i][i] == 0){
      int ni = -1;
      for(int j = i + 1; j < n; j++){
        if(a[j][i] != 0){ ni = j; break; }
      }
      for(int j = 0; j < n; j++){
        a[i][j] += a[ni][j]; a[i][j] %= N;
        b[i][j] += b[ni][j]; b[i][j] %= N;
      }
    }

    int ef = inv[a[i][i]];
    for(int j = 0; j < n; j++){ a[i][j] *= ef; a[i][j] %= N; }
    for(int j = 0; j < n; j++){ b[i][j] *= ef; b[i][j] %= N; }
    for(int j = 0; j < n; j++){
      if(j != i){
        int m = a[j][i];
        for(int k = 0; k < n; k++){
          a[j][k] -= a[i][k] * m % N;
          if(a[j][k] < 0) a[j][k] += N;
        }
        for(int k = 0; k < n; k++){
          b[j][k] -= b[i][k] * m % N;
          if(b[j][k] < 0) b[j][k] += N;
        }
      }
    }
  }

  /*
     for(int i = 0; i < n; i++){
     for(int j = 0; j < n; j++) printf("%d ", b[i][j]);
     printf("\n");
     }
   */

  // assume ans[0]
  for(int v = 0; v < N; v++){
    for(int i = 0; i < n; i++){
      x[i] = -p[i][0] * v;
      x[i] %= N; if(x[i] < 0) x[i] += N;
    }

    for(int i = 0; i < n; i++) y[i] = 0;
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        y[i] += b[i][j] * x[j] % N;
        if(y[i] >= N) y[i] -= N;
      }
    }

    printf("%d", v);
    for(int i = 0; i < n; i++) printf(" %d", y[i]);
    printf("\n");
  }
  return 0;
}

Compilation message

PQ.cpp: In function 'int main()':
PQ.cpp:10:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int N; scanf("%d", &N);
                         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1636 KB Output is correct
2 Correct 0 ms 1636 KB Output is correct
3 Correct 0 ms 1636 KB Output is correct
4 Correct 0 ms 1636 KB Output is correct
5 Incorrect 0 ms 1636 KB Output isn't correct
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 1636 KB Output is correct
2 Incorrect 0 ms 1636 KB Output isn't correct
3 Halted 0 ms 0 KB -