結果

問題 No.1269 I hate Fibonacci Number
ユーザー 👑 jupirojupiro
提出日時 2020-10-30 16:46:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 19 ms / 3,000 ms
コード長 5,971 bytes
コンパイル時間 1,818 ms
コンパイル使用メモリ 152,012 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-29 04:22:52
合計ジャッジ時間 3,425 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 17 ms
4,376 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 18 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 19 ms
4,380 KB
testcase_19 AC 14 ms
4,376 KB
testcase_20 AC 5 ms
4,376 KB
testcase_21 AC 11 ms
4,376 KB
testcase_22 AC 11 ms
4,376 KB
testcase_23 AC 11 ms
4,376 KB
testcase_24 AC 6 ms
4,376 KB
testcase_25 AC 8 ms
4,376 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 8 ms
4,380 KB
testcase_30 AC 4 ms
4,376 KB
testcase_31 AC 14 ms
4,376 KB
testcase_32 AC 8 ms
4,380 KB
testcase_33 AC 6 ms
4,380 KB
testcase_34 AC 2 ms
4,376 KB
testcase_35 AC 4 ms
4,380 KB
testcase_36 AC 2 ms
4,376 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <sstream>
#include <stack>
#include <algorithm>
#include <cmath>
#include <queue>
#include <bitset>
#include <iomanip>
#include <limits>
#include <chrono>
#include <random>
#include <array>
#include <unordered_map>
#include <functional>
#include <complex>
#include <numeric>
#include <cctype>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <tuple>
#include <assert.h>
#include <deque>
#include <utility>
#include <fstream>

using namespace std;
typedef long long ll;
using ull = unsigned long long;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<typename T> T gcd(T a, T b) { a = abs(a), b = abs(b); while (b > 0) { tie(a, b) = make_pair(b, a % b); } return a; }
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());

constexpr long long INF = 1LL << 60;
constexpr int inf = 1000000007;
constexpr long long mod = 1000000007LL;
//constexpr long long mod = 998244353;
constexpr int MAX = 1100000;


template<int char_size>
struct TrieNode {
	array<int, char_size> nxt;
	int cnt;
	vector<int> accept;
	TrieNode() :cnt(0) { fill(nxt.begin(), nxt.end(), -1); }
};

template<int char_size>
struct Trie {
	using Node = TrieNode<char_size>;
	vector<Node> nodes;
	int root;

	Trie() :root(0) { nodes.emplace_back(Node()); }
	void add(const vector<int>& str, int id) {
		int node_index = 0;
		for (auto& c : str) {
			if (nodes[node_index].nxt[c] == -1) {
				nodes[node_index].nxt[c] = (int)nodes.size();
				nodes.emplace_back(Node());
			}
			nodes[node_index].cnt += 1;
			node_index = nodes[node_index].nxt[c];
		}
		nodes[node_index].cnt += 1;
		nodes[node_index].accept.emplace_back(id);
	}
	void add(const vector<int>& str) { add(str, nodes[0].cnt); }

	int find(const vector<int>& str) {
		int node_index = 0;
		for (auto& c : str) {
			if (nodes[node_index].nxt[c] == -1) return -1;
			node_index = nodes[node_index].nxt[c];
		}
		return node_index;
	}
	int count() const { return nodes[0].cnt; }
	int size() const { return nodes.size(); }
};

template<int char_size>
struct AhoCorasick : Trie<char_size + 1> {
	using Trie<char_size + 1>::Trie;
	const int FAIL = char_size;
	vector<int> correct;
	//各文字列についての回数が知りたいならheavy=true, 全体の和だけでいいならheavy=false;
	void build(bool heavy) {
		correct.resize(this->size());
		for (int i = 0; i < correct.size(); i++) correct[i] = (int)this->nodes[i].accept.size();
		queue<int> q;
		for (int i = 0; i <= char_size; i++) {
			if (this->nodes[0].nxt[i] >= 0) {
				this->nodes[this->nodes[0].nxt[i]].nxt[FAIL] = 0;
				q.emplace(this->nodes[0].nxt[i]);
			}
			else this->nodes[0].nxt[i] = 0;
		}
		while (!q.empty()) {
			auto& cur = this->nodes[q.front()];
			int fail = cur.nxt[FAIL];
			correct[q.front()] += correct[fail];
			q.pop();
			for (int i = 0; i < char_size; i++) {
				if (cur.nxt[i] >= 0) {
					this->nodes[cur.nxt[i]].nxt[FAIL] = this->nodes[fail].nxt[i];
					if (heavy) {
						auto& u = this->nodes[cur.nxt[i]].accept;
						auto& v = this->nodes[this->nodes[fail].nxt[i]].accept;
						vector<int> accept; accept.reserve(u.size() + v.size());
						set_union(u.begin(), u.end(), v.begin(), v.end(), back_inserter(accept));
						u = accept;
					}
					q.emplace(cur.nxt[i]);
				}
				else cur.nxt[i] = this->nodes[fail].nxt[i];
			}
		}
	}

