MYF

HDU 5667 Segment

题目链接

HDU 5667

BestCoder #80C 中文题面

解题方法:欧拉函数 + 矩阵快速幂

题目分析

题目大意

给规律,求f(n)%p,看不懂题意的去上面的传送门吧。。。

解析

看到这个递推式很容易想到对指数进行矩阵快速幂相加,取到f(n)的指数,再进行快速幂的运算,感觉比较坑的是之前没有接触过欧拉函数的题,今天算是见到了。

欧拉函数(盗链):

其中欧拉函数的代码:

1
2
3
4
5
6
7
8
9
10
ll euler(ll x) {
ll res = x;
for (ll i = 2; i <= x / i; i++)
if (x % i == 0) {
res = res / i * (i - 1);
while(x % i == 0) x /= i;
}
if (x > 1) res = res / x * (x - 1);
return res;
}

快速幂:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/***快速幂开始***/
ll mult_mod(ll a,ll b,ll mod){
return (a*b-(ll)(a/(long double)mod*b+1e-3)*mod+mod)%mod;
}
ll pow_mod(ll x, ll n, ll mod) { //x^n%c
if(n == 1)return x % mod;
x %= mod;
ll tmp = x;
ll ret = 1;
while(n) {
if(n & 1) ret = mult_mod(ret, tmp, mod);
tmp = mult_mod(tmp, tmp, mod);
n >>= 1;
}
return ret;
}
/***快速幂结束***/

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <iostream>
#include <algorithm>
#define Memset(a,val) memset(a,val,sizeof(a))
#define PI acos(-1)
#define rt(n) (i == n ? '\n' : ' ')
#define hi printf("Hi----------\n")
#define IN freopen("input.txt","r",stdin);
#define OUT freopen("output.txt","w",stdout);
#define debug(x) cout<<"Debug : ---"<<x<<"---"<<endl;
#pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long ll;
using namespace std;
const int maxn=100000+5;
const int INF=0x3f3f3f3f;
const double eps=1e-8;
typedef long long mytype;
const int SZ=105;
long long M=1000000007;
long long quickpow(long long a, long long b)
{
if(b < 0) return 0;
long long ret = 1;
a %= M;
for (; b; b >>= 1, a = (a * a) % M)
if (b & 1)
ret = (ret * a) % M;
return ret;
}
long long inv(long long a)
{
return quickpow(a,M-2);
}
struct mat
{
int n,m;
mytype a[SZ][SZ];
void init()
{
memset(a,0,sizeof(a));
}
mat(int n=SZ,int m=SZ):n(n),m(m) {}
mat unit()
{
mat t(n,n);
t.init();
for (int i=0; i<n; i++)
t.a[i][i]=1;
return t;
}
mytype *operator [](int n)
{
return *(a+n);
}
mat operator +(const mat &b)
{
mat t(n,m);
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
t.a[i][j]=(a[i][j]+b.a[i][j]+M)%M;
return t;
}
mat operator -(const mat &b)
{
mat t(n,m);
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
t.a[i][j]=(a[i][j]-b.a[i][j]+M)%M;
return t;
}
mat operator *(const mat &b)
{
mat t(n,b.m);
for(int i=0; i<n; i++)
for(int j=0; j<b.m; j++)
{
t.a[i][j]=0;
for(int k=0; k<m; k++)
t.a[i][j]=(t.a[i][j]+(a[i][k]*b.a[k][j])%M)%M;
}
return t;
}
mat operator *(const mytype &b)
{
mat t(n,m);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
t.a[i][j]=a[i][j]*b%M;
return t;
}
mat operator /(const mytype &b)
{
mat t(n,m);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
t.a[i][j]=a[i][j]*inv(b)%M;
return t;
}
mat operator !()
{
mat t(n,m);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
t.a[i][j]=a[j][i];
return t;
}
friend mat quickpow(mat a, mytype b)
{
if(b<0) return a.unit();
mat ret=a.unit();
for (; b; b>>=1,a=a*a)
if (b&1)
ret=ret*a;
return ret;
}
void in()
{
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
scanf("%lld",&a[i][j]);
}
void out()
{
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
printf("%lld%c",a[i][j]," \n"[j==m-1]);
}
};

ll euler(ll x) {
ll res = x;
for (ll i = 2; i <= x / i; i++) if (x % i == 0) {
res = res / i * (i - 1);
while(x % i == 0) x /= i;
}
if (x > 1) res = res / x * (x - 1);
return res;
}

ll mult_mod(ll a,ll b,ll mod){
return (a*b-(ll)(a/(long double)mod*b+1e-3)*mod+mod)%mod;
}


ll pow_mod(ll x, ll n, ll mod) { //x^n%c
if(n == 1)return x % mod;
x %= mod;
ll tmp = x;
ll ret = 1;
while(n) {
if(n & 1) ret = mult_mod(ret, tmp, mod);
tmp = mult_mod(tmp, tmp, mod);
n >>= 1;
}
return ret;
}
int main(){
int t;
ll n,a,b,c,p;
scanf("%d",&t);
while (t--) {
scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&c,&p);
M=euler(p);
mat m=mat(3,3);
mat tmp=mat(3,3);
m.a[0][0]=0;m.a[0][1]=1;m.a[0][2]=0;
m.a[1][0]=1;m.a[1][1]=c;m.a[1][2]=1;
m.a[2][0]=0;m.a[2][1]=0;m.a[2][2]=1;
tmp=quickpow(m,n-2);
mat o=mat(3,1);
o.a[0][0]=0;
o.a[1][0]=b;
o.a[2][0]=b;
mat ans=tmp*o;
cout<<pow_mod(a, ans.a[1][0]+M, p)<<endl;
}
}