HDU3221Brute-force Algorithm(矩阵快速幂&&指数降幂)
来源:互联网 发布:淘宝特价民族风女装 编辑:程序博客网 时间:2024/04/30 14:50
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3221
Problem Description
Professor Brute is not good at algorithm design. Once he was asked to solve a path finding problem. He worked on it for several days and finally came up with the following algorithm:
Any fool but Brute knows that the function “funny” will be called too many times. Brute wants to investigate the number of times the function will be called, but he is too lazy to do it.
Now your task is to calculate how many times the function “funny” will be called, for the given a, b and n. Because the answer may be too large, you should output the answer module by P.
Any fool but Brute knows that the function “funny” will be called too many times. Brute wants to investigate the number of times the function will be called, but he is too lazy to do it.
Now your task is to calculate how many times the function “funny” will be called, for the given a, b and n. Because the answer may be too large, you should output the answer module by P.
Input
There are multiple test cases. The first line of the input contains an integer T, meaning the number of the test cases.
For each test cases, there are four integers a, b, P and n in a single line.
You can assume that 1≤n≤1000000000, 1≤P≤1000000, 0≤a, b<1000000.
For each test cases, there are four integers a, b, P and n in a single line.
You can assume that 1≤n≤1000000000, 1≤P≤1000000, 0≤a, b<1000000.
Output
For each test case, output the answer with case number in a single line.
Sample Input
33 4 10 34 5 13 53 2 19 100
Sample Output
Case #1: 2Case #2: 11Case #3: 12
从第三项开始 a的指数为斐波那契数列
由于比较大 我们根据公式a^b%p=a^(b%phi(p)+phi(p))%p b>=phi(p)对指数进行降幂
然后再快速幂取模即可得到结果
对p=1的情况进行特判;
#include <iostream>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;const int N = 2;struct matrax{ LL m[N][N];};matrax E={ 1,0, 0,1};matrax A={ 1,1, 1,0};LL a,b,p,n,mm;matrax multi(matrax a,matrax b){ matrax c; for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ c.m[i][j]=0; for(int k=0;k<N;k++) c.m[i][j]+=a.m[i][k]*b.m[k][j]; if(c.m[i][j]>mm) c.m[i][j]=c.m[i][j]%mm+mm; } } return c;}matrax pow(matrax a,LL n){ matrax ans=E,p=a; while(n){ if(n&1){ ans=multi(ans,p); n--; } n>>=1; p=multi(p,p); } return ans;}LL phi(LL n){ LL rea=n; for(int i=2;i*i<=n;i++){ if(n%i==0){ rea=rea-rea/i; while(n%i==0) n/=i; } } if(n>1) rea=rea-rea/n; return rea;}LL quick_mod(LL a,LL b){ LL ans=1; while(b){ if(b&1){ ans=ans*a%p; b--; } b>>=1; a=a*a%p; } return ans;}int main(){ int t=1,cas; cin>>cas; while(cas--){ cin>>a>>b>>p>>n; mm=phi(p); printf("Case #%d: ",t++); if(n==1){ printf("%I64d\n",a%p); continue; } if(n==2){ printf("%I64d\n",b%p); continue; } if(n==3){ printf("%I64d\n",a*b%p); continue; } if(p==1){ puts("0"); continue; } matrax g=pow(A,n-2); LL m1,m2,num1,num2; m1=g.m[1][0]; m2=g.m[0][1]+g.m[1][1]; if(m2>mm) m2=m2%mm+mm; num1=quick_mod(a,m1); num2=quick_mod(b,m2); printf("%I64d\n",num1*num2%p); } return 0;}#include <iostream>#include <cstring>#include <cstdio>using namespace std;typedef long long LL;const int N = 2;struct matrax{ LL m[N][N];};matrax E={ 1,0, 0,1};matrax A={ 1,1, 1,0};LL a,b,p,n,mm;matrax multi(matrax a,matrax b){ matrax c; for(int i=0;i<N;i++){ for(int j=0;j<N;j++){ c.m[i][j]=0; for(int k=0;k<N;k++) c.m[i][j]+=a.m[i][k]*b.m[k][j]; if(c.m[i][j]>mm) c.m[i][j]=c.m[i][j]%mm+mm; } } return c;}matrax pow(matrax a,LL n){ matrax ans=E,p=a; while(n){ if(n&1){ ans=multi(ans,p); n--; } n>>=1; p=multi(p,p); } return ans;}LL phi(LL n){ LL rea=n; for(int i=2;i*i<=n;i++){ if(n%i==0){ rea=rea-rea/i; while(n%i==0) n/=i; } } if(n>1) rea=rea-rea/n; return rea;}LL quick_mod(LL a,LL b){ LL ans=1; while(b){ if(b&1){ ans=ans*a%p; b--; } b>>=1; a=a*a%p; } return ans;}int main(){ int t=1,cas; cin>>cas; while(cas--){ cin>>a>>b>>p>>n; mm=phi(p); printf("Case #%d: ",t++); if(n==1){ printf("%I64d\n",a%p); continue; } if(n==2){ printf("%I64d\n",b%p); continue; } if(n==3){ printf("%I64d\n",a*b%p); continue; } if(p==1){ puts("0"); continue; } matrax g=pow(A,n-2); LL m1,m2,num1,num2; m1=g.m[1][0]; m2=g.m[0][1]+g.m[1][1]; if(m2>mm) m2=m2%mm+mm; num1=quick_mod(a,m1); num2=quick_mod(b,m2); printf("%I64d\n",num1*num2%p); } return 0;}
0 0
- HDU3221Brute-force Algorithm(矩阵快速幂&&指数降幂)
- HDU 3221 Brute-force Algorithm(指数降幂公式)
- hdu 3221 Brute-force Algorithm(快速幂取模,矩阵快速幂求fib)
- hdu3221 Brute-force Algorithm 裴波那锲数列 矩阵快速幂
- Hdu 3221 Brute-force Algorithm (矩阵 欧拉定理降幂)
- HDU 3221 Brute-force Algorithm(矩阵求fibnacci,指数取模)
- hdu3221 Brute-force Algorithm矩阵快速幂&&a^b%p=a^(b%phi(p)+phi(p))%p b>=phi(p)
- HDU 3221Brute-force Algorithm(降幂公式 神似hdu4549)
- BC #80 C Sequence(指数矩阵快速幂)
- HDU 5667 矩阵快速幂关于指数的递推
- HDU 5895 矩阵快速幂+欧拉降幂公式+指数循环节
- hdu 3221 Brute-force Algorithm 指数循环节
- HDU 3221 Brute-force Algorithm(欧拉公式降幂)
- 快速矩阵快速幂
- 找数组等分点&&指数快速幂
- 【HDU 5895】【指数循环节 矩阵 快速幂 逆元 推公式】Mathematician QSC 由递推式推公式
- HDU 5895 Mathematician QSC (矩阵快速幂 + 逆元应用 + 指数循环节 + 欧拉函数)
- 转移矩阵+矩阵快速幂
- java链接MySql数据库
- VB中GetCurrentDirectory()获取路径异常
- 技术工程师的能力与目标
- 如何删除网站信息负面
- Cocos2d-x v3.1 坐标系统(五)
- HDU3221Brute-force Algorithm(矩阵快速幂&&指数降幂)
- UICollectionView 为每个section 添加headerview
- 学生信息管理系统系列一:问题总结
- 大二结束了,马上大三了,开始写点博客来记录最后的大学学习时光。
- 情非得已:涅槃情事
- CMemDC类的使用
- __declspec(dllexport) 与 .def
- 在VC6.0中使用stl在Debug模式下编译导致的warning
- 解决VTune错误.../lib64/libstdc++.so.6: version `GLIBCXX_3.4.14' not found (required by ...)