Submission #22443

# Submission time Handle Problem Language Result Execution time Memory
22443 2017-04-30T04:41:42 Z 카시코이(#958, xdoju, ntopia, pps789) Polynomial Quine (KRIII5_PQ) C++11
2 / 7
239 ms 1640 KB
#include <cstdio>

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

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

bool DEBUG = false;

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] % N;
  }

  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]--;
  }

  if(DEBUG){
    puts("array: ");
    for(int i = 0; i < N; i++){
      for(int j = 0; j < N; j++) printf("%d ", p[i][j]);
      printf("\n");
    }
    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;

  if(DEBUG){
    puts("partial array:");
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++) printf("%d ", a[i][j]);
      printf("\n");
    }
    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;
      }
    }

    // printf("diag : %d\n", a[i][i]);
    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;
        }
      }
    }
  }

  if(DEBUG){
    puts("inverse array:");
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++) printf("%d ", b[i][j]);
      printf("\n");
    }

    printf("\n");

    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        a[i][j] = p[i][j + 1];
      }
    }

    puts("multiple:");
    for(int i = 0; i < n; i++){
      for(int j = 0; j < n; j++){
        int s = 0;

        for(int k = 0; k < n; k++){
          s += a[i][k] * b[k][j];
          s %= N;
        }

        printf("%d ", s);
      }
      printf("\n");
    }

    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:12:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int N; scanf("%d", &N);
                         ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1640 KB Output is correct
2 Correct 0 ms 1640 KB Output is correct
3 Correct 0 ms 1640 KB Output is correct
4 Correct 0 ms 1640 KB Output is correct
5 Correct 0 ms 1640 KB Output is correct
6 Correct 0 ms 1640 KB Output is correct
7 Correct 0 ms 1640 KB Output is correct
8 Correct 0 ms 1640 KB Output is correct
9 Correct 0 ms 1640 KB Output is correct
10 Correct 0 ms 1640 KB Output is correct
11 Correct 0 ms 1640 KB Output is correct
12 Correct 0 ms 1640 KB Output is correct
13 Correct 0 ms 1640 KB Output is correct
14 Correct 0 ms 1640 KB Output is correct
15 Correct 3 ms 1640 KB Output is correct
16 Correct 3 ms 1640 KB Output is correct
17 Correct 6 ms 1640 KB Output is correct
18 Correct 6 ms 1640 KB Output is correct
19 Correct 9 ms 1640 KB Output is correct
20 Correct 9 ms 1640 KB Output is correct
21 Correct 9 ms 1640 KB Output is correct
22 Correct 9 ms 1640 KB Output is correct
23 Correct 16 ms 1640 KB Output is correct
24 Correct 19 ms 1640 KB Output is correct
25 Correct 26 ms 1640 KB Output is correct
26 Correct 29 ms 1640 KB Output is correct
27 Correct 33 ms 1640 KB Output is correct
28 Correct 36 ms 1640 KB Output is correct
29 Correct 39 ms 1640 KB Output is correct
30 Correct 43 ms 1640 KB Output is correct
31 Correct 59 ms 1640 KB Output is correct
32 Correct 66 ms 1640 KB Output is correct
33 Correct 73 ms 1640 KB Output is correct
34 Correct 79 ms 1640 KB Output is correct
35 Correct 99 ms 1640 KB Output is correct
36 Correct 103 ms 1640 KB Output is correct
37 Correct 116 ms 1640 KB Output is correct
38 Correct 129 ms 1640 KB Output is correct
39 Correct 139 ms 1640 KB Output is correct
40 Correct 153 ms 1640 KB Output is correct
41 Correct 173 ms 1640 KB Output is correct
42 Correct 183 ms 1640 KB Output is correct
43 Correct 213 ms 1640 KB Output is correct
44 Correct 219 ms 1640 KB Output is correct
45 Correct 233 ms 1640 KB Output is correct
46 Correct 239 ms 1640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1640 KB Output is correct
2 Incorrect 0 ms 1640 KB Output isn't correct
3 Halted 0 ms 0 KB -