Submission #65038890


Source Code Expand

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll modexp(ll base, int exp, int mod) {
    ll result = 1;
    base %= mod;
    while(exp > 0) {
        if(exp & 1)
            result = (result * base) % mod;
        base = (base * base) % mod;
        exp >>= 1;
    }
    return result;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N, M;
    cin >> N >> M;
    vector<vector<int>> a(N, vector<int>(N));
    for (int i = 0; i < N; i++){
        for (int j = 0; j < N; j++){
            cin >> a[i][j];
        }
    }
    
    int moves = N - 1;
    vector<map<int,int>> mp1(N);
    for (int mask = 0; mask < (1 << moves); mask++){
        int r = 0, c = 0;
        int F = a[0][0] % M;
        for (int j = 0; j < moves; j++){
            if(mask & (1 << j))
                r++;     
            else
                c++;   
            F = (int)(((ll)F * 10 + a[r][c]) % M);
        }
        mp1[r][F]++;
    }
    vector<map<int,int>> mp2(N);
    for (int r = 0; r < N; r++){
        for (int mask = 0; mask < (1 << moves); mask++){
            if(__builtin_popcount(mask) != (moves - r))
                continue;
            int cr = r, cc = (N - 1 - r);
            int T = 0;
            for (int j = 0; j < moves; j++){
                if(mask & (1 << j))
                    cr++;
                else
                    cc++;
                T = (int)(((ll)T * 10 + a[cr][cc]) % M);
            }
            mp2[r][T]++;
        }
    }

    int factor = (int)modexp(10, moves, M);
    int ans = 0;
    for (int r = 0; r < N; r++){
        for (auto &p : mp1[r]){
            int F = p.first;
            int X = (int)(((ll)F * factor) % M);
            
            if(mp2[r].empty()) continue;
            
            int currentCandidate = 0;
            
            int threshold = M - X;
            auto it = mp2[r].lower_bound(threshold);
            if(it != mp2[r].begin()){
                int T_candidate = prev(it)->first;            
                int candidate1 = X + T_candidate;
                currentCandidate = candidate1;
            }
            
            int T_max = mp2[r].rbegin()->first;
            int candidate2 = (X + T_max) % M;
            currentCandidate = max(currentCandidate, candidate2);
            
            ans = max(ans, currentCandidate);
        }
    }
    
    cout << ans << "\n";
    return 0;
}

Submission Info

Submission Time
Task F - Path to Integer
User Marco_Escandon
Language C++ 20 (gcc 12.2)
Score 525
Code Size 2518 Byte
Status AC
Exec Time 529 ms
Memory 43928 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 3476 KiB
00_sample_01.txt AC 1 ms 3448 KiB
00_sample_02.txt AC 1 ms 3592 KiB
01_handmade_00.txt AC 1 ms 3484 KiB
01_handmade_01.txt AC 1 ms 3404 KiB
01_handmade_02.txt AC 1 ms 3344 KiB
01_handmade_03.txt AC 1 ms 3320 KiB
01_handmade_04.txt AC 1 ms 3512 KiB
01_handmade_05.txt AC 9 ms 4748 KiB
01_handmade_06.txt AC 118 ms 3608 KiB
01_handmade_07.txt AC 92 ms 3508 KiB
01_handmade_08.txt AC 93 ms 3400 KiB
01_handmade_09.txt AC 93 ms 3504 KiB
01_handmade_10.txt AC 1 ms 3444 KiB
01_handmade_11.txt AC 3 ms 3756 KiB
01_handmade_12.txt AC 173 ms 3564 KiB
01_handmade_13.txt AC 381 ms 30436 KiB
01_handmade_14.txt AC 109 ms 3492 KiB
01_handmade_15.txt AC 109 ms 3388 KiB
02_random_00.txt AC 1 ms 3452 KiB
02_random_01.txt AC 2 ms 3460 KiB
02_random_02.txt AC 1 ms 3608 KiB
02_random_03.txt AC 2 ms 3664 KiB
02_random_04.txt AC 1 ms 3408 KiB
02_random_05.txt AC 1 ms 3520 KiB
02_random_06.txt AC 1 ms 3384 KiB
02_random_07.txt AC 2 ms 3748 KiB
02_random_08.txt AC 41 ms 7844 KiB
02_random_09.txt AC 1 ms 3528 KiB
02_random_10.txt AC 407 ms 33328 KiB
02_random_11.txt AC 529 ms 43928 KiB
02_random_12.txt AC 399 ms 28104 KiB
02_random_13.txt AC 438 ms 34276 KiB
02_random_14.txt AC 399 ms 27216 KiB
02_random_15.txt AC 441 ms 37056 KiB
02_random_16.txt AC 420 ms 33852 KiB
02_random_17.txt AC 377 ms 28516 KiB
02_random_18.txt AC 429 ms 32652 KiB
02_random_19.txt AC 368 ms 26996 KiB
02_random_20.txt AC 472 ms 41396 KiB
02_random_21.txt AC 447 ms 35540 KiB
02_random_22.txt AC 401 ms 32140 KiB
02_random_23.txt AC 438 ms 36988 KiB
02_random_24.txt AC 350 ms 26732 KiB