結果

問題 No.5024 魔法少女うなと宝集め
コンテスト
ユーザー mono
提出日時 2026-05-02 17:35:16
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 13,468 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,930 ms
コンパイル使用メモリ 386,052 KB
実行使用メモリ 6,400 KB
スコア 1,656,903
最終ジャッジ日時 2026-05-02 17:35:22
合計ジャッジ時間 5,490 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#pragma GCC optimize ("O3,inline,omit-frame-pointer,no-asynchronous-unwind-tables,fast-math")
#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define rep(i, a, n) for(int i = a; i < n; i++)
#define rrep(i, a, n) for(int i = a; i >= n; i--)
#define ll long long
template<class t,class u> void chmax(t&a,u b){if(a<b)a=b;}
template<class t,class u> void chmin(t&a,u b){if(b<a)a=b;}

int n, t;
vector<vector<int>> a(20,vector<int>(20));
vector<vector<ll>> paths = {
    {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,279,278,277,276,275,274,273,272,271,270,269,268,267,266,265,264,263,262,261,260,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,319,318,317,316,315,314,313,312,311,310,309,308,307,306,305,304,303,302,301,300,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,359,358,357,356,355,354,353,352,351,350,349,348,347,346,345,344,343,342,341,340,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,399,398,397,396,395,394,393,392,391,390,389,388,387,386,385,384,383,382,381,380},
    {19,39,59,79,99,119,139,159,179,199,219,239,259,279,299,319,339,359,379,399,398,378,358,338,318,298,278,258,238,218,198,178,158,138,118,98,78,58,38,18,17,37,57,77,97,117,137,157,177,197,217,237,257,277,297,317,337,357,377,397,396,376,356,336,316,296,276,256,236,216,196,176,156,136,116,96,76,56,36,16,15,35,55,75,95,115,135,155,175,195,215,235,255,275,295,315,335,355,375,395,394,374,354,334,314,294,274,254,234,214,194,174,154,134,114,94,74,54,34,14,13,33,53,73,93,113,133,153,173,193,213,233,253,273,293,313,333,353,373,393,392,372,352,332,312,292,272,252,232,212,192,172,152,132,112,92,72,52,32,12,11,31,51,71,91,111,131,151,171,191,211,231,251,271,291,311,331,351,371,391,390,370,350,330,310,290,270,250,230,210,190,170,150,130,110,90,70,50,30,10,9,29,49,69,89,109,129,149,169,189,209,229,249,269,289,309,329,349,369,389,388,368,348,328,308,288,268,248,228,208,188,168,148,128,108,88,68,48,28,8,7,27,47,67,87,107,127,147,167,187,207,227,247,267,287,307,327,347,367,387,386,366,346,326,306,286,266,246,226,206,186,166,146,126,106,86,66,46,26,6,5,25,45,65,85,105,125,145,165,185,205,225,245,265,285,305,325,345,365,385,384,364,344,324,304,284,264,244,224,204,184,164,144,124,104,84,64,44,24,4,3,23,43,63,83,103,123,143,163,183,203,223,243,263,283,303,323,343,363,383,382,362,342,322,302,282,262,242,222,202,182,162,142,122,102,82,62,42,22,2,1,21,41,61,81,101,121,141,161,181,201,221,241,261,281,301,321,341,361,381,380,360,340,320,300,280,260,240,220,200,180,160,140,120,100,80,60,40,20,0},
    {399,398,397,396,395,394,393,392,391,390,389,388,387,386,385,384,383,382,381,380,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,359,358,357,356,355,354,353,352,351,350,349,348,347,346,345,344,343,342,341,340,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,319,318,317,316,315,314,313,312,311,310,309,308,307,306,305,304,303,302,301,300,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,279,278,277,276,275,274,273,272,271,270,269,268,267,266,265,264,263,262,261,260,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,239,238,237,236,235,234,233,232,231,230,229,228,227,226,225,224,223,222,221,220,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19},
    {380,360,340,320,300,280,260,240,220,200,180,160,140,120,100,80,60,40,20,0,1,21,41,61,81,101,121,141,161,181,201,221,241,261,281,301,321,341,361,381,382,362,342,322,302,282,262,242,222,202,182,162,142,122,102,82,62,42,22,2,3,23,43,63,83,103,123,143,163,183,203,223,243,263,283,303,323,343,363,383,384,364,344,324,304,284,264,244,224,204,184,164,144,124,104,84,64,44,24,4,5,25,45,65,85,105,125,145,165,185,205,225,245,265,285,305,325,345,365,385,386,366,346,326,306,286,266,246,226,206,186,166,146,126,106,86,66,46,26,6,7,27,47,67,87,107,127,147,167,187,207,227,247,267,287,307,327,347,367,387,388,368,348,328,308,288,268,248,228,208,188,168,148,128,108,88,68,48,28,8,9,29,49,69,89,109,129,149,169,189,209,229,249,269,289,309,329,349,369,389,390,370,350,330,310,290,270,250,230,210,190,170,150,130,110,90,70,50,30,10,11,31,51,71,91,111,131,151,171,191,211,231,251,271,291,311,331,351,371,391,392,372,352,332,312,292,272,252,232,212,192,172,152,132,112,92,72,52,32,12,13,33,53,73,93,113,133,153,173,193,213,233,253,273,293,313,333,353,373,393,394,374,354,334,314,294,274,254,234,214,194,174,154,134,114,94,74,54,34,14,15,35,55,75,95,115,135,155,175,195,215,235,255,275,295,315,335,355,375,395,396,376,356,336,316,296,276,256,236,216,196,176,156,136,116,96,76,56,36,16,17,37,57,77,97,117,137,157,177,197,217,237,257,277,297,317,337,357,377,397,398,378,358,338,318,298,278,258,238,218,198,178,158,138,118,98,78,58,38,18,19,39,59,79,99,119,139,159,179,199,219,239,259,279,299,319,339,359,379,399},
    {0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380,381,361,341,321,301,281,261,241,221,201,181,161,141,121,101,81,61,41,21,1,2,22,42,62,82,102,122,142,162,182,202,222,242,262,282,302,322,342,362,382,383,363,343,323,303,283,263,243,223,203,183,163,143,123,103,83,63,43,23,3,4,24,44,64,84,104,124,144,164,184,204,224,244,264,284,304,324,344,364,384,385,365,345,325,305,285,265,245,225,205,185,165,145,125,105,85,65,45,25,5,6,26,46,66,86,106,126,146,166,186,206,226,246,266,286,306,326,346,366,386,387,367,347,327,307,287,267,247,227,207,187,167,147,127,107,87,67,47,27,7,8,28,48,68,88,108,128,148,168,188,208,228,248,268,288,308,328,348,368,388,389,369,349,329,309,289,269,249,229,209,189,169,149,129,109,89,69,49,29,9,10,30,50,70,90,110,130,150,170,190,210,230,250,270,290,310,330,350,370,390,391,371,351,331,311,291,271,251,231,211,191,171,151,131,111,91,71,51,31,11,12,32,52,72,92,112,132,152,172,192,212,232,252,272,292,312,332,352,372,392,393,373,353,333,313,293,273,253,233,213,193,173,153,133,113,93,73,53,33,13,14,34,54,74,94,114,134,154,174,194,214,234,254,274,294,314,334,354,374,394,395,375,355,335,315,295,275,255,235,215,195,175,155,135,115,95,75,55,35,15,16,36,56,76,96,116,136,156,176,196,216,236,256,276,296,316,336,356,376,396,397,377,357,337,317,297,277,257,237,217,197,177,157,137,117,97,77,57,37,17,18,38,58,78,98,118,138,158,178,198,218,238,258,278,298,318,338,358,378,398,399,379,359,339,319,299,279,259,239,219,199,179,159,139,119,99,79,59,39,19},
    {380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,379,378,377,376,375,374,373,372,371,370,369,368,367,366,365,364,363,362,361,360,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,339,338,337,336,335,334,333,332,331,330,329,328,327,326,325,324,323,322,321,320,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,299,298,297,296,295,294,293,292,291,290,289,288,287,286,285,284,283,282,281,280,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,259,258,257,256,255,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,139,138,137,136,135,134,133,132,131,130,129,128,127,126,125,124,123,122,121,120,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0},
    {399,379,359,339,319,299,279,259,239,219,199,179,159,139,119,99,79,59,39,19,18,38,58,78,98,118,138,158,178,198,218,238,258,278,298,318,338,358,378,398,397,377,357,337,317,297,277,257,237,217,197,177,157,137,117,97,77,57,37,17,16,36,56,76,96,116,136,156,176,196,216,236,256,276,296,316,336,356,376,396,395,375,355,335,315,295,275,255,235,215,195,175,155,135,115,95,75,55,35,15,14,34,54,74,94,114,134,154,174,194,214,234,254,274,294,314,334,354,374,394,393,373,353,333,313,293,273,253,233,213,193,173,153,133,113,93,73,53,33,13,12,32,52,72,92,112,132,152,172,192,212,232,252,272,292,312,332,352,372,392,391,371,351,331,311,291,271,251,231,211,191,171,151,131,111,91,71,51,31,11,10,30,50,70,90,110,130,150,170,190,210,230,250,270,290,310,330,350,370,390,389,369,349,329,309,289,269,249,229,209,189,169,149,129,109,89,69,49,29,9,8,28,48,68,88,108,128,148,168,188,208,228,248,268,288,308,328,348,368,388,387,367,347,327,307,287,267,247,227,207,187,167,147,127,107,87,67,47,27,7,6,26,46,66,86,106,126,146,166,186,206,226,246,266,286,306,326,346,366,386,385,365,345,325,305,285,265,245,225,205,185,165,145,125,105,85,65,45,25,5,4,24,44,64,84,104,124,144,164,184,204,224,244,264,284,304,324,344,364,384,383,363,343,323,303,283,263,243,223,203,183,163,143,123,103,83,63,43,23,3,2,22,42,62,82,102,122,142,162,182,202,222,242,262,282,302,322,342,362,382,381,361,341,321,301,281,261,241,221,201,181,161,141,121,101,81,61,41,21,1,0,20,40,60,80,100,120,140,160,180,200,220,240,260,280,300,320,340,360,380},
    {19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,139,138,137,136,135,134,133,132,131,130,129,128,127,126,125,124,123,122,121,120,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,219,218,217,216,215,214,213,212,211,210,209,208,207,206,205,204,203,202,201,200,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,259,258,257,256,255,254,253,252,251,250,249,248,247,246,245,244,243,242,241,240,260,261,262,263,264,265,266,267,268,269,270,271,272,273,274,275,276,277,278,279,299,298,297,296,295,294,293,292,291,290,289,288,287,286,285,284,283,282,281,280,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,339,338,337,336,335,334,333,332,331,330,329,328,327,326,325,324,323,322,321,320,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,356,357,358,359,379,378,377,376,375,374,373,372,371,370,369,368,367,366,365,364,363,362,361,360,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399},
};

int main() {
    cin >> n >> t;
    rep(i,0,n)rep(j,0,n) cin >> a[i][j];
    int besti = -1, bestj = -1, score = -1;
    rep(i,0,paths.size()){
        auto vec = paths[i];
        int num = 0;
        int l = 0, r = 0;
        rep(j,0,t){
            auto x = vec[j]/n, y = vec[j]%n;
            num += a[x][y], r++;
        }
        if(score < num){
            besti = i;
            bestj = l;
            score = num;
        }
        while(r < n*n){
            {
                auto x = vec[r]/n, y = vec[r]%n;
                num += a[x][y], r++;
            }
            {
                auto x = vec[l]/n, y = vec[l]%n;
                num -= a[x][y], l++;
            }
            if(score < num){
                besti = i;
                bestj = l;
                score = num;
            }
        }
    }
    cout << t << endl;
    auto vec = paths[besti];
    rep(i,bestj,bestj+t){
        auto x = vec[i]/n, y = vec[i]%n;
        cout << x << " " << y << endl;
    }

    return 0;
}
0