推出公式后, 用欧拉定理。
欧拉定理的内容是:如果a和n互质,那么aφ(n)=1(mod n);对于任意a, n和较大的b>= φ(n) ,有ab=aφ(n)+b mod φ(n)(mod n)。证明
Brute-force Algorithm
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2364 Accepted Submission(s): 590
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.
![](https://i-blog.csdnimg.cn/blog_migrate/462c90bb7e8823915f915441230de1d7.jpeg)
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
3 3 4 10 3 4 5 13 5 3 2 19 100
Sample Output
Case #1: 2 Case #2: 11 Case #3: 12
Source
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define prt(k) cout<<#k" = "<<k<<endl
typedef long long ll;
#include <cmath>
ll a, b, P, n;
ll euler(ll n)
{
int m=sqrt(n+0.5);
ll ret = n;
for(int i=2;i<=m;i++)
{
if(n%i==0)
{
ret = ret / i * (i - 1);
while(n%i==0) n/=i;
}
}
if(n>1) ret = ret / n * (n-1);
return ret;
}
ll phi;
struct Mat
{
ll a[3][3];
Mat() { memset(a,0,sizeof a); }
void go()
{
a[0][0]=1; a[0][1]=1;
a[1][0]=1; a[1][1]=0;
}
Mat operator * (Mat b)
{
Mat c;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j] % phi) % phi;
return c;
}
};
ll powMod(ll a, ll b, ll m)
{
ll ret = 1;
for(a%=m;b>0;b>>=1,a=a*a%m) if(b&1) ret=ret*a%m;
return ret;
}
Mat pow(Mat a, int n)
{
Mat ret;
for(int i=0;i<2;i++) ret.a[i][i] = 1;
for(;n;n>>=1,a=a*a) if(n&1) ret=ret*a;
return ret;
}
ll fib[424];
ll mia, mib;
ll gao()
{
if(n==1) return a;
if(n==2) return b;
if(n<=35)
{
return powMod(a, fib[n-3], P) * powMod(b, fib[n-2], P) % P;
}
ll x = powMod(a, phi+mia, P);
ll y = powMod(b, phi+mib, P);
return x * y % P;
}
int main()
{
fib[0] = 1;fib[1] = 1;
for(int i=2;i<=160;i++) fib[i]=fib[i-1]+fib[i-2];
int re; cin>>re; int ca=1;
while(re--)
{
cin>>a>>b>>P>>n;
phi = euler(P);
prt(phi);
Mat mat;
mat.go();
if(n>=2) mat=pow(mat,n-2);
mia = mat.a[0][1];
mib = mat.a[0][0];
ll ans = gao();
ans %= P;
printf("Case #%d: %I64d\n", ca++, ans);
}
}
推出公式后,用欧拉定理。
欧拉定理的内容是:如果a和n互质,那么aφ(n)=1(mod n);对于任意a, n和较大的b>= φ(n) ,有ab=aφ(n)+b mod φ(n)(mod n)。证明
Brute-force Algorithm
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2364 Accepted Submission(s): 590
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.
![](https://i-blog.csdnimg.cn/blog_migrate/462c90bb7e8823915f915441230de1d7.jpeg)
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
3 3 4 10 3 4 5 13 5 3 2 19 100
Sample Output
Case #1: 2 Case #2: 11 Case #3: 12
Source