Submission #65037914
Source Code Expand
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < (int)(n); ++i)
#define RREP(i, n) for(int i = (int)(n); i-- > 0;)
#define FOR(i, l, r) for(int i = (int)(l); i < (int)(r); ++i)
#define RFOR(i, r, l) for(int i = (int)(r); i-- > (int)(l);)
#define ALL(v) std::begin(v), std::end(v)
using llong = long long;
using vi = std::vector<int>;
using vvi = std::vector<vi>;
using pii = std::pair<int, int>;
using namespace std;
constexpr int INF = 1e9;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr int MOD = 998'244'353;
constexpr int MOD2 = 1e9 + 7;
template <typename Type>
inline std::istream &operator>>(std::istream &is, std::vector<Type> &v) {
for(auto &elem : v) is >> elem;
return is;
}
template <typename Type>
inline std::ostream &operator<<(std::ostream &os, const std::vector<Type> &v) {
for(auto iter = v.cbegin(); iter != v.cend(); ++iter) os << (iter == v.cbegin() ? "" : " ") << *iter;
return os;
}
#ifdef DEBUG
#include "debug.hpp"
#else
#define debug(...) static_cast<void>(0)
#endif
// 繰り返し二乗法(mod付き).O(logK).
constexpr long long mod_pow(long long n, long long k, int mod) {
assert(k >= 0);
assert(mod >= 1);
long long res = 1;
n %= mod;
while(k > 0) {
if(k & 1LL) res = res * n % mod;
n = n * n % mod;
k >>= 1;
}
return res;
}
int main(){
int n;
int m;
cin>>n>>m;
vvi a(n,vi(n));
REP(i,n)REP(j,n) cin>>a[i][j];
// if(n<=10){
// string s(2*n-1,'0');
// auto calc=[&]()->llong{
// // debug(s);
// llong res=0;
// for(auto c:s) res=(res*10+(c-'0'))%m;
// return res;
// };
// llong ans=0;
// auto dfs=[&](auto self,int y,int x)->void{
// // debug(y,x);
// s[y+x]='0'+a[y][x];
// if(y==n-1 and x==n-1){
// ans=max(ans,calc());
// return;
// }
// if(y<n-1) self(self,y+1,x);
// if(x<n-1) self(self,y,x+1);
// };
// dfs(dfs,0,0);
// cout<<ans<<endl;
// return 0;
// }
string s(n,'0');
auto calc=[&]()->llong{
llong res=0;
for(auto c:s) res=(res*10+(c-'0'))%m;
return res;
};
vector<vector<llong>> v(n);
auto dfs=[&](auto self,int y,int x)->void{
s[y+x]='0'+a[y][x];
if(y+x==n-1){
v[y].push_back(calc());
return;
}
if(y<n-1) self(self,y+1,x);
if(x<n-1) self(self,y,x+1);
};
dfs(dfs,0,0);
vector<vector<llong>> w(n);
auto dfs2=[&](auto self,int y,int x)->void{
s[y+x-(n-1)]='0'+a[y][x];
if(y+x==n-1){
s[0]='0';
w[y].push_back(calc());
return;
}
if(y>0) self(self,y-1,x);
if(x>0) self(self,y,x-1);
};
dfs2(dfs2,n-1,n-1);
for(auto &elem:w) sort(ALL(elem));
llong ans=0;
llong ad=mod_pow(10,n-1,m);
REP(i,n){
for(auto a:v[i]){
llong b;
llong want=m-a*ad%m;
auto itr=lower_bound(ALL(w[i]),want);
if(itr==w[i].begin()) b=w[i].back();
else b=*prev(itr);
llong tmp=(a*ad%m+b)%m;
ans=max(ans,tmp);
}
}
cout<<ans<<endl;
}
Submission Info
| Submission Time |
|
| Task |
F - Path to Integer |
| User |
Today |
| Language |
C++ 23 (gcc 12.2) |
| Score |
525 |
| Code Size |
3049 Byte |
| Status |
AC |
| Exec Time |
169 ms |
| Memory |
13692 KiB |
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
525 / 525 |
| Status |
|
|
| 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 |
3444 KiB |
| 00_sample_02.txt |
AC |
1 ms |
3636 KiB |
| 01_handmade_00.txt |
AC |
1 ms |
3560 KiB |
| 01_handmade_01.txt |
AC |
1 ms |
3400 KiB |
| 01_handmade_02.txt |
AC |
1 ms |
3492 KiB |
| 01_handmade_03.txt |
AC |
1 ms |
3640 KiB |
| 01_handmade_04.txt |
AC |
1 ms |
3504 KiB |
| 01_handmade_05.txt |
AC |
5 ms |
3812 KiB |
| 01_handmade_06.txt |
AC |
103 ms |
13692 KiB |
| 01_handmade_07.txt |
AC |
105 ms |
13488 KiB |
| 01_handmade_08.txt |
AC |
105 ms |
13692 KiB |
| 01_handmade_09.txt |
AC |
103 ms |
13604 KiB |
| 01_handmade_10.txt |
AC |
1 ms |
3488 KiB |
| 01_handmade_11.txt |
AC |
2 ms |
3568 KiB |
| 01_handmade_12.txt |
AC |
130 ms |
13572 KiB |
| 01_handmade_13.txt |
AC |
160 ms |
13484 KiB |
| 01_handmade_14.txt |
AC |
102 ms |
13604 KiB |
| 01_handmade_15.txt |
AC |
102 ms |
13564 KiB |
| 02_random_00.txt |
AC |
1 ms |
3424 KiB |
| 02_random_01.txt |
AC |
1 ms |
3524 KiB |
| 02_random_02.txt |
AC |
1 ms |
3436 KiB |
| 02_random_03.txt |
AC |
2 ms |
3528 KiB |
| 02_random_04.txt |
AC |
1 ms |
3560 KiB |
| 02_random_05.txt |
AC |
1 ms |
3508 KiB |
| 02_random_06.txt |
AC |
1 ms |
3488 KiB |
| 02_random_07.txt |
AC |
1 ms |
3608 KiB |
| 02_random_08.txt |
AC |
17 ms |
4620 KiB |
| 02_random_09.txt |
AC |
1 ms |
3440 KiB |
| 02_random_10.txt |
AC |
160 ms |
13496 KiB |
| 02_random_11.txt |
AC |
169 ms |
13520 KiB |
| 02_random_12.txt |
AC |
160 ms |
13628 KiB |
| 02_random_13.txt |
AC |
160 ms |
13468 KiB |
| 02_random_14.txt |
AC |
156 ms |
13628 KiB |
| 02_random_15.txt |
AC |
163 ms |
13492 KiB |
| 02_random_16.txt |
AC |
163 ms |
13548 KiB |
| 02_random_17.txt |
AC |
157 ms |
13484 KiB |
| 02_random_18.txt |
AC |
159 ms |
13568 KiB |
| 02_random_19.txt |
AC |
159 ms |
13692 KiB |
| 02_random_20.txt |
AC |
162 ms |
13556 KiB |
| 02_random_21.txt |
AC |
166 ms |
13500 KiB |
| 02_random_22.txt |
AC |
160 ms |
13516 KiB |
| 02_random_23.txt |
AC |
161 ms |
13540 KiB |
| 02_random_24.txt |
AC |
159 ms |
13568 KiB |