	vector<int> match(vector<int>& str, bool heavy) {
		vector<int> ret(heavy ? this->size() : 1);
		int cur = 0;
		for (auto& c : str) {
			cur = this->nodes[cur].nxt[c];
			if (heavy) for (auto& v : this->nodes[cur].accept) ret[v] += 1;
			else ret[0] += correct[cur];
		}
		return ret;
	}

	pair<int, ll> move(int c, int cur = 0) {
		int nxt = this->nodes[cur].nxt[c];
		return { nxt, correct[nxt] };
	}
};

struct mint {
	long long x;
	mint(long long x = 0) :x((x% mod + mod) % mod) {}
	mint& operator+=(const mint a) {
		if ((x += a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator-=(const mint a) {
		if ((x += mod - a.x) >= mod) x -= mod;
		return *this;
	}
	mint& operator*=(const mint a) {
		(x *= a.x) %= mod;
		return *this;
	}
	mint operator+(const mint a) const {
		mint res(*this);
		return res += a;
	}
	mint operator-(const mint a) const {
		mint res(*this);
		return res -= a;
	}
	mint operator*(const mint a) const {
		mint res(*this);
		return res *= a;
	}
	mint pow(ll t) const {
		if (!t) return 1;
		mint a = pow(t >> 1);
		a *= a;
		if (t & 1) a *= *this;
		return a;
	}

	// for prime mod
	mint inv() const {
		return pow(mod - 2);
	}
	mint& operator/=(const mint a) {
		return (*this) *= a.inv();
	}
	mint operator/(const mint a) const {
		mint res(*this);
		return res /= a;
	}
};
int main()
{

	cin.tie(nullptr);
	ios::sync_with_stdio(false);

	ll n, L, R; cin >> n >> L >> R;
	vector<ll> fibo(88); fibo[0] = fibo[1] = 1;
	for (int i = 2; i <= 87; i++) fibo[i] = fibo[i - 1] + fibo[i - 2];
	auto enc = [&](ll x)->vector<int> {
		if (x == 0) {
			vector<int> ret = { 0 };
			return ret;
		}
		vector<int> ret;
		while (x > 0) {
			ret.emplace_back(x % 10);
			x /= 10;
		}
		reverse(ret.begin(), ret.end());
		return ret;
	};
	AhoCorasick<10> aho;
	for (int i = 1; i <= 87; i++) {
		if (L <= fibo[i] and fibo[i] <= R) {
			auto v = enc(fibo[i]);
			aho.add(v);
		}
	}
	aho.build(false);
	vector dp(2, vector<mint>(aho.size()));
	int cur = 0;
	int nxt = 1;
	dp[cur][0] = 1;
	auto init = [&](int idx) -> void{
		fill(dp[idx].begin(), dp[idx].end(), 0);
	};
	for (int i = 0; i < n; i++) {
		init(nxt);
		for (int j = 0; j < aho.size(); j++) {
			if (dp[cur][j].x == 0) continue;
			for (int k = 0; k < 10; k++) {
				auto [nj, sum] = aho.move(k, j);
				if (sum > 0) continue;
				dp[nxt][nj] += dp[cur][j];
			}
		}
		swap(cur, nxt);
	}
	mint res = 0; for (int j = 0; j < aho.size(); j++) res += dp[cur][j];
	res -= 1;
	cout << res.x << "\n";
}
0