Submission #00362


ソースコード

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int MAX = 1e5 + 10;
long long factArray[MAX], invFactArray[MAX], invArray[MAX];
long long powMod(long long a, long long p) {
if (p == 0) {
return 1;
}
if (p % 2) {
return a * powMod(a, p - 1) % MOD;
} else {
long long t = powMod(a, p / 2);
return t * t % MOD;
}
}
long long fact(int n) {
if (factArray[n] != -1) {
return factArray[n];
}
return factArray[n] = n * fact(n - 1) % MOD;
}
using row = std::vector<long long>;
using matrix = std::vector<row>;
//(A * B) mod
inline matrix matrixMul(matrix &A, matrix &B, const int mod) {
const int asz = A.size();
const int bsz = B.size();
const int bcolsz = B.front().size();
matrix res(asz, row(bcolsz, 0));
for (int i = 0; i < asz; i++) {
for (int k = 0; k < bsz; k++) {
for (int j = 0; j < bcolsz; j++) {
(res[i][j] += A[i][k] * B[k][j]) %= mod;
}
}
}
return res;
}
//(mat^p) mod
inline matrix matPowMod(matrix &mat, long long p, const int mod) {
const int matsz = mat.size();
assert(mat.front().size() == matsz);//must square matrix!!
matrix res(matsz, row(matsz, 0));
for (int i = 0; i < matsz; i++) {
res[i][i] = 1;
}
while (p) {
if (p & 1) res = matrixMul(res, mat, mod);
mat = matrixMul(mat, mat, mod);
p >>= 1;
}
return res;
}
// ( matrix * vector ) % mod
inline row mul(matrix mat, row r, const int mod) {
assert(mat[0].size() == r.size());
row res(mat.size(), 0);
for (int i = 0; i < mat.size(); i++) {
for (int j = 0; j < r.size(); j++) {
(res[i] += mat[i][j] * r[j]) %= mod;
}
}
return res;
}
long long allCnt[10], cntCnt[10];
long long a[MAX], b[MAX];
pair<long long, long long> mi = make_pair(MOD, 9);
long long calcMinNum() {
long long res = 0;
if (cntCnt[0]) {
allCnt[mi.first] -= mi.second;
vector<vector<long long>> mat(2, vector<long long>(2));
mat[0][0] = 10;
mat[0][1] = mi.first;
mat[1][0] = 0;
mat[1][1] = 1;
auto powMat = matPowMod(mat, mi.second - 1, MOD);
auto row = mul(powMat, vector<long long>{mi.first, 1}, MOD);
(res *= powMod(10, mi.second)) %= MOD;
(res += row[0]) %= MOD;
}
for (int i = 0; i < 10; i++) {
if (allCnt[i]) {
vector<vector<long long>> mat(2, vector<long long>(2));
mat[0][0] = 10;
mat[0][1] = i;
mat[1][0] = 0;
mat[1][1] = 1;
auto powMat = matPowMod(mat, allCnt[i] - 1, MOD);
auto row = mul(powMat, vector<long long>{i, 1}, MOD);
(res *= powMod(10, allCnt[i])) %= MOD;
(res += row[0]) %= MOD;
}
}
return res;
}
int main() {
memset(factArray, -1, sizeof(factArray));
factArray[0] = 1;
int N;
scanf("%d", &N);
for (int i = 0; i < N; i++) {
scanf("%lld%lld", a + i, b + i);
allCnt[a[i]] += b[i];
cntCnt[a[i]]++;
if (a[i] != 0) {
mi = min(mi, make_pair(a[i], b[i]));
}
}
int miCnt = 0;
for (int i = 0; i < N; i++) {
if (a[i] == mi.first && b[i] == mi.second) ++miCnt;
}
if (N == 1 && allCnt[0]) {
puts("0\n1");
return 0;
}
const long long miNum = calcMinNum();
long long comb = 1;
if (allCnt[0]) {
cntCnt[mi.first]--;
(comb *= miCnt) %= MOD;
}
for (int i = 0; i < 10; i++) {
(comb *= fact(cntCnt[i])) %= MOD;
}
printf("%lld\n%lld\n", miNum, comb);
return 0;
}

ステータス

項目 データ
問題 0003 - repdigit
ユーザー名 sekiya9311
投稿日時 2017-07-07 22:59:48
言語 C++17
状態 Time Limit Exceeded
得点 0
ソースコード長 3604 Byte
最大実行時間 1000 ms
最大メモリ使用量 4588 KB

セット

セット 得点 Cases
1 ALL 0 / 100 *

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
01_sample1.in AC 14 ms 1248 KB
1
01_sample2.in AC 14 ms 1328 KB
1
01_sample3.in AC 12 ms 1148 KB
1
02_handmake1.in AC 14 ms 1224 KB
1
02_handmake2.in AC 12 ms 1308 KB
1
02_handmake3.in AC 15 ms 1256 KB
1
02_handmake4.in AC 12 ms 1212 KB
1
02_handmake5.in AC 14 ms 1288 KB
1
02_handmake6.in AC 15 ms 1240 KB
1
02_handmake7.in AC 13 ms 1316 KB
1
02_handmake8.in AC 14 ms 1268 KB
1
02_handmake9.in AC 13 ms 1348 KB
1
03_random1.in AC 13 ms 1172 KB
1
03_random2.in AC 13 ms 1252 KB
1
03_random3.in AC 16 ms 1332 KB
1
03_random4.in AC 15 ms 1408 KB
1
03_random5.in AC 22 ms 1360 KB
1
03_random6.in AC 21 ms 1308 KB
1
04_random1.in AC 12 ms 1384 KB
1
04_random2.in AC 12 ms 1464 KB
1
04_random3.in AC 14 ms 1544 KB
1
04_random4.in AC 15 ms 1372 KB
1
04_random5.in AC 12 ms 1452 KB
1
04_random6.in AC 16 ms 1400 KB
1
05_random1.in AC 36 ms 3144 KB
1
05_random2.in AC 26 ms 3052 KB
1
05_random3.in AC 24 ms 3096 KB
1
05_random4.in AC 36 ms 3136 KB
1
06_random1.in AC 28 ms 4588 KB
1
06_random3.in TLE 1000 ms 2960 KB
1