hdu 3221 Brute-force Algorithm
2013-05-18 15:39
253 查看
Brute-force Algorithm
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1825 Accepted Submission(s): 444
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
分析:见hdu4550
#include<cstdio> #include<cmath> #define ll __int64 ll a,b,p,n; struct Matrix{ ll v[2][2]; }; Matrix A={0,1,1,1},B={1,0,0,1}; ll euler(ll x){ ll i,ans=x,t=(ll)sqrt(x*10.0); for(i=2;i<=t;i++)if(x%i==0){ ans=ans/i*(i-1); while(x%i==0)x/=i; } if(x>1)ans=ans/x*(x-1); return ans; } ll quickpow(ll x,ll n){ ll ans=1; while(n){ if(n&1)ans=ans*x%p; n>>=1; x=x*x%p; } return ans; } Matrix mul(Matrix m1,Matrix m2,ll M){ int i,j,k; Matrix c; for(i=0;i<2;i++)for(j=0;j<2;j++){ c.v[i][j]=0; for(k=0;k<2;k++)c.v[i][j]+=(m1.v[i][k]*m2.v[k][j])%M; c.v[i][j]%=M; } return c; } Matrix Mpow(int n,ll M){ Matrix x=A,c=B; while(n>=1){ if(n&1)c=mul(c,x,M); n>>=1; x=mul(x,x,M); } return c; } int main(){ int T,ca=1,i; scanf("%d",&T); while(T--){ scanf("%I64d%I64d%I64d%I64d",&a,&b,&p,&n); printf("Case #%d: ",ca++); 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%p)*(b%p))%p);continue;} ll phi=euler(p),f1=0,f2=1,f3,aa,bb;bool j=0; for(i=4;i<=n;i++){ f3=f1+f2; if(f3>=phi){f3%=phi;j=1;break;} f1=f2,f2=f3; } if(j){ Matrix res=Mpow(n-3,phi); f1=res.v[1][0],f2=res.v[1][1],f3=f1+f2; aa=f2%phi+phi,bb=f3%phi+phi; } else {aa=f3,bb=f1+f2;} printf("%I64d\n",(quickpow(a,aa)*quickpow(b,bb))%p); } return 0; }
相关文章推荐
- hdu 3221 Brute-force Algorithm(高速幂取模,矩阵高速幂求fib)
- HDU 3221 Brute-force Algorithm(指数降幂公式)
- HDU 3221 Brute-force Algorithm
- hdu 3221 Brute-force Algorithm
- HDU 3221 Brute-force Algorithm
- hdu 3221 Brute-force Algorithm 指数循环节
- HDU 3221 Brute-force Algorithm
- HDU 3221 Brute-force Algorithm
- HDU 3221 Brute-force Algorithm(欧拉公式降幂)
- HDU 3221Brute-force Algorithm(降幂公式 神似hdu4549)
- hdu 3221 Brute-force Algorithm
- hdu 3221 Brute-force Algorithm(快速幂取模,矩阵快速幂求fib)
- Brute-force Algorithm [HDU 3221]
- hdu 3221 Brute-force Algorithm
- hdu 3221 Brute-force Algorithm
- hdu 3221 Brute-force Algorithm
- SPOJ 5152 Brute-force Algorithm EXTREME && HDU 3221 Brute-force Algorithm 快速幂,快速求斐波那契数列,欧拉函数,同余 难度:1
- HDU 3221 Brute-force Algorithm
- HDU 3221 Brute-force Algorithm(矩阵求fibnacci,指数取模)
- Hdu 3221 Brute-force Algorithm (矩阵 欧拉定理降幂)