hdu3221 Brute-force Algorithm矩阵快速幂&&a^b%p=a^(b%phi(p)+phi(p))%p b>=phi(p)
来源:互联网 发布:r330打印机清零软件 编辑:程序博客网 时间:2024/04/30 03:22
Brute-force Algorithm
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1530 Accepted Submission(s): 379
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
Source
2009 Asia Shanghai Regional Contest Host by DHU
Recommend
zhuweicong
根据题意f(n)=f(n-1)*f(n-2)
f(1)=a,f(2)=b,f(3)=ab,f(4)=a*b^2,f(5)=a^2*b^3……
发现a的指数序列是1 0 1 1 2 3 5 ……
从第三项开始是斐波那契数列
同理可以发现b的指数序列从第二项开始是斐波那契数列。
由于n很大,通过公式a^b%p=a^(b%phi(p)+phi(p))%p b>=phi(p)我们可以将要求的指数降幂。
求指数利用快速幂即可。
#include<cstdio>#include<iostream>#include<cstdlib>#include<stdio.h>#include<math.h>#define ll long longusing namespace std;const int MAX = 2;ll ai,bi,p;ll mm;typedef struct{long long m[MAX][MAX];} Matrix;Matrix P ={1,1,1,0,};Matrix I ={1,0,0,1,};Matrix matrixmul(Matrix a,Matrix b){int i,j,k;Matrix c;for (i = 0 ; i < MAX; i++)for (j = 0; j < MAX;j++){c.m[i][j] = 0;for (k = 0; k < MAX; 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;}Matrix quickpow(long long n){Matrix m = P, b = I;while (n >= 1){if (n & 1)b = matrixmul(b,m);n = n >> 1;m = matrixmul(m,m);}return b;}__int64 eular(__int64 nn){ __int64 ans=1,i; for(i=2;i*i<=nn;i++) { if(nn%i==0) { nn/=i; ans*=i-1; while(nn%i==0) { nn/=i; ans*=i; } } } if(nn>1) ans*=nn-1; return ans;}ll quickmod(ll a,ll b){ ll res=1; while(b) { if(b&1) res=(res*a)%p; b>>=1; a=(a*a)%p; } return res;}int main(){ int t; ll n; int count=1; scanf("%d",&t); while(t--) { scanf("%I64d%I64d%I64d%I64d",&ai,&bi,&p,&n); mm=eular(p); printf("Case #%d: ",count++); if(n==1) {printf("%I64d\n",ai%p);continue;} else if(n==2) {printf("%I64d\n",bi%p); continue;} else if(n==3) {printf("%I64d\n",ai*bi%p);continue;} // else if(n==4) {printf("%I64d\n",ai*ai*bi*bi*bi%p);continue;} if(p==1) {puts("0");continue;} Matrix g=quickpow(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; // cout<<m1<<" "<<m2<<" "<<mm<<"*"<<endl; num1=quickmod(ai,m1); num2=quickmod(bi,m2); printf("%I64d\n",num1*num2%p); }}
- hdu3221 Brute-force Algorithm矩阵快速幂&&a^b%p=a^(b%phi(p)+phi(p))%p b>=phi(p)
- hdu2837 Calculation a^b%p=a^(b%phi(p)+phi(p))%p
- CH BR13数学(啥?-a^b≡a^b mod phi(p)+phi(p)(mod p)(b>=phi(p))公式)
- p(a|b)=p(ab)/p(b)为什么?
- 为什么phi(p^n)=p^n-p^(n-1)
- A *p=new B;
- 1063 A^B%P
- XDOJ1265 - A^B % P
- hdu 5667 Sequence (矩阵快速幂,a ^ b % c = a ^(b % phi(c) + phi(c)) % c)
- P(AB)与P(B|A)区别!!!!!!
- BSGS(a^x%p=b)
- 条件概率 -- P(A|B) P(B|A)
- 第四周 项目一 改错 s(面积)=sqrt(p*(p-a)*(p-b)*(p-c))
- class A 和 class B: public A中A a; B b; A *p = &a; p->foo();相关题目
- 矩阵A~B <=> 存在可逆矩阵P及Q使PAQ=B
- 为什么A *a=new B不直接写成B b,或者B *p呢?
- a^b === c (mod p)知二求一: p已知
- 解一类x^a = b (mod p)的方程
- ruby基本语法(2)
- 【大学生活】优秀的有趣的博客=来自小魏的修行路
- Eclipse OSGi Services - Tutorial
- oracle中exp,imp的使用详解
- ruby和java
- hdu3221 Brute-force Algorithm矩阵快速幂&&a^b%p=a^(b%phi(p)+phi(p))%p b>=phi(p)
- 如何向nutch索引中添加自己的Field
- 树的直径解法小结 #by Plato
- Deltopia.Deltawalker.v1.9.9.2.Incl.Patch.And.Keymaker-AGAiN
- vs2010安装fixedsys字体,回到vc6的感觉
- Deltopia.Deltawalker.v1.9.9.2.x64.Incl.Patch.And.Keymaker-AGAiN
- ggplot2(二)
- hdu 4417 Super Mario(哈希表+树状树组)
- 用例图中的extend和include