Submission #00303
ソースコード
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 | #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; 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; } long long res = 0; 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])) %= MOD; } printf ( "%lld\n%lld\n" , miNum, comb); return 0; } |
ステータス
項目 | データ |
---|---|
問題 | 0003 - repdigit |
ユーザー名 | sekiya9311 |
投稿日時 | 2017-07-07 22:35:08 |
言語 | C++17 |
状態 | Compile Error |
得点 | 0 |
ソースコード長 | 4074 Byte |
最大実行時間 | - |
最大メモリ使用量 |
コンパイルメッセージ
./Main.cpp: 関数 ‘long long int calcMinNum()’ 内: ./Main.cpp:120:6: エラー: ‘res’ was not declared in this scope (res *= powMod(10, mi.first)) %= MOD; ^~~
セット
セット | 得点 | Cases |
---|
テストケース
ファイル名 | 状態 | 実行時間 | メモリ使用量 | # |
---|