Submission #00297


ソースコード

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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#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 inv(long long n) {
return powMod(n, MOD - 2);
}
long long fact(int n) {
if (factArray[n] != -1) {
return factArray[n];
}
return factArray[n] = n * fact(n - 1) % MOD;
}
long long invFact(int n) {
if (invFactArray[n] != -1) {
return invFactArray[n];
}
return invFactArray[n] = inv(fact(n)) % MOD;
}
long long nCr(int n, int r) {
if (n < r) {
return 0;
}
return fact(n) % MOD * invFact(n - r) % MOD * invFact(r) % MOD;
}
void init() {
memset(invArray, -1, sizeof(invArray));
memset(factArray, -1, sizeof(factArray));
memset(invFactArray, -1, sizeof(invFactArray));
factArray[0] = invFactArray[0] = 1;
for (int i = 0; i < MAX; i++) {
fact(i);
}
for (int i = 0; i < MAX; i++) {
invFact(i);
}
}
using row = std::vector<long long>;
using matrix = std::vector<row>;
//(A * B) mod
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
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
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];
pair<long long, long long> mi = make_pair(MOD, 9);
long long calcMinNum() {
if (cntCnt[0]) {
allCnt[mi.second] -= mi.first;
}
long long res = 0;
if (cntCnt[0]) {
vector<vector<long long>> mat(2, vector<long long>(2));
mat[0][0] = 10;
mat[0][1] = mi.second;
mat[1][0] = 0;
mat[1][1] = 1;
auto powMat = matPowMod(mat, mi.first - 1, MOD);
auto row = mul(powMat, vector<long long>{mi.second, 1}, MOD);
(res *= powMod(10, mi.first)) %= 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() {
init();
int N;
scanf("%d", &N);
if (N == 1) {
long long a, b;
scanf("%lld%lld", &a, &b);
long long num = calcMinNum();
printf("%lld\n", num);
puts("1");
return 0;
}
for (int i = 0; i < N; i++) {
long long a, b;
scanf("%lld%lld", &a, &b);
allCnt[a] += b;
cntCnt[a]++;
mi = min(mi, make_pair(b, a));
}
const long long miNum = calcMinNum();
if (allCnt[0]) {
cntCnt[mi.second]--;
}
long long comb = 1;
for (int i = 0; i < 10; i++) {
comb *= fact(cntCnt[i]);
}
printf("%lld\n%lld\n", miNum, comb);
return 0;
}

ステータス

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

セット

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

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
01_sample1.in AC 36 ms 2784 KB
1
01_sample2.in AC 43 ms 2720 KB
1
01_sample3.in AC 34 ms 2780 KB
1
02_handmake1.in WA 34 ms 2844 KB
1
02_handmake2.in WA 36 ms 2908 KB
1
02_handmake3.in WA 39 ms 2840 KB
1
02_handmake4.in WA 35 ms 2904 KB
1
02_handmake5.in WA 37 ms 2968 KB
1
02_handmake6.in WA 33 ms 2900 KB
1
02_handmake7.in WA 34 ms 2832 KB
1
02_handmake8.in AC 38 ms 2896 KB
1
02_handmake9.in WA 38 ms 2960 KB
1
03_random1.in WA 35 ms 2896 KB
1
03_random2.in WA 38 ms 2960 KB
1
03_random3.in WA 36 ms 3020 KB
1
03_random4.in WA 39 ms 2824 KB
1
03_random5.in WA 36 ms 2888 KB
1
03_random6.in WA 36 ms 2944 KB
1
04_random1.in WA 37 ms 2752 KB
1
04_random2.in WA 34 ms 2816 KB
1
04_random3.in WA 33 ms 3012 KB
1
04_random4.in WA 34 ms 2944 KB
1
04_random5.in WA 33 ms 2748 KB
1
04_random6.in WA 40 ms 2812 KB
1
05_random1.in WA 49 ms 2876 KB
1
05_random2.in WA 51 ms 2936 KB
1
05_random3.in WA 49 ms 3004 KB
1
05_random4.in WA 45 ms 2940 KB
1
06_random1.in WA 48 ms 3000 KB
1
06_random3.in TLE 1000 ms 3064 KB
1