Submission #20736872
Source Code Expand
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
//#pragma GCC optimize ("-O3")
using namespace std;
void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60;
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//---------------------------------------------------------------------------------------------------
template<int MOD> struct ModInt {
static const int Mod = MOD; unsigned x; ModInt() : x(0) { }
ModInt(signed sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
ModInt(signed long long sig) { x = sig < 0 ? sig % MOD + MOD : sig % MOD; }
int get() const { return (int)x; }
ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }
ModInt &operator/=(ModInt that) { return *this *= that.inverse(); }
ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
ModInt operator/(ModInt that) const { return ModInt(*this) /= that; }
ModInt inverse() const { long long a = x, b = MOD, u = 1, v = 0;
while (b) { long long t = a / b; a -= t * b; std::swap(a, b); u -= t * v; std::swap(u, v); }
return ModInt(u); }
bool operator==(ModInt that) const { return x == that.x; }
bool operator!=(ModInt that) const { return x != that.x; }
ModInt operator-() const { ModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
template<int MOD> ostream& operator<<(ostream& st, const ModInt<MOD> a) { st << a.get(); return st; };
template<int MOD> ModInt<MOD> operator^(ModInt<MOD> a, unsigned long long k) {
ModInt<MOD> r = 1; while (k) { if (k & 1) r *= a; a *= a; k >>= 1; } return r; }
typedef ModInt<1000000007> mint;
/*---------------------------------------------------------------------------------------------------
∧_∧
∧_∧ (´<_` ) Welcome to My Coding Space!
( ´_ゝ`) / ⌒i @hamayanhamayan0
/ \ | |
/ / ̄ ̄ ̄ ̄/ |
__(__ニつ/ _/ .| .|____
\/____/ (u ⊃
---------------------------------------------------------------------------------------------------*/
string N; int K;
map<char, int> dic;
mint dp[201010][18][2][2]; // dp[digit][kind][isless][leadingzero]
//---------------------------------------------------------------------------------------------------
mint solve() {
bitset<16> used;
int len = N.length();
dp[0][0][0][1] = 1;
rep(digit, 0, len) {
int x = dic[N[digit]];
rep(kind, 0, 17) rep(isless, 0, 2) rep(leadingzero, 0, 2) {
mint& cur = dp[digit][kind][isless][leadingzero];
if (isless) {
// 既にNより小さい場合において、既に選択された数字を選ぶ場合の遷移
dp[digit + 1][kind][isless][leadingzero] += cur * kind;
if (!leadingzero) {
// 既にNより小さい場合、かつ、leadingzeroじゃない場合において、新しい数を選ぶ場合
dp[digit + 1][kind + 1][isless][leadingzero] += cur * (16 - kind);
}
else {
// leadingzeroの場合は種類数をカウントしない(leadingzeroは種類にカウントしない)ためkind=0であるはず
// 既にNより小さい場合、かつ、leadingzeroである場合において、0を選択する場合(leadingzeroを継続する場合)
dp[digit + 1][kind][isless][1] += cur;
// 既にNより小さい場合、かつ、leadingzeroである場合において、0以外を選択する場合
dp[digit + 1][kind + 1][isless][0] += cur * (16 - kind - 1);
}
}
else {
// まだNより小さくない(Nと等しい)場合は、小さくなるように数を選択する
rep(nxt, 0, x) {
// 先頭で0を選ぶ場合のみleading-zeroになるので、そちらに遷移をさせる
if (digit == 0 && nxt == 0) dp[digit + 1][0][1][1] += cur;
else {
// それ以外の場合は、種類数が増えるかを確認して、遷移を行う
bitset<16> used2 = used;
used2.set(nxt);
dp[digit + 1][used2.count()][1][0] += cur;
}
}
}
}
// こちらの遷移は先頭digit桁がNと同じになるように遷移させるもの
int pre = used.count();
used.set(x);
int post = used.count();
if (digit == 0) dp[digit + 1][post][0][0] += dp[digit][pre][0][1];
else dp[digit + 1][post][0][0] += dp[digit][pre][0][0];
}
mint ans = dp[len][K][0][0]; // Nと全く同じパターン。K種類であればここで計算される
ans += dp[len][K][1][0]; // N未満でK種類のもの
return ans;
}
//---------------------------------------------------------------------------------------------------
void _main() {
rep(i, 0, 10) dic[char('0' + i)] = i;
rep(i, 0, 6) dic[char('A' + i)] = 10 + i;
cin >> N >> K;
cout << solve() << endl;
}
Submission Info
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
600 / 600 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt |
| All |
handmade_00.txt, handmade_01.txt, handmade_02.txt, handmade_03.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt |
| Case Name |
Status |
Exec Time |
Memory |
| handmade_00.txt |
AC |
50 ms |
60152 KiB |
| handmade_01.txt |
AC |
46 ms |
60120 KiB |
| handmade_02.txt |
AC |
39 ms |
60116 KiB |
| handmade_03.txt |
AC |
40 ms |
60188 KiB |
| random_00.txt |
AC |
321 ms |
60324 KiB |
| random_01.txt |
AC |
321 ms |
60120 KiB |
| random_02.txt |
AC |
317 ms |
60324 KiB |
| random_03.txt |
AC |
320 ms |
60284 KiB |
| random_04.txt |
AC |
320 ms |
60268 KiB |
| random_05.txt |
AC |
318 ms |
60176 KiB |
| random_06.txt |
AC |
320 ms |
60216 KiB |
| random_07.txt |
AC |
318 ms |
60324 KiB |
| random_08.txt |
AC |
318 ms |
60280 KiB |
| random_09.txt |
AC |
320 ms |
60312 KiB |
| random_10.txt |
AC |
318 ms |
60268 KiB |
| random_11.txt |
AC |
317 ms |
60312 KiB |
| random_12.txt |
AC |
94 ms |
60248 KiB |
| random_13.txt |
AC |
167 ms |
60196 KiB |
| random_14.txt |
AC |
239 ms |
60232 KiB |
| random_15.txt |
AC |
303 ms |
60172 KiB |
| random_16.txt |
AC |
103 ms |
60256 KiB |
| random_17.txt |
AC |
129 ms |
60164 KiB |
| random_18.txt |
AC |
241 ms |
60232 KiB |
| random_19.txt |
AC |
268 ms |
60160 KiB |
| random_20.txt |
AC |
51 ms |
60124 KiB |
| random_21.txt |
AC |
171 ms |
60308 KiB |
| random_22.txt |
AC |
194 ms |
60216 KiB |
| random_23.txt |
AC |
313 ms |
60272 KiB |
| sample_01.txt |
AC |
48 ms |
60044 KiB |
| sample_02.txt |
AC |
40 ms |
60104 KiB |
| sample_03.txt |
AC |
42 ms |
60100 KiB |
| sample_04.txt |
AC |
45 ms |
60104 KiB |
| sample_05.txt |
AC |
39 ms |
60180 KiB |