Sequence
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 1338 Accepted Submission(s): 487
Problem Description
Let us define a sequence as below
f(n) = d * f(n - 1) + c * f(n - 2) + p / n;
f(1) = A , f(2) = B;
0 <= a,b,c,d <= 10^9;
1 <= p,n <= 10^9;
1 <= T <= 20;
Your job is simple, for each task, you should output Fn module 109+7.
题意 :
求如上斐波那契的变形公式的某20项的答案。
由于P/ n这个值会变,所以只能分块进行矩阵快速幂。
对n进行分段 ,每一段内的 floor(P/ni) 都是一定的,这样就可以对每一段直接跑矩阵快速幂了。
//hdu 6359 time 2000ms memory 262144k
// f(n) = d * f(n - 1) + c * f(n - 2) + p / n;
// f(1) = A , f(2) = B;
// 0 <= a,b,c,d <= 10^9;
// 1 <= p,n <= 10^9;
// 1 <= T <= 20;
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <list>
#define ll long long
#define int64 __int64
#define ui unsigned int
using namespace std;
const ll mod = 1e9 + 7;
const int maxn = 100009;
struct matrix {
ll m [5] [5];
} a , b;
ll A , B , C , D , P , n;
ll f [maxn];
matrix operator * (matrix a , matrix b) {
matrix ans;
int nn = 3;//两个矩阵最大的行或列方便套版
for (int i = 0; i < nn; i++) {
for (int j = 0; j < nn; j++) {
ans.m [i] [j] = 0;
for (int k = 0; k < nn; k++)
ans.m [i] [j] += ( a.m [i] [k] * b.m [k] [j] ) % mod;
ans.m [i] [j] %= mod;
}
}
return ans;
}
matrix power_mod(ll n) {
while (n) {
if (n & 1) {
b = a * b;
}
n >>= 1;
a = a * a;
}
return b;
}
//b为答案矩阵,a为构造的矩阵,初始化矩阵
void matrix_init(ll f2 , ll f1 , ll pn) {
a.m [0] [0] = D;
a.m [0] [1] = C;
a.m [0] [2] = 1;
a.m [1] [0] = 1;
a.m [1] [1] = 0;
a.m [1] [2] = 0;
a.m [2] [0] = 0;
a.m [2] [1] = 0;
a.m [2] [2] = 1;
b.m [0] [0] = f2;
b.m [1] [0] = f1;
b.m [2] [0] = pn;
}
struct xx {
ll r , l , pn;
} interval [maxn];
void init() {
f [1] = A;
f [2] = B;
for (int i = 3; i <= 100000; i++) {
f [i] = ( C * f [i - 2] % mod + D * f [i - 1] % mod + P / i ) % mod;
}
}
int main() {
//cin.sync_with_stdio(false);//降低cin,cout时间
int t;
while (~scanf("%d",&t)) {
while (t--) {
scanf("%d %d %d %d %d %d" , &A , &B , &C , &D , &P , &n);
init();
//前 100000直接暴力算出
if (n <= 100000) {
printf("%lld\n" , f [n]);
} else {
int counter = 0;
ll r = 1 , l = 2;
ll pn = 1;
//对P分块
while (P / l > 100000) {
interval [counter].r = P / r;
interval [counter].l = P / l + 1;
interval [counter++].pn = pn;
r++ , l++ , pn++;
}
interval [counter].r = P / r;
interval [counter].l = P / l + 1;
interval [counter++].pn = pn;
ll index = interval [counter - 1].l;
ll f1 = f [index - 1];
ll f2 = f [index - 2];
f2 = ( C * f2 % mod + D * f1 % mod + P / index ) % mod;
//区间矩阵快速幂
for (int i = counter - 1; i >= 0; i--) {
matrix_init(f2 , f1 , interval [i].pn);
//n在区间内直接break答案
if (interval [i].l <= n && interval [i].r >= n) {
matrix tp = power_mod(n - interval [i].l);
f2 = tp.m [0] [0];
break;
}
matrix tp = power_mod(interval [i].r - interval [i].l);
ll f12 = tp.m [0] [0];
ll f11 = tp.m [1] [0];
if (i != 0) {
ll temp = f12;
f12 = ( C * f11 % mod + D * f12 % mod + interval [i - 1].pn ) % mod;
f11 = temp;
}
f2 = f12;
f1 = f11;
}
//若P < n后面的 P / n 都是0直接进行矩阵快速幂
if (P < n) {
ll temp = f2;
f2 = ( C * f1 % mod + D * f2 % mod ) % mod;
f1 = temp;
matrix_init(f2 , f1 , 0);
matrix tp = power_mod(n - ( P + 1 ));
f2 = tp.m [0] [0];
}
printf("%lld\n" , f2 % mod);
}
}
}
return 0;
}
//9215647 1236548 456781 15648 45612387 100001