Submission #65028664


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[25][25];
ll L[25][110000], R[25][110000];
int lc[25], rc[25];
int n, M;
ll qp;
ll fp(int b, int e, int M)
{
    ll r = 1, p = b;
    while (e)
    {
        if (e & 1)
            r = (r * p) % M;
        p = (p * p) % M;
        e >>= 1;
    }
    return r;
}
void dfsL(int x, int y, int d, ll v)
{
    if (d == n - 1)
    {
        L[x][lc[x]++] = v;
        return;
    }
    if (x < n)
        dfsL(x + 1, y, d + 1, (v * 10 + a[x + 1][y]) % M);
    if (y < n)
        dfsL(x, y + 1, d + 1, (v * 10 + a[x][y + 1]) % M);
}
void dfsR(int x, int y, int d, ll v, int r)
{
    if (d == n - 1)
    {
        if (x == n && y == n)
            R[r][rc[r]++] = v;
        return;
    }
    if (x < n)
        dfsR(x + 1, y, d + 1, (v * 10 + a[x + 1][y]) % M, r);
    if (y < n)
        dfsR(x, y + 1, d + 1, (v * 10 + a[x][y + 1]) % M, r);
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    cin >> n >> M;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            cin >> a[i][j];
        }
    }
    dfsL(1, 1, 0, (ll)a[1][1] % M);
    for (int i = 1; i <= n; i++)
    {
        int j = n + 1 - i;
        if (j >= 1 && j <= n)
            dfsR(i, j, 0, 0, i);
    }
    qp = fp(10, n - 1, M);
    ll ans = -1;
    for (int i = 1; i <= n; i++)
    {
        if (lc[i] == 0 || rc[i] == 0)
            continue;
        sort(R[i], R[i] + rc[i]);
        for (int j = 0; j < lc[i]; j++)
        {
            ll lv = (L[i][j] * qp) % M;
            ll tar = M - 1 - lv;
            int lo = 0, hi = rc[i] - 1, pos = -1;
            while (lo <= hi)
            {
                int mid = lo + ((hi - lo) >> 1);
                if (R[i][mid] <= tar)
                {
                    pos = mid;
                    lo = mid + 1;
                }
                else
                    hi = mid - 1;
            }
            if (pos != -1)
                ans = max(ans, lv + R[i][pos]);
            lo = 0, hi = rc[i] - 1, pos = -1;
            while (lo <= hi)
            {
                int mid = lo + ((hi - lo) >> 1);
                if (R[i][mid] > tar)
                {
                    pos = mid;
                    hi = mid - 1;
                }
                else
                    lo = mid + 1;
            }
            if (pos != -1)
                ans = max(ans, (lv + R[i][pos]) % M);
        }
    }
    cout << ans;
    return 0;
}

Submission Info

Submission Time
Task F - Path to Integer
User mywwzh
Language C++ 17 (gcc 12.2)
Score 525
Code Size 2628 Byte
Status AC
Exec Time 124 ms
Memory 12008 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 525 / 525
Status
AC × 3
AC × 44
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_handmade_00.txt, 01_handmade_01.txt, 01_handmade_02.txt, 01_handmade_03.txt, 01_handmade_04.txt, 01_handmade_05.txt, 01_handmade_06.txt, 01_handmade_07.txt, 01_handmade_08.txt, 01_handmade_09.txt, 01_handmade_10.txt, 01_handmade_11.txt, 01_handmade_12.txt, 01_handmade_13.txt, 01_handmade_14.txt, 01_handmade_15.txt, 02_random_00.txt, 02_random_01.txt, 02_random_02.txt, 02_random_03.txt, 02_random_04.txt, 02_random_05.txt, 02_random_06.txt, 02_random_07.txt, 02_random_08.txt, 02_random_09.txt, 02_random_10.txt, 02_random_11.txt, 02_random_12.txt, 02_random_13.txt, 02_random_14.txt, 02_random_15.txt, 02_random_16.txt, 02_random_17.txt, 02_random_18.txt, 02_random_19.txt, 02_random_20.txt, 02_random_21.txt, 02_random_22.txt, 02_random_23.txt, 02_random_24.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 1 ms 3464 KiB
00_sample_01.txt AC 1 ms 3620 KiB
00_sample_02.txt AC 1 ms 3556 KiB
01_handmade_00.txt AC 1 ms 3584 KiB
01_handmade_01.txt AC 1 ms 3516 KiB
01_handmade_02.txt AC 1 ms 3516 KiB
01_handmade_03.txt AC 1 ms 3552 KiB
01_handmade_04.txt AC 1 ms 3596 KiB
01_handmade_05.txt AC 3 ms 3900 KiB
01_handmade_06.txt AC 42 ms 11880 KiB
01_handmade_07.txt AC 41 ms 11948 KiB
01_handmade_08.txt AC 40 ms 12008 KiB
01_handmade_09.txt AC 39 ms 11820 KiB
01_handmade_10.txt AC 1 ms 3456 KiB
01_handmade_11.txt AC 1 ms 3756 KiB
01_handmade_12.txt AC 64 ms 11852 KiB
01_handmade_13.txt AC 114 ms 11764 KiB
01_handmade_14.txt AC 40 ms 11884 KiB
01_handmade_15.txt AC 40 ms 11884 KiB
02_random_00.txt AC 1 ms 3464 KiB
02_random_01.txt AC 1 ms 3584 KiB
02_random_02.txt AC 1 ms 3604 KiB
02_random_03.txt AC 1 ms 3648 KiB
02_random_04.txt AC 1 ms 3608 KiB
02_random_05.txt AC 1 ms 3608 KiB
02_random_06.txt AC 1 ms 3492 KiB
02_random_07.txt AC 1 ms 3620 KiB
02_random_08.txt AC 12 ms 4672 KiB
02_random_09.txt AC 1 ms 3612 KiB
02_random_10.txt AC 111 ms 11808 KiB
02_random_11.txt AC 124 ms 11880 KiB
02_random_12.txt AC 111 ms 11764 KiB
02_random_13.txt AC 111 ms 11824 KiB
02_random_14.txt AC 110 ms 11784 KiB
02_random_15.txt AC 121 ms 11880 KiB
02_random_16.txt AC 117 ms 11816 KiB
02_random_17.txt AC 107 ms 11756 KiB
02_random_18.txt AC 111 ms 11736 KiB
02_random_19.txt AC 112 ms 11776 KiB
02_random_20.txt AC 115 ms 11884 KiB
02_random_21.txt AC 118 ms 11780 KiB
02_random_22.txt AC 113 ms 12000 KiB
02_random_23.txt AC 116 ms 11776 KiB
02_random_24.txt AC 114 ms 11884 KiB