首页 技术 正文
技术 2022年11月16日
0 收藏 718 点赞 3,550 浏览 1800 个字

http://acm.hdu.edu.cn/showproblem.php?pid=4549

f[0] = a^1*b^0%p,f[1] = a^0*b^1%p,f[2] = a^1*b^1%p…..f[n] = a^fib[n-1] * b^fib[n-2]%p。

这里p是质数,且a,p互素,那么我们求a^b%p,当b非常大时要对b降幂。

由于a,p互素,那么由费马小定理知a^(p-1)%p = 1。令b = k*(p-1) + b’。a^b%p = a^(k*(p-1)+b’)%p = a^(k*(p-1))%p * a^b’%p

= a^b’%p。

当中p’ = b%(p-1)。

那么上题的fib[n-1]和fib[n-2]能够经过矩阵高速幂对(p-1)取模,实现降幂以后再用高速幂求得。

事实上a^b%c较一般的形式是a^(b%phi[c]+phi[c])%c (b>=phi[c]),在c是素数或a与c互素的时候能够简化为a^(b%(c-1))%c。

纳闷了一中午,结果把fib数列弄错了。f[0] = 0, f[1] = 1,f[n] = f[n-1]+f[n-2](n >= 2)。第0项是0,sad….

http://www.narutoacm.com/archives/a-pow-b-mod-m/

对大神表示深深的ym.

#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;
const int mod = 1000000007;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,n;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])%(mod-1);
}
}
return ans;
}matrix pow_matrix(matrix m, LL n)
{
matrix ans;
ans.init();
while(n)
{
if(n&1)
ans = mul_matrix(ans,m);
m = mul_matrix(m,m);
n >>= 1;
}
return ans;
}LL pow_mod(LL a, LL b)
{
LL res = 1;
a = a%mod;
while(b)
{
if(b&1)
res = res*a%mod;
a = a*a%mod;
b >>= 1;
}
return res;
}int main()
{while(~scanf("%lld %lld %lld",&a,&b,&n))
{
m.mat[0][0] = m.mat[0][1] = m.mat[1][0] = 1;
m.mat[1][1] = 0;
if(n == 0)
{
printf("%lld\n",a%mod);
continue;
}
if(n == 1)
{
printf("%lld\n",b%mod);
continue;
}
else if(n == 2)
{
printf("%lld\n",a*b%mod);
continue;
}
matrix ans = pow_matrix(m,n);
LL res = pow_mod(a,ans.mat[1][1]) * pow_mod(b,ans.mat[0][1]) %mod;
printf("%lld\n",res);
}
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,944
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,470
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,284
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,100
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,730
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,766