ソースコード
#if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>
auto& in = std::cin;
auto& out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;
template<typename T>
void fill_all(T& arr, const T& v) {
arr = v;
}
template<typename ARR, typename U>
void fill_all(ARR& arr, const U& v) {
for (auto& i : arr) { fill_all(i, v); }
}
int64_t N, K;
int64_t dp[1001][1001];
int64_t func(int i, int32_t wa)
{
if (i == 0) {
return (wa==0)?1:0;
}
if (dp[i][wa] != -1) {
return dp[i][wa];
}
int64_t res = 0;
for (int32_t num = 0; num <= 9; num++)
{
if (wa < num) {
break;
}
res += func(i - 1, wa - num);
}
if (res > 10000000000) {
res = 10000000000;
}
return dp[i][wa] = res;
}
int main()
{
using std::endl;
in.sync_with_stdio(false);
out.sync_with_stdio(false);
in.tie(nullptr);
out.tie(nullptr);
fill_all(dp, (int64_t)-1);
in >> N>>K;
int32_t dig = 1;
for (; dig < 1010; dig++)
{
auto next = func(dig, N);
if (next < K) {
}
else {
break;
}
}
K -= func(dig - 1, N);
for (int32_t i = 0; i < dig; i++)
{
for (int32_t num = 0; num <= 9; num++)
{
if (i == 0 && num==0) {
continue;
}
auto next = func(dig-i-1, N-num);
if (next < K) {
K -= next;
}
else {
out << num;
N -= num;
break;
}
}
}
out << endl;
return 0;
}
#endif
#if 0
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>
auto& in = std::cin;
auto& out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;
class Trie
{
private:
int8_t just = 0;
uint16_t child_num = 0;
std::unique_ptr<Trie> childlen['z'-'a'+1];
public:
void insert(const char* str) {
++child_num;
if (*str == '\0') { ++just; return; }
if (childlen[*str - 'a'] == nullptr) { childlen[*str - 'a'] = std::make_unique<Trie>(); }
childlen[*str - 'a']->insert(str + 1);
}
int32_t count(const char* str)const {
if (*str == '\0') { return just; }
if (*str == '*') { return child_num; }
if (*str == '?') {
int32_t res = 0;
for (auto& i : childlen)
{
if(i){ res += i->count(str + 1); }
}
return res;
}
if (childlen[*str - 'a']) {
return childlen[*str - 'a']->count(str + 1);
}
else {
return 0;
}
}
};
int32_t N, M;
std::string buf;
int main()
{
using std::endl;
in.sync_with_stdio(false);
out.sync_with_stdio(false);
in.tie(nullptr);
out.tie(nullptr);
in >> N>>M;
Trie trie;
Trie trie_rev;
buf.reserve(256);
for (size_t i = 0; i < N; i++)
{
in >> buf;
trie.insert(buf.c_str());
std::reverse(buf.begin(), buf.end());
trie_rev.insert(buf.c_str());
}
for (size_t i = 0; i < M; i++)
{
in >> buf;
if (buf[0] == '*') {
std::reverse(buf.begin(), buf.end());
out << trie_rev.count(buf.c_str()) << endl;
}
else {
out << trie.count(buf.c_str()) << endl;
}
}
return 0;
}
#endif