Submission #22509

# Submission time Handle Problem Language Result Execution time Memory
22509 2017-04-30T05:10:58 Z 카시코이(#958, xdoju, ntopia, pps789) Polynomial Quine (KRIII5_PQ) C++11
2 / 7
376 ms 1812 KB
#include <cstdio>
#include <algorithm>
using namespace std;

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

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

bool DEBUG = false;

int ans[210][210];

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

  for(int T = 0; ; T++){

    // 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 = 2; i < N; i++){
      for(int v = 1; v < N; v++) if(i * v % N == 1){ inv[i] = v; break; }
    }

    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
    // a[1]�� �����Ѵٰ� ����
    for(int i = 0; i < N - 1; i++){
      for(int j = 0; j < N; j++){
        if(j == T) continue;
        a[i][j < T ? j : j - 1] = p[i][j];
      }
      // 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++) for(int j = 0; j < n; j++) b[i][j] = 0;
    for(int i = 0; i < n; i++) b[i][i] = 1;

    for(int i = 0; i < n; i++){
      if(inv[a[i][i]] == 0){
        int ni = -1;
        for(int j = i + 1; j < n; j++){
          if(inv[a[j][i]] != 0){ ni = j; break; }
        }
        for(int j = 0; j < n; j++){
          swap(a[i][j], a[ni][j]);
          swap(b[i][j], b[ni][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][T] * 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;
        }
      }

      for(int i = 0; i < N; i++){
        ans[v][i] = (i == T ? v : i < T ? y[i] : y[i - 1]);
      }
    }

    bool valid = true;

    for(int t = 0; t < N; t++){
      bool val = true;
      for(int v = 0; v < N; v++){
        int s = 0, w = 1;
        for(int i = 0; i < N; i++){
          s += ans[t][N - 1 - i] * w; s %= N;
          w *= v; w %= N;
        }

        if(s != ans[t][N - 1 - v]){ val = false; break; }
      }

      if(!val){ valid = false; break; }
    }

    if(!valid) continue;

    for(int i = 0; i < N; i++){
      for(int j = i + 1; j < N; j++){
        bool big = false;
        for(int k = 0; k < N; k++){
          if(ans[i][k] < ans[j][k]) break;
          if(ans[i][k] > ans[j][k]){ big = true; break; }
        }

        if(big){
          for(int k = 0; k < N; k++) swap(ans[i][k], ans[j][k]);
        }
      }
    }

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

    break;
  }
  return 0;
}

Compilation message

PQ.cpp: In function 'int main()':
PQ.cpp:16: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 1812 KB Output is correct
2 Correct 0 ms 1812 KB Output is correct
3 Correct 0 ms 1812 KB Output is correct
4 Correct 0 ms 1812 KB Output is correct
5 Correct 0 ms 1812 KB Output is correct
6 Correct 0 ms 1812 KB Output is correct
7 Correct 0 ms 1812 KB Output is correct
8 Correct 0 ms 1812 KB Output is correct
9 Correct 0 ms 1812 KB Output is correct
10 Correct 0 ms 1812 KB Output is correct
11 Correct 0 ms 1812 KB Output is correct
12 Correct 0 ms 1812 KB Output is correct
13 Correct 3 ms 1812 KB Output is correct
14 Correct 3 ms 1812 KB Output is correct
15 Correct 3 ms 1812 KB Output is correct
16 Correct 6 ms 1812 KB Output is correct
17 Correct 9 ms 1812 KB Output is correct
18 Correct 6 ms 1812 KB Output is correct
19 Correct 13 ms 1812 KB Output is correct
20 Correct 13 ms 1812 KB Output is correct
21 Correct 16 ms 1812 KB Output is correct
22 Correct 19 ms 1812 KB Output is correct
23 Correct 19 ms 1812 KB Output is correct
24 Correct 26 ms 1812 KB Output is correct
25 Correct 36 ms 1812 KB Output is correct
26 Correct 43 ms 1812 KB Output is correct
27 Correct 46 ms 1812 KB Output is correct
28 Correct 49 ms 1812 KB Output is correct
29 Correct 53 ms 1812 KB Output is correct
30 Correct 73 ms 1812 KB Output is correct
31 Correct 99 ms 1812 KB Output is correct
32 Correct 103 ms 1812 KB Output is correct
33 Correct 153 ms 1812 KB Output is correct
34 Correct 113 ms 1812 KB Output is correct
35 Correct 153 ms 1812 KB Output is correct
36 Correct 153 ms 1812 KB Output is correct
37 Correct 183 ms 1812 KB Output is correct
38 Correct 203 ms 1812 KB Output is correct
39 Correct 189 ms 1812 KB Output is correct
40 Correct 249 ms 1812 KB Output is correct
41 Correct 239 ms 1812 KB Output is correct
42 Correct 253 ms 1812 KB Output is correct
43 Correct 319 ms 1812 KB Output is correct
44 Correct 303 ms 1812 KB Output is correct
45 Correct 359 ms 1812 KB Output is correct
46 Correct 376 ms 1812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 1812 KB Output is correct
2 Correct 0 ms 1812 KB Output is correct
3 Correct 0 ms 1812 KB Output is correct
4 Correct 0 ms 1812 KB Output is correct
5 Correct 0 ms 1812 KB Output is correct
6 Correct 0 ms 1812 KB Output is correct
7 Correct 0 ms 1812 KB Output is correct
8 Correct 0 ms 1812 KB Output is correct
9 Correct 0 ms 1812 KB Output is correct
10 Correct 0 ms 1812 KB Output is correct
11 Correct 0 ms 1812 KB Output is correct
12 Correct 0 ms 1812 KB Output is correct
13 Correct 0 ms 1812 KB Output is correct
14 Correct 0 ms 1812 KB Output is correct
15 Correct 0 ms 1812 KB Output is correct
16 Correct 0 ms 1812 KB Output is correct
17 Correct 0 ms 1812 KB Output is correct
18 Correct 0 ms 1812 KB Output is correct
19 Correct 0 ms 1812 KB Output is correct
20 Correct 13 ms 1812 KB Output is correct
21 Correct 0 ms 1812 KB Output is correct
22 Correct 3 ms 1812 KB Output is correct
23 Correct 3 ms 1812 KB Output is correct
24 Correct 3 ms 1812 KB Output is correct
25 Correct 3 ms 1812 KB Output is correct
26 Correct 6 ms 1812 KB Output is correct
27 Correct 3 ms 1812 KB Output is correct
28 Correct 16 ms 1812 KB Output is correct
29 Incorrect 63 ms 1812 KB Output isn't correct
30 Halted 0 ms 0 KB -