#include <iostream>
#include <cmath>
using namespace std;

const int N = 4;

int Q[N][N] = {
    {3, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0},
    {0, 0, 0, 0}
};

int QMatrix[N][N] = {
    {16, 11, 10, 16},
    {12, 12, 14, 19},
    {14, 13, 16, 24},
    {14, 17, 22, 29}
};

double alpha(int x) {
    return x == 0 ? 1.0 / sqrt(2.0) : 1.0;
}

int main() {
    double DCT[N][N], result[N][N] = {0};

    for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
            DCT[i][j] = Q[i][j] * QMatrix[i][j];

    for (int x = 0; x < N; x++) {
        for (int y = 0; y < N; y++) {
            for (int u = 0; u < N; u++) {
                for (int v = 0; v < N; v++) {
                    result[x][y] += alpha(u) * alpha(v) * DCT[u][v] *
                        cos((2 * x + 1) * u * M_PI / 8.0) *
                        cos((2 * y + 1) * v * M_PI / 8.0);
                }
            }
            result[x][y] *= 0.25;
        }
    }

    cout << "Відновлена матриця зображення:\n";
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)
            printf("%6.2f ", result[i][j]);
        cout << endl;
    }

    return 0;
}