UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#197430#3448. 心加心Zeardoe1001696ms103664kbC++112.9kb2023-11-12 09:33:162023-11-12 13:42:53

answer

/*
[templates]: 
duipai
spjdp
compre
addhis
floor_sum
treedfs
matrix
network_flow
polynomial
lca
bitset
valuesgt
fenwick
erbitree
*/
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx")
#include<bits/stdc++.h>
using namespace std;
// #define int long long
//use ll instead of int.
#define f(i, a, b) for(int i = (a); i <= (b); i++)
#define cl(i, n) i.clear(),i.resize(n);
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int inf = 1e9;
//#define cerr if(false)cerr
//#define freopen if(false)freopen
mt19937 rng(time(0)); 
int rnd(int l, int r) {return rng() % (r-l+1) + l; }
#define watch(x) cerr  << (#x) << ' '<<'i'<<'s'<<' ' << x << endl
void pofe(int number, int bitnum) {
    string s; f(i, 0, bitnum) {s += char(number & 1) + '0'; number >>= 1; } 
    reverse(s.begin(), s.end()); cerr << s << endl; 
    return;
}
template <typename TYP> void cmax(TYP &x, TYP y) {if(x < y) x = y;}
template <typename TYP> void cmin(TYP &x, TYP y) {if(x > y) x = y;}
//调不出来给我对拍!
//use std::array.
const int N = 5000; 
int a[N + 10][220], len[N + 10], pw[220]; bool vis[N + 10]; 
int w[N + 10][N + 10]; int dis[N + 10], ans; 
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(NULL);
    cout.tie(NULL);
    //freopen();
    //freopen();
    //time_t start = clock();
    //think twice,code once.
    //think once,debug forever.
    int n, p; cin >> n >> p; 
    pw[0] = 1; 
    f(i, 1, 200) pw[i] = pw[i - 1] * 10 % p;  
    f(i, 0, p - 1) f(j, 0, p - 1) w[i][j] = inf; 
    f(i, 1, n) {
        string s; cin >> s;
        len[i] = s.size(); 
        int cnt = 0, cur = 0; 
        for(char ch : s) {
            a[i][len[i] - ++cnt] = ch - '0'; 
            cur = (cur * 10 + a[i][len[i] - cnt]) % p; 
        }
        // cerr << 0 << " " << cur << " " << len[i] << endl; 
        cmin(w[0][cur], len[i]); 
        f(j, 1, p - 1) {
            cur += pw[len[i]]; 
            cur %= p; 
            // cerr << j << " " << cur << " " << len[i] << endl; 
            cmin(w[j][cur], len[i]); 
        }
    } 
    f(i, 1, p - 1) dis[i] = inf; 
    int now = 0; ans = inf; 
    f(tt, 1, p) {
        vis[now] = 1; 
        int tmp = 0, mx = inf; 
        f(i, 0, p - 1) {
            int iw = w[now][i]; 
            if(i == 0) cmin(ans, dis[now] + iw); 
            else if(!vis[i]) {
                cmin(dis[i], dis[now] + iw);
                if(dis[i] < mx) {
                    mx = dis[i]; 
                    tmp = i; 
                } 
            }
        }
        now = tmp; 
    } 
    cout << (ans == inf ? -1 : ans) << endl; 
    for(int i = p - 1; i >= 1; i --) cout << (dis[i] == inf ? -1 : dis[i]) << endl;
    //time_t finish = clock();
    //cout << "time used:" << (finish-start) * 1.0 / CLOCKS_PER_SEC <<"s"<< endl;
    return 0;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 1ms
memory: 3756kb

input:

10 98
0
1
2
3
4
5
6
7
8
9

output:

1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
...

result:

ok 98 numbers

Test #2:

score: 10
Accepted
time: 13ms
memory: 7860kb

input:

5000 99
14781892056687055378359451878122218601921996058131918743098670380384485452067769009096639454...

output:

1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
...

result:

ok 99 numbers

Test #3:

score: 10
Accepted
time: 0ms
memory: 3760kb

input:

3 99
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5
0
4
5...

output:

1
4
7
7
4
4
5
5
8
4
4
4
8
8
4
4
8
9
8
4
8
8
8
8
8
8
8
9
7
7
7
7
7
6
5
5
5
5
6
3
3
3
7
6
2
2
5
5
6
2
...

result:

ok 99 numbers

Test #4:

score: 10
Accepted
time: 0ms
memory: 3744kb

input:

6 95
0
2
3
5
6
9
0
2
3
5
6
9
0
2
3
5
6
9
0
2
3
5
6
9
0
2
3
5
6
9
0
2
3
5
6
9
0
2
3
5
6
9
0
2
3
5
6
9...

output:

1
3
2
2
3
2
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
3
2
3
3
2
2
3
2
2
3
2
2
3
3
2
2
3
2
2
3
2
3
3
3
3
...

result:

ok 95 numbers

Test #5:

score: 10
Accepted
time: 0ms
memory: 3332kb

input:

5 3
412
986
7687
79583
8318

output:

6
3
3

result:

ok 3 number(s): "6 3 3"

Test #6:

score: 10
Accepted
time: 0ms
memory: 3332kb

input:

5 3
5387
39817
6
6804
1

output:

1
2
1

result:

ok 3 number(s): "1 2 1"

Test #7:

score: 10
Accepted
time: 13ms
memory: 7860kb

input:

5000 100
4662215847345417996058539422385909140989122582319939315897492645456767910985172332639303829...

output:

180
180
180
180
180
180
180
180
180
180
180
181
180
180
180
181
180
180
180
181
181
180
180
181
180
...

result:

ok 100 numbers

Test #8:

score: 10
Accepted
time: 14ms
memory: 7864kb

input:

5000 100
6017404839398900982117908052227697611177103082938397805192623914538500903540418343249962031...

output:

180
180
180
181
182
180
180
181
180
181
180
180
180
180
180
180
180
180
180
180
180
181
180
180
180
...

result:

ok 100 numbers

Test #9:

score: 10
Accepted
time: 705ms
memory: 103664kb

input:

5000 5000
173577890992330520159895384217403731027027923558584792233344058149389687419038746812531024...

output:

196
184
183
192
192
195
-1
181
194
199
180
-1
180
-1
-1
-1
190
194
-1
185
182
-1
-1
-1
188
184
-1
19...

result:

ok 5000 numbers

Test #10:

score: 10
Accepted
time: 950ms
memory: 103664kb

input:

5000 5000
109684703722761599523811752824414002980896386859466348397242225190298237477285061568577404...

output:

184
180
191
185
195
195
-1
182
181
183
199
-1
192
-1
-1
-1
181
182
-1
185
-1
188
189
190
-1
190
188
...

result:

ok 5000 numbers

Extra Test:

score: 0
Extra Test Passed