任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6395

Sequence

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 2564    Accepted Submission(s): 999

Problem Description
Let us define a sequence as below

⎧⎩⎨⎪⎪⎪⎪⎪⎪F1F2Fn===ABC⋅Fn−2+D⋅Fn−1+⌊Pn⌋

Your job is simple, for each task, you should output Fn module 109+7.

 
Input
The first line has only one integer T, indicates the number of tasks.

Then, for the next T lines, each line consists of 6 integers, A , B, C, D, P, n.

1≤T≤200≤A,B,C,D≤1091≤P,n≤109

 
Sample Input
2
3 3 2 1 3 5
3 2 2 2 1 4
 
Sample Output
36
24
 
Source
 

题意概括:

给出 A,B,C,D,P,N;

根据函数:

F(1)=A, F(2)=B,  F(i)=C*F(i-2)+D*F(i-1)+p/i;

求 F( N );

解题思路:

一开始看错题目,以为 p/n 为 一个常数,其实题目里的 n 是变量(即题意里的 i );

如果是常数直接构造矩阵,矩阵快速幂跑一波即可,但是这里是是变量。

所以一开始选择了暴力 p/i ;p的范围是 1e9 果断超时。

怎么优化呢?

其实由于整型除法的向下取整,我们可以按 p/i 的种类分成一段一段的,这样大大缩短了暴力区间。

AC code:

 #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#define LL long long
using namespace std;
const int MAXN = ;
const int Mod = 1e9+;
const int NN = ;
int N, A, B, C, D, P;
struct mat
{
LL m[MAXN][MAXN];
}base, ans; mat muti(mat a, mat b)
{
mat res;
memset(res.m, , sizeof(res.m));
for(int i = ; i <= NN; i++)
for(int j = ; j <= NN; j++){
if(a.m[i][j]){
for(int k = ; k <= NN; k++){
res.m[i][k] = (res.m[i][k] + a.m[i][j]*b.m[j][k])%Mod;
}
}
} return res;
} mat qpow(mat a, int n)
{
mat res;
memset(res.m, , sizeof(res.m));
for(int i = ; i <= NN; i++) res.m[i][i] = ;
while(n){
if(n&) res = muti(res, a);
n>>=;
a = muti(a, a);
}
return res;
} int main()
{
int K, T_case;
scanf("%d", &T_case);
while(T_case--){
memset(base.m, , sizeof(base.m));
memset(ans.m, , sizeof(ans.m));
scanf("%d %d %d %d %d %d", &A, &B, &C, &D, &P, &N);
if(N == ){printf("%d\n", A);continue;}
if(N == ){printf("%d\n", B);continue;}
else{
base.m[][] = C;
base.m[][] = D;
base.m[][] = ;
base.m[][] = ;
base.m[][] = P/;
ans.m[][] = A;
ans.m[][] = B;
ans.m[][] = ;
int now = , x, len = , lst;
for(;now <= N; now = lst+){
x = P/now;
if(x != ) lst = min(P/x, N);
else lst = N;
len = lst-now+;
base.m[][] = x;
ans = muti(ans, qpow(base, len));
}
}
printf("%lld\n", ans.m[][]);
} return ;
}

HDU 6395 Sequence 【矩阵快速幂 && 暴力】的更多相关文章

  1. HDU 6395 分段矩阵快速幂 HDU 6386 建虚点+dij

    http://acm.hdu.edu.cn/showproblem.php?pid=6395 Sequence Time Limit: 4000/2000 MS (Java/Others)    Me ...

  2. HDU 5667 Sequence 矩阵快速幂+费马小定理

    题目不难懂.式子是一个递推式,并且不难发现f[n]都是a的整数次幂.(f[1]=a0;f[2]=ab;f[3]=ab*f[2]c*f[1]...) 我们先只看指数部分,设h[n]. 则 h[1]=0; ...

  3. HDU 5667 Sequence 矩阵快速幂

    官方题解: 观察递推式我们可以发现,所有的fi​​都是a的幂次,所以我们可以对f​i​​取一个以a为底的log,g​i​​=log​a​​ f​i​​ 那么递推式变g​i​​=b+c∗g​i−1​​+ ...

  4. HDU.2640 Queuing (矩阵快速幂)

    HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...

  5. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

  6. HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...

  7. HDU - 1005 Number Sequence 矩阵快速幂

    HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...

  8. HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)

    Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...

  9. HDU - 1005 -Number Sequence(矩阵快速幂系数变式)

    A number sequence is defined as follows:  f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...

随机推荐

  1. JS实现图片预览与等比缩放

    案例仅为图片预览功能,省略图片上传步骤,框架为easyui. HTML代码: @*text-align:center;水平居中 vertical-align: middle;display: tabl ...

  2. CentOS 6 安装 MySQL 8.0.+

    1.先查询是否安装MySQL 大多数centos 6 自带 MySQL5.1 命令: rpm -qa|grep mysql 执行: [root@lifan ~]# rpm -qa|grep mysql ...

  3. hdu 3091 Necklace 状态压缩dp *******

    Necklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)Total ...

  4. 【Android】13.0 UI开发(四)——列表控件RecyclerView的横向布局排列实现

    1.0 新建项目,由于ListView的局限性,RecyclerView是一种很好取代ListView的控件,可以灵活实现多种布局. 2.0 新建项目RecyclerviewTest,目录如下: 3. ...

  5. 在vue中安装使用vux

    最近因为的工作的原因在弄vue,从后端弄到前端之前一直用js,现在第一次接触vue感觉还挺有意思的,就是自己太菜了,这个脑子呀....不太够用.....页面设计用了一个叫vux的东西,vux可以提供一 ...

  6. 文字编辑器FCKeditor 简介以及基本配置和使用方法

    什么是FCKeditor FCKeditor是一个专门使用在网页上属于开放源代码的所见即所得文字编辑器.它志于轻量化,不需要太复杂的安装步骤即可使用.它可和PHP.JavaScript.ASP.ASP ...

  7. 关于输入框在谷歌浏览器 ie 浏览器中 黄色背景的去除

    谷歌有自己对input 的填充色 加上下面的css 就可以了 input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white ins ...

  8. 获取当前时间CTime

    std::string getcurtime(){ USES_CONVERSION; CTime z_CurTime; CString z_TimeStr; z_CurTime = CTime::Ge ...

  9. Studying TCP's Throughput and Goodput using NS

    Studying TCP's Throughput and Goodput using NS What is Throughput Throughput is the amount of data r ...

  10. C++ 无名对象

    http://blog.sina.com.cn/s/blog_5f0e13360100bxlj.html 可以直接调用构造函数产生无名对象. 例如,下面的代码在函数fn()中,创建了一个无名对象: c ...