UVa 12333 Revenge of Fibonacci (字典树+大数)
题意:给定一个长度小于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;
}
UVa 12333 Revenge of Fibonacci (字典树+大数)的更多相关文章
- hdu 4099 Revenge of Fibonacci 字典树+大数
将斐波那契的前100000个,每个的前40位都插入到字典树里(其他位数删掉),然后直接查询字典树就行. 此题坑点在于 1.字典树的深度不能太大,事实上,超过40在hdu就会MLE…… 2.若大数加法时 ...
- UVA - 12333 Revenge of Fibonacci 高精度加法 + 字典树
题目:给定一个长度为40的数字,问其是否在前100000项fibonacci数的前缀 因为是前缀,容易想到字典树,同时因为数字的长度只有40,所以我们只要把fib数的前40位加入字典树即可.这里主要讨 ...
- UVa 12333 - Revenge of Fibonacci manweifc(模拟加法竖式 & 字典树)
题意: 给定n个(n<=40)数字, 求100000个以内有没有前面n个数字符合给定的数字的fibonacci项, 如果有, 给出最小的fibonacci项, 如果没有, 输出-1. 分析: 可 ...
- UVA 12333 Revenge of Fibonacci
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA - 12333 Revenge of Fibonacci (大数 字典树)
The well-known Fibonacci sequence is defined as following: F(0) = F(1) = 1 F(n) = F(n − 1) + F(n − 2 ...
- hdu4099 Revenge of Fibonacci 字典树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4099 思想很容易想到 就是预处理出前10w个的fib数,然后建树查询 建树时只用前40位即可,所以在计 ...
- UVA - 12333 字典树+大数
思路:用字典树将前40个数字记录下来,模拟大数加法即可. AC代码 #include <cstdio> #include <cmath> #include <algori ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- UVA 11488 Hyper Prefix Sets (字典树)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
随机推荐
- HDU 1418 抱歉 (欧拉公式)
[题目链接]:pid=1418">click here~~ [题目大意]: 假设平面上有n个点,而且每一个点至少有2条曲线段和它相连,就是说,每条曲线都是封闭的.同一时候,我们规定: ...
- Java UUID 生成(转载)
来自:http://www.cnblogs.com/jdonson/archive/2009/07/22/1528466.html 基本原理:GUID是一个128位长的数字,一般用16进制表示.算法的 ...
- 关于TCP通信程序中数据的传递格式
前言 在之前的回射程序中,实现了字符串的传递与回射.幸运的是,字符串的传递不用担心不同计算机类型的大小端匹配问题,然而,如果传递二进制数据,这就是一个要好好考虑的问题.在客户端和服务器使用不同的字节序 ...
- UIAlertController Changes in iOS 8
本文转载至 http://www.th7.cn/Program/IOS/201409/276000.shtml As part of the theme of iOS 8 to make inte ...
- 九度OJ 1136:Number Steps(步数) (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:691 解决:412 题目描述: Starting from point (0,0) on a plane, we have written ...
- ABAP 取字段的简短描述
取字段的简短描述: 取字段描述 DATA inttab LIKE STANDARD TABLE OF dfies WITH HEADER LINE. CALL FUNCTION 'DDIF_FIELD ...
- assign,copy,strong,weak,nonatomic的具体理解
例子: NSString *houseOfMM = [[NSString alloc] initWithString:'MM的三室两厅']; 上面一段代码会执行以下两个动作: 1 在堆上分配一段内存 ...
- flex平分测试
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 网络抓取功能实现 将获取的结果进行过滤并写入到TXT文档中
下面是自己编写的 网络抓取功能实现 将获取的结果进行过滤并写入到TXT文档中 (以防忘记) 原创哟 import java.io.BufferedReader;import java.io.Buffe ...
- python学习笔记:第一天
1.经典程序测试:hello world 入门编程语言第一件事,先写hello world. #!/usr/bin/env python# -*- coding: UTF-8 -*- print(&q ...