結果

問題 No.1269 I hate Fibonacci Number
ユーザー milanis48663220
提出日時 2022-01-10 17:01:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 409 ms / 3,000 ms
コード長 7,156 bytes
コンパイル時間 2,143 ms
コンパイル使用メモリ 150,536 KB
最終ジャッジ日時 2025-01-27 10:17:59
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
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; }
using namespace std;
typedef long long ll;
template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
return vector<vector<T>>(n, vector<T>(m, v));
}
template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}
template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
if(v.empty()) {
cout << endl;
return;
}
for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
cout << v.back() << endl;
}
const ll MOD = 1000000007;
class ModInt{
public:
ll v;
ModInt(ll _v = 0){
if(_v < 0) _v = (_v%MOD)+MOD;
if(_v >= MOD) _v %= MOD;
v = _v;
}
ModInt operator+(ll n){
return ModInt((v+n)%MOD);
}
ModInt operator-(ll n){
return ModInt((v-n+MOD)%MOD);
}
ModInt operator*(ll n){
if(n >= MOD) n %= MOD;
return ModInt((v*n)%MOD);
}
ModInt operator/(ll n){
return ModInt((ModInt(n).inv()*v).v%MOD);
}
ModInt &operator+=(ll n){
v = (v+n)%MOD;
return *this;
}
ModInt &operator-=(ll n){
v = (v-n+MOD)%MOD;
return *this;
}
ModInt &operator*=(ll n){
v = (v*n+MOD)%MOD;
return *this;
}
ModInt operator+(ModInt n){
return ModInt((v+n.v)%MOD);
}
ModInt operator-(ModInt n){
return ModInt((v-n.v+MOD)%MOD);
}
ModInt operator*(ModInt n){
return ModInt((v*n.v)%MOD);
}
ModInt operator/(ModInt n){
return ModInt((n.inv()*v).v%MOD);
}
ModInt &operator+=(ModInt n){
v = (v+n.v)%MOD;
return *this;
}
ModInt &operator-=(ModInt n){
v = (v-n.v+MOD)%MOD;
return *this;
}
ModInt &operator*=(ModInt n){
v = (v*n.v)%MOD;
return *this;
}
bool operator==(ModInt n){
return v == n.v;
}
bool operator!=(ModInt n){
return v != n.v;
}
ModInt &operator=(ll n){
v = n%MOD;
return *this;
}
ModInt inv(){
if(v == 1) return ModInt(1);
else return ModInt(MOD-ModInt(MOD%v).inv().v*(MOD/v)%MOD);
}
};
ostream& operator<<(ostream& os, const ModInt& m){
os << m.v;
return os;
}
istream & operator >> (istream &in, ModInt &m){
in >> m.v;
return in;
}
ModInt pow(ModInt a, ll n) {
assert(n >= 0);
ModInt ans = 1;
while (n > 0) {
if (n&1) ans = ans*a;
a = a*a;
n >>= 1;
}
return ans;
}
using mint = ModInt;
class AhoCorasick {
public:
struct Edge{
int to;
char c;
Edge(int to, char c): to(to), c(c) { }
};
vector<string> key_words;
int n_nodes;
AhoCorasick(vector<string> key_words): key_words(key_words) {
n_nodes = 1;
output = {{}};
tree = {{}};
for(int i = 0; i < key_words.size(); i++) {
add(i);
}
// build failure and output
failure.resize(n_nodes);
failure[0] = 0;
queue<int> que;
for(Edge e: tree[0]){
que.push(e.to);
failure[e.to] = 0;
}
while(!que.empty()){
int v = que.front(); que.pop();
for(Edge e: tree[v]){
que.push(e.to);
int u = failure[v];
while(find_node(u, e.c) == -1 && u != 0) {
u = failure[u];
}
failure[e.to] = find_node(u, e.c);
if(u == 0 && failure[e.to] == -1){
failure[e.to] = 0;
}
for(int i: output[failure[e.to]]) output[e.to].insert(i);
}
}
}
vector<int> search_text(string text){
int cur = 0;
vector<int> ans(key_words.size());
for(char c: text){
while(true){
int nx = find_node(cur, c);
if(cur == 0 && nx == -1){
nx = 0;
}
if(nx != -1) {
cur = nx;
break;
}
cur = failure[cur];
}
for(int i: output[cur]) ans[i]++;
}
return ans;
}
vector<vector<Edge>> tree;
vector<int> failure;
vector<set<int>> output;
void add(int idx){
int cur = 0;
for(char c: key_words[idx]){
int nx = find_node(cur, c);
if(nx == -1){
nx = add_node(cur, c);
}
cur = nx;
}
output[cur].insert(idx);
}
int find_node(int i, char c){
for(Edge e: tree[i]){
if(e.c == c) return e.to;
}
return -1;
}
int add_node(int from, char c){
int to = n_nodes;
tree[from].push_back(Edge(to, c));
n_nodes++;
output.push_back({});
tree.push_back({});
return to;
}
};
vector<ll> list_fib(ll l, ll r){
vector<ll> fib = {1, 2};
for(int i = 2;;i++){
ll x = fib[i-1]+fib[i-2];
if(x > r) break;
fib.push_back(x);
}
vector<ll> ans;
for(ll x: fib){
if(l <= x && x <= r) ans.push_back(x);
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
int n; ll l, r; cin >> n >> l >> r;
auto fib = list_fib(l, r);
vector<string> key_words;
for(ll x: fib) key_words.push_back(to_string(x));
auto ac = AhoCorasick(key_words);
auto dp = vec2d(n+1, ac.n_nodes, mint(0));
dp[0][0] = 1;
vector<bool> ok(ac.n_nodes, true);
for(int i = 0; i < ac.n_nodes; i++){
if(!ac.output[i].empty()) ok[i] = false;
}
for(int i = 0; i < n; i++){
for(int j = 0; j < ac.n_nodes; j++){
if(!ok[j]) continue;
for(int k = 0; k < 10; k++){
int cur = j;
char c = '0'+k;
while(true){
int nx = ac.find_node(cur, c);
if(cur == 0 && nx == -1){
nx = 0;
}
if(nx != -1) {
cur = nx;
break;
}
cur = ac.failure[cur];
}
if(!ok[cur]) continue;
dp[i+1][cur] += dp[i][j];
}
}
}
cout << accumulate(dp[n].begin(), dp[n].end(), mint(0))-1 << endl;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0