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)的更多相关文章

  1. HDU1013,1163 ,2035九余数定理 快速幂取模

    1.HDU1013求一个positive integer的digital root,即不停的求数位和,直到数位和为一位数即为数根. 一开始,以为integer嘛,指整型就行吧= =(too young ...

  2. CSU - 1556 Jerry&#39;s trouble(高速幂取模)

    [题目链接]:click here [题目大意]:计算x1^m+x2^m+..xn^m(1<=x1<=n)( 1 <= n < 1 000 000, 1 <= m < ...

  3. HDU 1061 Rightmost Digit --- 快速幂取模

    HDU 1061 题目大意:给定数字n(1<=n<=1,000,000,000),求n^n%10的结果 解题思路:首先n可以很大,直接累积n^n再求模肯定是不可取的, 因为会超出数据范围, ...

  4. hdu 1097 A hard puzzle 快速幂取模

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1097 分析:简单题,快速幂取模, 由于只要求输出最后一位,所以开始就可以直接mod10. /*A ha ...

  5. hdu 4506 小明系列故事——师兄帮帮忙【幂取模乱搞】

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

  6. HDU 1061.Rightmost Digit-规律题 or 快速幂取模

    Rightmost Digit Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  7. 题解报告:hdu 1061 Rightmost Digit(快速幂取模)

    Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...

  8. HDU 4704 Sum 超大数幂取模

    很容易得出答案就是2^(n-1) 但是N暴大,所以不可以直接用幂取模,因为除法操作至少O(len)了,总时间会达到O(len*log(N)) 显然爆的一塌糊涂 套用FZU1759的模板+顺手写一个大数 ...

  9. 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)

    先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...

  10. UVa 11582 Colossal Fibonacci Numbers! 【大数幂取模】

    题目链接:Uva 11582 [vjudge] watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fil ...

随机推荐

  1. Objective-C基础笔记(5)Protocol

    Protocol简单来说就是一系列方法的列表,当中声明的方法能够被不论什么类实现.这中模式一般称为代理(delegation)模式. 在IOS和OS X开发中,Apple採用了大量的代理模式来实现MV ...

  2. Android layoutInflate.inflate 方法具体解释,removeView()错误解决

    错误: The specified child already has a parent. You must call removeView(). 解答: 这个错误非常直白,就是你viewGroup. ...

  3. C# - 线程操作

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  4. java操作oracle空间信息介绍

    转自:http://www.cdtarena.com/javapx/201307/9088.html sde是Spatial Database Engine简写,中文全称:空间数据库引擎. SDE是一 ...

  5. asp.net 生成xml文件 与 asp生成xml文件

    一.asp.net 生成xml文件 webservice方式,调用接口: public XmlDocument List() { XmlDocument doc = new XmlDocument() ...

  6. QT+vs2010下改变可执行程序的图标

    原地址:http://blog.163.com/tijijun@126/blog/static/6820974520134209457308/ 在解决方案下面的工程里,点击右键 ->选择[添加( ...

  7. MFC获取rgb图像数据后动态显示及保存图片的方法

    该情况可用于视频通信中获取的位图数据回放显示或显示摄像头捕获的本地图像 第一种方法 #include<vfw.h> 加载 vfw32.lib  链接库 //---------------- ...

  8. EF连接MySQL数据Web.Config配置

    EF连接MySQL数据Web.Config配置 <?xml version="1.0" encoding="utf-8"?> <configu ...

  9. webdynpro 组件重用 传值问题

    组件zwd1,需要调用组件zwd2的时候,zwd2组件控制器中需要定义一个方法,定义所要传输的参数,并且该方法需要定义为interface方法. 组件zwd1可以通过代码向导调用组件zwd2,的该方法 ...

  10. Python Base64转码解码

    Python Base64 提供了好几种方法例如: encode, decode, encodestring, decodestring, b64encode, b64decode, standard ...