HDU 3117 Fibonacci Numbers 数学
http://acm.hdu.edu.cn/showproblem.php?pid=3117
fib是有一个数学公式的。
这里的是标准的fib公式
那么fib = 1 / sqrt(5) * ((1 + sqrt(5) / 2) ^ n - ((1 - sqrt(5)) / 2)^n) = 1 / sqrt(5) * (A^n - B^n)
那么,求后4位可以直接矩阵快速幂。
不能用上面公式的快速幂取模,因为存在精度误差。
然后求前4位的话,就是一个套路公式了。
在上一篇博客。http://www.cnblogs.com/liuweimingcprogram/p/6583154.html
这里因为后面的B很小,所以可以忽略。如果不忽略的话,就没办法做了。
比如你取A得前4位,减去B的前4位,那肯定错。比如123456789的前4位是1234,而1234的前4位又是1234,那么相减就等于0了。不过这里B的小数,前4位也是0.
然后还要除以sqrt(5),这个时候就需要你保留前5位,除以sqrt(5)后,再保留4位。
不然的话,比如你取了前4位是1234,除以sqrt(5),只剩下3位了。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = ;
struct Matrix {
LL a[maxn][maxn];
int row;
int col;
};
struct Matrix matrix_mul (struct Matrix a, struct Matrix b, int MOD) { //求解矩阵a*b%MOD
struct Matrix c = {}; //这个要多次用到,栈分配问题,maxn不能开太大,
//LL的时候更加是,空间是maxn*maxn的,这样时间用得很多,4和5相差300ms
c.row = a.row; //行等于第一个矩阵的行
c.col = b.col; //列等于第二个矩阵的列
for (int i = ; i <= a.row; i++) { //枚举第一个矩阵的行
for (int j = ; j <= b.col; j++) { //枚举第二个矩阵的列,其实和上面数值一样
for (int k = ; k <= b.row; k++) { //b中的一列中,有“行”个元素 notice
c.a[i][j] += a.a[i][k] * b.a[k][j]; //这里不及时取模,又有可能错!HDU 4565
}
c.a[i][j] = (c.a[i][j] + MOD) % MOD; //如果怕出现了负数取模的话。可以这样做
}
}
return c;
}
struct Matrix quick_matrix_pow(struct Matrix ans, struct Matrix base, int n, int MOD) {
//求解a*b^n%MOD
while (n) {
if (n & ) {
ans = matrix_mul(ans, base, MOD);//传数组不能乱传,不满足交换律
}
n >>= ;
base = matrix_mul(base, base, MOD);
}
return ans;
}
LL fib[];
void init() {
fib[] = ;
fib[] = ;
for (int i = ; i <= ; ++i) {
fib[i] = fib[i - ] + fib[i - ];
}
}
int n;
int calc(double a, LL b, int k) { //a^b的前k + 1位
double res = b * log10(a * 1.0) - (LL)(b * log10(a * 1.0)); //获得小数部分
return (int)(pow(10.0, k + res) / sqrt(5.0));
} int getMost() {
double a = ( + sqrt(5.0)) / ;
int res = calc(a, n, );
while (res >= ) res /= ;
return res;
}
int getLow() {
struct Matrix t1 = {};
t1.row = , t1.col = ;
t1.a[][] = , t1.a[][] = ; struct Matrix t2 = {};
t2.row = t2.col = ;
t2.a[][] = t2.a[][] = ;
t2.a[][] = , t2.a[][] = ; struct Matrix res = quick_matrix_pow(t1, t2, n - , );
return res.a[][];
}
void work() {
if (n <= ) {
cout << fib[n] << endl;
return;
}
int most = getMost();
int low = getLow();
printf("%04d...%04d\n", most, low);
}
int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
init();
while (cin >> n) work();
return ;
}
HDU 3117 Fibonacci Numbers 数学的更多相关文章
- HDU 3117 Fibonacci Numbers(围绕四个租赁斐波那契,通过计++乘坐高速动力矩阵)
HDU 3117 Fibonacci Numbers(斐波那契前后四位,打表+取对+矩阵高速幂) ACM 题目地址:HDU 3117 Fibonacci Numbers 题意: 求第n个斐波那契数的 ...
- hdu 3117 Fibonacci Numbers 矩阵快速幂+公式
斐波那契数列后四位可以用快速幂取模(模10000)算出.前四位要用公式推 HDU 3117 Fibonacci Numbers(矩阵快速幂+公式) f(n)=(((1+√5)/2)^n+((1-√5) ...
- HDU 3117 Fibonacci Numbers(矩阵)
Fibonacci Numbers [题目链接]Fibonacci Numbers [题目类型]矩阵 &题解: 后4位是矩阵快速幂求,前4位是用log加Fibonacci通项公式求,详见上一篇 ...
- HDU 3117 Fibonacci Numbers( 矩阵快速幂 + 数学推导 )
链接:传送门 题意:给一个 n ,输出 Fibonacci 数列第 n 项,如果第 n 项的位数 >= 8 位则按照 前4位 + ... + 后4位的格式输出 思路: n < 40时位数不 ...
- hdu 3117 Fibonacci Numbers
这道题其实也是水题来的,求Fibonacci数的前4位和后4位,在n==40这里分界开.后4位不难求,因为n达到了10^18的规模,所以只能用矩阵快速幂来求了,但在输出后4位的时候一定要注意前导0的处 ...
- UVA 11582 Colossal Fibonacci Numbers(数学)
Colossal Fibonacci Numbers 想先说下最近的状态吧,已经考完试了,这个暑假也应该是最后刷题的暑假了,打完今年acm就应该会退了,但是还什么都不会呢? +_+ 所以这个暑假,一定 ...
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
- [HDU3117]Fibonacci Numbers
题目:Fibonacci Numbers 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3117 分析: 1)后四位可以用矩阵快速幂解决.$T= \left ...
- hdu Interesting Fibonacci
Interesting Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- html5--4-2 video元素的属性
html5--4-2 video元素的属性 学习要点 掌握video元素的基本用法 直到现在,在网页中的大多数视频是通过插件(比如 Flash)来显示的.然而,并非所有浏览器都拥有同样的插件.HTML ...
- IDEA 设置背景颜色及字号
intellij IDEA 设置背景颜色 File--> Settings 2. Appearance & Behavior --> Appearance 设置边框背景颜色 3 ...
- bzoj1013高斯消元
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1013 似乎是很明显的高斯消元: 第一次写高斯消元. 代码如下: #include<io ...
- javascript 前段MVVM 框架
http://www.likebin.net/meteorlist.html http://www.cnblogs.com/sskyy/p/3197917.html
- <正则吃饺子>:关于使用powerDesign连接oracle数据库,导出数据表结构(ER图吧)
最近做的项目中,没有完整的数据库表结构图(ER图),自己就根据服务器上oracle数据库和powerdesign整理一份,但是存在两个问题:1.没有把数据库表的相关备注弄下来:2.数据库表中的主外键关 ...
- Hibernate的session.createSQLQuery的几种查询方式
当我们用HQL进行子查询的时候,如select * from Tree where pid in (select id from Tree,此时HIBERANTE就会报错,说什么*号错误之类的.但如果 ...
- 揭开 iOS 7 之 Multipath TCP 的面纱(转)
看到中文圈似乎讨论 iOS 7 的这个特性的还不多,于是我稍微研究了一下这个「Mutlipath TCP」,写点心得.过程是这样的: Olivier Bonaventure 通过 Wireshark ...
- 使用命令把类打成jar包
测试用类 public class Hello { public static void main(String[] args) { System.out.println("hello wo ...
- C#基础:线程之异步回调(委托)
异步回调,什么是异步回调?我是这样理解的,当主线程在执行一段代码的时候,我们用委托执行了一个线程,这个线程要返回一个结果,关键是什么时候返回这个结果,异步回调就是在这个线程执行完成后立即返回这个线程的 ...
- LeetCode: 485 Max Consecutive Ones(easy)
题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...