hdu 3221 Brute-force Algorithm (矩阵快速幂+指数循环节)

题目链接

题意:给出了一段伪代码。分析得知其实就是f[1]= a,f[2] = b,f[n]=f[n-1] * f[n-2]

思路:一眼题,和hdu4549很类似hdu4549解题报告

不同的是这道题中p不一定是质数(其实不是也无所谓啊...hdu4549只不过是因为1E9+7是指数,又用费马小定理化简了一下,这道理%phi(p)即可)

还有这道题让我知道了

首先我们知道指数循环节公式,也就是所谓的降幂公式为:**a^x = a^(x mod phi(c)+phi(c)) (mod c) x>=phi(c),(ps:后面的限制条件,在x**

括号里的话是错误的。只有当x<phi(c)的时候,这个公式才成立。

这道题就是反例,不加判断会wa。

/* ***********************************************
Author :111qqz
Created Time :Sun 30 Oct 2016 11:46:33 PM CST
File Name :code/hdu/3221.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6LL a,b,p,n;
 7LL mod;
 8LL euler( LL x)
 9{
10    LL ret = 1;
11    for ( LL i = 2 ; i*i <= x;  i++)
12    {
13	if (x%i==0)
14	{
15	    x/=i;
16	    ret*=(i-1);
17	    while (x%i==0)
18	    {
19		x/=i;
20		ret*=i;
21	    }
22	}
23    }
24    if (x>1) ret*=(x-1);
25    return ret;
26}
1struct Mat
2{
3    LL mat[2][2];
4    void clear()
5    {
6	ms(mat,0);
7    }
8}M,M1;
 1Mat operator * (Mat a,Mat b)
 2{
 3    Mat res;
 4    res.clear();
 5    for ( int i = 0 ; i < 2 ; i++)
 6	for ( int j = 0 ; j < 2 ; j++)
 7	    for ( int k = 0 ; k < 2 ; k++)
 8	    {
 9		res.mat[i][j] = (res.mat[i][j] + a.mat[i][k]*b.mat[k][j]);
10		if (res.mat[i][j]>=mod)
11		    res.mat[i][j] = res.mat[i][j] % mod + mod;
12	    }
13    return res;
14}
15Mat operator ^ (Mat a,LL b)
16{
17    Mat res;
18    res.clear();
19    for ( int i = 0 ; i < 2 ; i++) res.mat[i][i] = 1;
20    while (b>0)
21    {
22	if (b&1) res = res * a;
23	b = b >> 1LL;
24	a = a * a;
25    }
26    return res;
27}
28void init()
29{
30    M.clear();
31    M.mat[0][1] =  M.mat[1][0] = M.mat[1][1] = 1;
32    M1.clear();
33    M1.mat[0][0] = 0 ;
34    M1.mat[1][0] = 1;
35}
36LL ksm( LL a,LL b,LL k)
37{
38    LL res = 1LL;
39    while (b>0)
40    {
41	if (b&1) res = (res * a) % k;
42	b = b >> 1LL;
43	a = ( a * a ) % k;
44    }
45    return res;
46}
47int main()
48{
49	#ifndef  ONLINE_JUDGE 
50	freopen("code/in.txt","r",stdin);
51  #endif
52	int T;
53	int cas = 0 ;
54	cin>>T;
55	while (T--)
56	{
57	    scanf("%lld%lld%lld%lld",&a,&b,&p,&n);
58	    printf("Case #%d: ",++cas);
59	    if (n==1)
60	    {
61		printf("%lld\n",a%p);
62		continue;
63	    }
64	    if (n==2)
65	    {
66		printf("%lld\n",b%p);
67		continue;
68	    }
69	    init();
70	    mod = euler(p);
71	    Mat ans;
72	    ans.clear();
73	    ans = (M^(n-2))*M1;
74	    LL x = ans.mat[0][0];
75	    LL y = ans.mat[1][0];
76	    LL ret = ksm(a,x,p)*ksm(b,y,p)%p;
77	    printf("%lld\n",ret);
78	   // printf("Case #%d: %lld\n",++cas,ret);
79	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}