题意:给定一个长度小于40的序列,问你那是Fib数列的哪一项的前缀。

析:首先用大数把Fib数列的前100000-1项算出来,注意,一定不能是100000,要不然会WA的,然后每个数取前40位,不足40位的全取,然后插入到字典树上,

并用一个数组标记是哪一项,最后查询的时候,如果查不到就是无解,否则输出答案。

在我电脑上跑了10秒多,交上去才1秒多。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-5;
const int maxn = 40000 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} struct BigInteger{
static const int BASE = 100000000;
static const int WIDTH = 8;
vector<int> s; BigInteger(LL num = 0){ *this = num; }
BigInteger operator = (LL num){
s.clear();
do{
s.push_back(num % BASE);
num /= BASE;
} while(num);
return *this;
}
BigInteger operator = (const string &str){
s.clear();
int x, len = (str.length() - 1) / WIDTH + 1;
for(int i = 0; i < len; ++i){
int end = str.length() - i * WIDTH;
int start = max(0, end - WIDTH);
sscanf(str.substr(start, end-start).c_str(), "%d", &x);
s.push_back(x);
}
return *this;
}
friend istream &operator >> (istream &in, BigInteger &x){
string s;
if(!(in >> s)) return in;
x = s;
return in;
} string getNum(){
string ss;
int t = s.back();
while(t) ss.push_back(t % 10 + '0'), t /= 10;
reverse(ss.begin(), ss.end());
for(int i = s.size()-2; i >= 0; --i){
char buf[20];
sprintf(buf, "%08d", s[i]);
int len = strlen(buf);
for(int j = 0; j < len; ++j) ss.push_back(buf[j]);
if(ss.size() >= 40) return ss;
}
return ss;
} friend ostream &operator << (ostream &out, const BigInteger &x){
out << x.s.back();
for(int i = x.s.size()-2; i >= 0; --i){
char buf[20];
sprintf(buf, "%08d", x.s[i]);
int len = strlen(buf);
for(int j = 0; j < len; ++j) out << buf[j];
}
return out;
} BigInteger operator + (const BigInteger &b) const{
BigInteger c;
c.s.clear();
for(int i = 0, g = 0; ; ++i){
if(!g && i >= s.size() && i >= b.s.size()) break;
int x = g;
if(i < s.size()) x += s[i];
if(i < b.s.size()) x += b.s[i];
c.s.push_back(x % BASE);
g = x / BASE;
}
return c;
}
};
const int maxnode = 40 * 100000 + 10;
const int sigam_size = 10;
struct Tire{
int ch[maxnode][sigam_size];
int val[maxnode];
int sz;
void init(){
sz = 1;
memset(ch, 0, sizeof ch);
memset(val, 0, sizeof val);
}
int idx(char c){ return c - '0'; } void Insert(const string &s, int v){
int u = 0;
for(int i = 0; i < s.size(); ++i){
int c = idx(s[i]);
if(!val[u]) val[u] = v;
if(!ch[u][c]) ch[u][c] = sz++;
u = ch[u][c];
}
if(!val[u]) val[u] = v;
} int query(const string &s){
int u = 0;
for(int i = 0; i < s.size(); ++i){
int c = idx(s[i]);
if(!ch[u][c]) return -1;
u = ch[u][c];
}
return val[u] - 1;
}
}; Tire fib; void init(){
fib.init();
BigInteger a = 1;
BigInteger b = 1;
BigInteger c = 1;
fib.Insert(a.getNum(), 1);
fib.Insert(a.getNum(), 2);
for(int i = 2; i < 100000; ++i){
c = a + b;
string s = c.getNum();
if(s.size() > 40) s = s.substr(0, 40);
fib.Insert(s, i+1);
a = b; b = c;
}
} int main(){
ios::sync_with_stdio(false);
init();
int T; cin >> T;
for(int kase = 1; kase <= T; ++kase){
string s;
cin >> s;
int ans = fib.query(s);
cout << "Case #" << kase << ": " << ans << endl;
}
return 0;
}

最新文章

  1. UMLl类图实例
  2. ruby 学习笔记 2 -变量
  3. dba诊断之IO
  4. SQL SERVER 2008 字段值合并
  5. python脚本工具 - 4 获取系统当前时间
  6. WebApi2官网学习记录---OData中的查询
  7. libevent evbuffer bug
  8. iOS动态运行时方法
  9. java输出换行的标准姿势&quot;line.separator&quot;
  10. 一类斜率优化的dp(特有性质:只能连续,不能交叉)
  11. JDBC公共动作类
  12. vsftp 详解鸟哥版
  13. linux ubuntu下如何安装并且切换java版本(Unsupported major.minor version 52.0)
  14. bzoj 1036
  15. Java基础---Java---基础加强---内省的简单运用、注解的定义与反射调用、 自定义注解及其应用、泛型及泛型的高级应用、泛型集合的综合
  16. EventBus3.0 study
  17. 内置对象Cookie和Session有何不同【常见面试题】
  18. 2018-2019-2 20165235《网络对抗技术》Exp2 后门原理与实践
  19. 修改sql数据库名称
  20. 关于Verilog中begin-end & fork-join

热门文章

  1. nice命令兼容性分析实例
  2. Java链接MySql数据库(转)
  3. 08 comet反向ajax
  4. 用 ERD 盘解决 Win8 自己主动更新后不能启动的问题
  5. js比较3个数字的大小
  6. 什么是 CAS 机制?
  7. xml 基础属性
  8. Block浅析一
  9. spring boot 整合 quartz 集群环境 实现 动态定时任务配置【原】
  10. php mcrypt加密实例