Sequence

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

Problem Description
Let us define a sequence as below

F1=A
F2=B
Fn=C⋅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
 
 
 

cin>>n;
ll ans = ;
for(ll l = ,r;l <= n;l = r + ){
r = n / (n / l);
ans += (r - l + ) * (n / l);
}
cout<<ans<<endl;

除法分块模板

 
[p/n]是整除,一段内的值是相同的,他的整除值有sqrt(p)种。

因此可以将变量分块每块看作常量,对每一块使用矩阵快速幂。
 
 
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define MAX 10
#define INF 0x3f3f3f3f
#define MOD 1000000007
using namespace std;
typedef long long ll; ll p,q;
struct mat{
ll a[MAX][MAX];
}; mat operator *(mat x,mat y)
{
mat ans;
memset(ans.a,,sizeof(ans.a));
for(int i=;i<=;i++){
for(int j=;j<=;j++){
for(int k=;k<=;k++){
ans.a[i][j]+=x.a[i][k]*y.a[k][j]%MOD;
ans.a[i][j]%=MOD;
}
}
}
return ans;
}
mat qMod(ll x,mat a,ll n)
{
mat t;
t.a[][]=q;t.a[][]=p;t.a[][]=x;
t.a[][]=;t.a[][]=;t.a[][]=;
t.a[][]=;t.a[][]=;t.a[][]=;
while(n){
if(n&) a=t*a;
n>>=;
t=t*t;
}
return a;
}
int main()
{
int t,i;
ll a1,a2,x,n;
scanf("%d",&t);
while(t--){
scanf("%lld%lld%lld%lld%lld%lld",&a1,&a2,&p,&q,&x,&n);
if(n==) printf("%lld\n",a1);
else if(n==) printf("%lld\n",a2);
else{
mat a;
a.a[][]=a2;a.a[][]=;a.a[][]=;
a.a[][]=a1;a.a[][]=;a.a[][]=;
a.a[][]=;a.a[][]=;a.a[][]=;
if(x>=n){
for(i=;i<=n;i=x/(x/i)+){
a=qMod(x/i,a,min(n,x/(x/i))-i+);
}
}
else{
for(i=;i<=x;i=x/(x/i)+){
a=qMod(x/i,a,x/(x/i)-i+);
}
a=qMod(,a,n-max(x,2ll));
}
printf("%lld\n",a.a[][]);
}
}
return ;
}

HDU-6395 多校7 Sequence(除法分块+矩阵快速幂)的更多相关文章

  1. 杭电多校第七场 1010 Sequence(除法分块+矩阵快速幂)

    Sequence Problem Description Let us define a sequence as below f1=A f2=B fn=C*fn-2+D*fn-1+[p/n] Your ...

  2. HDU - 6395 Sequence (整除分块+矩阵快速幂)

    定义数列: $\left\{\begin{eqnarray*} F_1 &=& A \\ F_2 &=& B \\ F_n &=& C\cdot{}F_ ...

  3. HDU6395-Sequence 矩阵快速幂+除法分块 矩阵快速幂模板

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门  原题目描述在最下面. Solution ...

  4. [hdu-6395]Sequence 分块+矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6395 因为题目数据范围太大,又存在递推关系,用矩阵快速幂来加快递推. 每一项递推时  加的下取整的数随 ...

  5. poj2778DNA Sequence (AC自动机+矩阵快速幂)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud DNA Sequence Time Limit: 1000MS   Memory ...

  6. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

  7. hdu6395 Sequence(分段矩阵快速幂)

    Sequence 题目传送门 解题思路 可以比较容易的推出矩阵方程,但是由于p/i向下取整的值在变,所以要根据p/i的变化将矩阵分段快速幂.p/i一共有sqrt(p)种结果,所以最多可以分为sqrt( ...

  8. HDU 6395 Sequence(分段矩阵快速幂)题解

    题意: 已知\(A,B,C,D,P,n\)以及 \[\left\{ \begin{aligned} & F_1 = A \\ & F_2 = B\\ & F_n = C*F_{ ...

  9. Sequence( 分块+矩阵快速幂 )

    题目链接 #include<bits/stdc++.h> using namespace std; #define e exp(1) #define pi acos(-1) #define ...

随机推荐

  1. 九度OJ 1075:斐波那契数列 (数字特性)

    时间限制:5 秒 内存限制:32 兆 特殊判题:否 提交:3121 解决:1806 题目描述: 编写一个求斐波那契数列的递归函数,输入n值,使用该递归函数,输出如样例输出的斐波那契数列. 输入: 一个 ...

  2. Spring 简单描述

    摘抄自知乎 建议不要硬着头皮看spring代码,本身的代码800多m,就是不上班开始看也不知道什么时候看完.如果想学学ioc,控制反转这些建议看看jodd项目,比较简练,但是我仍然不建议过多的看这些框 ...

  3. 将QQ登录接口整合到你的网站和如何修改配置

    http://www.phpfensi.com/php/20140727/3998.html 摘要:QQ登录的官方SDK进行了一些修改,使其更加容易的整合到自己的网站上去... 对QQ登录的官方SDK ...

  4. TensorFlow框架(6)之RNN循环神经网络详解

    1. RNN循环神经网络 1.1 结构 循环神经网络(recurrent neural network,RNN)源自于1982年由Saratha Sathasivam 提出的霍普菲尔德网络.RNN的主 ...

  5. Flash+XML前后按钮超酷焦点图

    在线演示 本地下载

  6. c语言学习的第12天

    #include <stdio.h> int main(void) { int *p; int i=5; char ch='A'; p=&i; *p=99; printf(&quo ...

  7. 鸟哥的linux私房菜 - 第5/6/7/9章(在线求助 man page、Linux档案权限与目录配置、Linux档案与目录管理、压缩与打包)

    第五章.在线求助 man page X window与文本模式的切换 Ctrl+Alt+F1~F6:文字接口登入tty1~tty6终端机: Ctrl+Alt+F7:图形接口桌面. 注销当前用户:exi ...

  8. Cocos2d-x中单例的使用

    大家都知道一个程序中只有一个导演类,eg: CCDirector *pDirectory = CCDirector::sharedDirector();//初始化导演类. 可通过 pDirectory ...

  9. 修改ubuntu14.04命令行启动

    方法1: 原来要想默认不进入xwindows,只需编辑文件”/etc/default/grub”, 把 GRUB_CMDLINE_LINUX_DEFAULT=”quiet splash” 改成GRUB ...

  10. html5--2.9新的布局元素(6)-figure/figcaption

    html5--2.9新的布局元素(6)-figure/figcaption 学习要点 了解figure/figcaption元素的语义和用法 通过实例理解figure/figcaption元素的用法 ...