题意:如果一个数中的某一段是长度大于2的菲波那契数,那么这个数就被定义为F数,前几个F数是13,21,34,55......将这些数字进行编号,a1 = 13, a2 = 21。现给定一个数n,输出和n相差最小的数ax与n的差值的绝对值,其中下标x满足是一个菲波那契数。

分析:该题所求真是九曲十八弯,说了那么多其实要解决的问题可以转化为给定一个x,求1-x之间有多少个F数,通过二分查找能够把下标是菲波那契数的序列求出来,之后就直接for循环找到那个最相近的数就可以了。关键是如何求解1-x之间有多少个F数,容易想到的是数位dp,但是这里不太好弄,因为10^11次方之内有50多个数,每个数又有一定的长度,因此通过枚举每一数位来直接dp状态记录成了问题,而ac自动机能够解决多串匹配的问题,因此通过建立一个ac自动机(使用静态数组实现),把状态映射到ac自动机上的静态数组下标上,这样一来状态数少了不说而且能够把这个匹配过程表示出来。其他地方和一般的数位dp没什么区别了。C++编译器调用abs函数WA了,看了看头文件确实没有long long型的重载,不过G++中调用abs竟然过了,因此索性改成手动判负。最近dev也不知怎么搞的,%I64d无法读入long long啊,本地不过提交AC这种事情真忧伤,索性cin了。

#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std; typedef long long LL;
const int N = ;
LL n;
int bit[];
LL f[];
LL dp[][N]; // dp[i][j]表示剩余i位没有限制,并且已经在自动机中匹配到j位置的F数的个数
LL POW[]; // 存储10的幂
LL seq[];
LL suf[];
queue<int>q; struct Ac_Auto{
int ch[N][];
int fail[N];
bool flag[N];
int idx, root;
int newnd() {
memset(ch[idx], , sizeof (ch[idx]));
flag[idx] = false, fail[idx] = ;
return idx++;
}
void init() {
idx = , root = newnd();
}
void insert(LL x) {
int id = ;
while (x) bit[id++] = x%, x/=;
int p = root;
for (int i = id-; i >= ; --i) {
if (!ch[p][bit[i]]) ch[p][bit[i]] = newnd();
p = ch[p][bit[i]];
}
flag[p] = true;
}
void build() {
for (int i = ; i < ; ++i) {
if (ch[root][i]) {
q.push(ch[root][i]);
}
}
while (!q.empty()) {
int u = q.front();
q.pop();
for (int i = ; i < ; ++i) {
int &v = ch[u][i];
int x = fail[u];
if (v) {
q.push(v);
while (x && !ch[x][i]) x = fail[x];
fail[v] = ch[x][i];
flag[v] = flag[v] || flag[fail[v]];
} else {
v = ch[x][i]; // 直接通过引用来更改,叼啊
}
}
}
}
void find(LL x) {
int id = ;
while (x) bit[id++] = x%, x/=;
int p = root;
for (int i = id-; i >= ; --i) {
p = ch[p][bit[i]];
if (flag[p]) {
cout << "OMG" << endl;
return;
}
}
}
};
Ac_Auto ac; LL count(int p, int sta, bool bound) {
if (p == ) return ;
if (!bound && ~dp[p][sta]) return dp[p][sta];
int y = bound ? bit[p] : ;
LL sum = ;
int tsta;
for (int i = ; i <= y; ++i) {
tsta = ac.ch[sta][i];
if (ac.flag[tsta]) {
if (bound&&i==y) sum += suf[p-]+;
else sum += POW[p-];
continue;
}
sum += ::count(p-, tsta, bound&&i==y);
}
if (!bound) dp[p][sta] = sum;
return sum;
} LL cal(LL x) {
int idx = ;
while (x) suf[idx] = suf[idx-]+x%*POW[idx-], bit[idx++] = x%, x/=;
return ::count(idx-, ac.root, true);
} void prepare() {
f[] = , f[] = ;
for (int i = ; i <= ; ++i) {
f[i] = f[i-] + f[i-];
}
ac.init();
for (int i = ; i <= ; ++i) {
ac.insert(f[i]);
}
ac.build();
POW[] = ;
for (int i = ; i < ; ++i) POW[i] = POW[i-] * ;
memset(dp, 0xff, sizeof (dp));
LL tmp;
for (int i = ; i <= ; ++i) {
LL l = , r = POW[], mid;
while (l <= r) {
mid = (l + r) >> ;
if ((tmp=cal(mid)) < f[i]) l = mid + ;
else if (tmp > f[i]) r = mid - ;
else seq[i] = mid, r = mid - ;
}
}
} int main() {
prepare();
while (cin >> n, n != -) {
LL ret = 1LL << ;
for (int i = ; i <= ; ++i) {
LL t = seq[i] - n;
if (t < ) t = -t;
ret = min(ret, t);
}
cout << ret << endl;
}
return ;
}

