hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)
http://acm.hdu.edu.cn/showproblem.php?pid=3221
一晚上搞出来这么一道题。。Mark。
给出这么一个程序。问funny函数调用了多少次。
我们定义数组为所求:f[1] = a,f[2] = b, f[3] = f[2]*f[3]......f[n] = f[n-1]*f[n-2]。相应的值表示也可为a^1*b^0%p。a^0*b^1%p,a^1*b^1%p,.....a^fib[n-3]*b^fib[n-2]%p。即a,b的指数从n=3以后与fib数列一样。
由于n非常大。fib[n]也想当大。
a^fib[n]%p能够利用a^fib[n]%p = a^(fib[n]%phi[p]+phi[p])%p进行降幂,条件时fib[n]>=phi[p]。求fib[n]%phi[p]能够构造矩阵。利用矩阵高速幂求fib[n]%phi[p]。
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
#define C 240
#define S 20
using namespace std;
const int maxn = 110; struct matrix
{
LL mat[3][3];
void init()
{
memset(mat,0,sizeof(mat));
for(int i = 0; i < 2; i++)
mat[i][i] = 1;
}
} m; LL a,b,p,n,phi_p;
LL fib[10000000]; //phi[p]
LL Eular(LL num)
{
LL res = num;
for(int i = 2; i*i <= num; i++)
{
if(num%i == 0)
{
res -= res/i;
while(num%i == 0)
num /= i;
}
}
if(num > 1)
res -= res/num;
return res;
}
//矩阵相乘
matrix mul_matrix(matrix x, matrix y)
{
matrix ans;
memset(ans.mat,0,sizeof(ans.mat));
for(int i = 0; i < 2; i++)
{
for(int k = 0; k < 2; k++)
{
if(x.mat[i][k] == 0) continue;
for(int j = 0; j < 2; j++)
{
ans.mat[i][j] = (ans.mat[i][j] + x.mat[i][k]*y.mat[k][j])%phi_p;
}
}
}
return ans;
}
//a^t%phi_p
LL pow_matrix(LL t)
{
matrix a,b;
a.mat[0][0] = a.mat[0][1] = a.mat[1][0] = 1;
a.mat[1][1] = 0;
b.init();
while(t)
{
if(t&1)
b = mul_matrix(a,b);
a = mul_matrix(a,a);
t >>= 1;
}
return b.mat[0][0];
}
//a^t%p
LL pow(LL a, LL t)
{
LL res = 1;
a %= p;
while(t)
{
if(t&1)
res = res*a%p;
a = a*a%p;
t >>= 1;
}
return res;
}
//a^fib[t]%p转化为a^(fib[t]%phi[p]+phi[p])%p,fib[t] >= phi[p]。
LL solve(LL a, LL t)
{
fib[0] = 1;
fib[1] = 1;
int i;
for(i = 2; i <= t; i++)
{
fib[i] = fib[i-1] + fib[i-2];
if(fib[i] >= phi_p)
break;
}
if(i <= t) //当满足条件fib[t] >= phi[p]时,进行降幂
{
LL c = pow_matrix(t) + phi_p;
return pow(a,c);
}
else
return pow(a,fib[t]);
} int main()
{
int test;
scanf("%d",&test);
for(int item = 1; item <= test; item++)
{
scanf("%lld %lld %lld %lld",&a,&b,&p,&n);
printf("Case #%d: ",item);
if(n == 1)
{
printf("%lld\n",a%p);
continue;
}
if(n == 2)
{
printf("%lld\n",b%p);
continue;
}
if(n == 3)
{
printf("%lld\n",a*b%p);
continue;
}
if(p == 1)
{
printf("0\n");
continue;
}
phi_p = Eular(p);
LL res = solve(a,n-3)*solve(b,n-2)%p;
printf("%lld\n",res);
}
return 0;
}
hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)的更多相关文章
- HDU1013,1163 ,2035九余数定理 快速幂取模
1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...
- CSU - 1556 Jerry's trouble(高速幂取模)
[题目链接]:click here [题目大意]:计算x1^m+x2^m+..xn^m(1<=x1<=n)( 1 <= n < 1 000 000, 1 <= m < ...
- HDU 1061 Rightmost Digit --- 快速幂取模
HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...
- hdu 1097 A hard puzzle 快速幂取模
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...
- hdu 4506 小明系列故事——师兄帮帮忙【幂取模乱搞】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4506 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1061.Rightmost Digit-规律题 or 快速幂取模
Rightmost Digit Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 题解报告:hdu 1061 Rightmost Digit(快速幂取模)
Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...
- HDU 4704 Sum 超大数幂取模
很容易得出答案就是2^(n-1) 但是N暴大,所以不可以直接用幂取模,因为除法操作至少O(len)了,总时间会达到O(len*log(N)) 显然爆的一塌糊涂 套用FZU1759的模板+顺手写一个大数 ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】
题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...
随机推荐
- 将DataTable 存到一个集合当中
将DataTable 存到一个集合中 此做法来自:http://www.codeproject.com/Articles/692832/Simple-way-of-using-SQL-DataTabl ...
- 经典集合 与 IQueryable集合 的差别
经典集合 与 IQueryable集合 的差别 经典集合与IQueryable 集合存在本质的区别,经典结合是在内存中开辟一片区域用来存储数据,而IQueryable集合是延迟加载的集合,只有在用到的 ...
- MySQL推出Applier,可实时复制数据到Hadoop
MySQL复制操作可以将数据从一个MySQL服务器(主)复制到其他的一个或多个MySQL服务器(从).试想一下,如果从服务器不再局限为一个MySQL服务器,而是其他任何数据库服务器或平台,并且复制事件 ...
- 大白痴学习webmagic
摘要 webmagic 学习 从头 刚刚开始学,很多东西可能理解错了,还请各位指教 一些基本类: Request:包含要爬行的url和一些附加信息,是Page的一个成员变量 主要成员变量 String ...
- ubuntu 14.04 使用极点五笔输入法
相比12.04在外观改变不是非常大,但当中细节有些许变化,特别输入法非常不大好用,为此,我们使用fcitx输入法,使用我喜欢的五笔拼音,安装步骤例如以下: 方法一: 最新的方法非常easy: 安装14 ...
- HDU 5046 Airport(DLX反复覆盖)
HDU 5046 Airport 题目链接 题意:给定一些机场.要求选出K个机场,使得其它机场到其它机场的最大值最小 思路:二分+DLX反复覆盖去推断就可以 代码: #include <cstd ...
- Swift - String与NSString的区别,以及各自的使用场景
String是Swift里新增加的类型,它与原来的NSString可以很方便地互相转换.但在实际开发中,我们该如何选择? 1,能使用String类型就尽量使用String类型,原因如下: (1)现在C ...
- java http 分段下载
http://www.iteye.com/topic/1136815 http://www.iteye.com/topic/1128336 http://blog.chinaunix.net/uid- ...
- Web前端,高性能优化
高性能HTML 一.避免使用iframe iframe也叫内联frame,可将一个HTML文档嵌入另一个HTML文档中. iframe的好处是,嵌入的文档独立于父文档,通常也借此使浏览器模拟多线程.缺 ...
- 杭电 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...