HDU3221Brute-force Algorithm(矩阵快速幂&&指数降幂)
2014-07-01 20:23
369 查看
题目链接: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.
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.
Output
For each test case, output the answer with case number in a single line.
Sample Input
3
3 4 10 3
4 5 13 5
3 2 19 100
Sample Output
Case #1: 2
Case #2: 11
Case #3: 12
写出前几项为 a,b,ab,ab^2,a^2b^3,a^3,b^5;
从第三项开始 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
;
};
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
;
};
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;
}
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.
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.
Output
For each test case, output the answer with case number in a single line.
Sample Input
3
3 4 10 3
4 5 13 5
3 2 19 100
Sample Output
Case #1: 2
Case #2: 11
Case #3: 12
写出前几项为 a,b,ab,ab^2,a^2b^3,a^3,b^5;
从第三项开始 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
;
};
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
;
};
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;
}
相关文章推荐
- hdu3802 矩阵快速幂&特征方程&降幂
- hdu 2276 Kiki & Little Kiki 2(矩阵快速幂)
- HDU 3221 Brute-force Algorithm(矩阵求fibnacci,指数取模)
- HDU 2276 Kiki & Little Kiki 2(矩阵快速幂)
- 再论斐波那契数列(矩阵&快速幂)
- hdu 4291 A Short problem 打表找规律&矩阵快速幂
- 【递推&矩阵快速幂】hdu 2604
- HDU 5171 GTY's birthday gift (矩阵快速幂)
- HDU2276 - Kiki & Little Kiki 2(矩阵快速幂)
- 多校第九场:贪心+矩阵快速幂中间优化+线性递推&线段树递推
- [BZOJ1898] Swamp 沼泽鳄鱼 && 矩阵快速幂
- POJ 3233 Matrix Power Series (矩阵&快速等比数列求和取模)
- HDU 4549 M斐波那契数列 (费马小定理降幂&矩阵快速幂)
- hdu3658&nbsp;递推&nbsp;矩阵快速幂
- hdu 5015 233 Matrix 2014 ACM/ICPC Asia Regional Xi'an Online 矩阵快速幂
- HDU 2276 Kiki & Little Kiki 2 矩阵快速幂
- Hdu 3221 Brute-force Algorithm (矩阵 欧拉定理降幂)
- 利用矩阵&快速幂解决斐波那契数列相关题目小结
- POJ 3233 && NYOJ 298 Matrix Power Series(矩阵快速幂)
- SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1