HDU-4518 吉哥系列故事——最终数 AC自动机+数位DP的更多相关文章

  1. 吉哥系列故事——恨7不成妻(数位DP)

    吉哥系列故事——恨7不成妻 http://acm.hdu.edu.cn/showproblem.php?pid=4507 Time Limit: 1000/500 MS (Java/Others)   ...

  2. hdu4507吉哥系列故事——恨7不成妻 (数位dp)

    Problem Description 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: ...

  3. hdu-4507 吉哥系列故事——恨7不成妻 数位DP 状态转移分析/极限取模

    http://acm.hdu.edu.cn/showproblem.php?pid=4507 求[L,R]中不满足任意条件的数的平方和mod 1e9+7. 条件: 1.整数中某一位是7:2.整数的每一 ...

  4. HDU-4507 吉哥系列故事——恨7不成妻 数位DP

    题意:给定区间[L, R]求区间内与7无关数的平方和.一个数当满足三个规则之一则认为与7有关:1.整数中某一位是7:2.整数的每一位加起来的和是7的整数倍:3.这个整数是7的整数倍: 分析:初看起来确 ...

  5. 【hdu4507】吉哥系列故事——恨7不成妻 数位dp

    题目描述 求 $[L,R]$ 内满足:数位中不包含7.数位之和不是7的倍数.本身不是7的倍数 的所有数的平方和 mod $10^9+7$ . 输入 输入数据的第一行是case数T(1 <= T ...

  6. hdu4507 吉哥系列故事——恨7不成妻[数位DP]

    这题面什么垃圾玩意儿 首先看到问题格式想到数位DP,但是求的是平方和.尝试用数位DP推出. 先尝试拼出和.设$f[len][sum][mod]$表示填到$len$位,已填位置数位和$sum$,数字取余 ...

  7. hdu 4512 吉哥系列故事——完美队形I【LCIS经典应用】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=4512 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  8. HDU 4513 吉哥系列故事――完美队形II(Manacher)

    题目链接:cid=70325#problem/V">[kuangbin带你飞]专题十六 KMP & 扩展KMP & Manacher V - 吉哥系列故事――完美队形I ...

  9. HDU 4513 吉哥系列故事――完美队形II

    http://acm.hdu.edu.cn/showproblem.php?pid=4513 吉哥系列故事——完美队形II Time Limit: 3000/1000 MS (Java/Others) ...

随机推荐

  1. 不错的linux下通用的java程序启动脚本(转载)

    转自:http://www.cnblogs.com/langtianya/p/4164151.html 虽然写起动shell的频率非常不高...但是每次要写都要对付一大堆的jar文件路径,新加jar包 ...

  2. Mootools插件-闪烁的标题

    转自:http://www.cnblogs.com/see7di/archive/2012/10/09/2716024.html 回想起来,我已经好久没有写点啥了,尤其是关于Mootools方面的东西 ...

  3. BZOJ 1588:营业额统计(Splay)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1588 题意:中文题意. 思路:每一个点每一个点插入Splay,然后插入新的一个点之后,查这个节点的前 ...

  4. js比typeof更准确的验证类型方法

    var type = function (o){ var s = Object.prototype.toString.call(o); return s.match(/\[object (.*?)\] ...

  5. iOS开发系列之 itms-services 协议

    通过 itms-services 协议,发布或者分享 iOS 应用程序   通过 itms-services 协议,发布或者分享 iOS 应用程序 <button onclick="w ...

  6. mysql之使用xtrabackup进行物理备份、恢复、在线克隆从库、在线重做主从

    注:图片来自<深入浅出MySQL 数据库开发 优化与管理维护 第2版> 物理备份和恢复 1.冷备份:停掉mysql再备份,一般很少用,因为很多应用不允许长时间停机,停机备份的可以直接CP数 ...

  7. HDU 1715 大菲波数

    大菲波数 问题描述 : Fibonacci数列,定义如下: f(1)=f(2)=1 f(n)=f(n-1)+f(n-2)  n>=3. 计算第n项Fibonacci数值. 输入: 输入第一行为一 ...

  8. 【转】Program Files (x86)文件夹是干什么的

    转载地址:http://leonax.net/p/2373/what-program-files-x86-folder-for/ 今天在微博上看到有人问这个问题.在装好64位的Windows之后,能在 ...

  9. 【转】Tomcat中部署java web应用程序

    http://www.blogjava.net/jiafang83/archive/2009/06/02/279644.html 转载:今天给大家介绍怎样在Tomcat5.5.9中部署Java Web ...

  10. ASp.net 注册

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs ...