HDU6395 Sequence(矩阵快速幂+数论分块)
题意:
F(1)=A,F(2)=B,F(n)=C*F(n-2)+D*F(n-1)+P/n
给定ABCDPn,求F(n) mod 1e9+7
思路:
P/n在一段n里是不变的,可以数论分块,再在每一段里用矩阵快速幂
debug了一下午。。
坑点:
1.数论分块的写法要注意,已更新
2.矩阵乘法在赋值回去的时候记得模一下
3.矩阵相乘不可逆,注意看一下
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<stack>
#include<queue>
#include<deque>
#include<set>
#include<vector>
#include<map>
#include<functional> #define fst first
#define sc second
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define lc root<<1
#define rc root<<1|1
#define lowbit(x) ((x)&(-x)) using namespace std; typedef double db;
typedef long double ldb;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PI;
typedef pair<ll,ll> PLL; const db eps = 1e-;
const int mod = 1e9+;
const int maxn = 2e6+;
const int maxm = 2e6+;
const int inf = 0x3f3f3f3f;
const db pi = acos(-1.0);
void prt(ll b[][]){
for(int i = ; i <= ; i++){
for(int j = ; j <= ; j++){
printf("%3I64d ", b[i][j]);
}
printf("\n");
}
return;
}
void mtpl(ll a[][], ll b[][], ll s[][]){
ll tmp[][];
mem(tmp, );
for(int i = ; i <= ; i++){
for(int j = ; j <= ; j++){ for(int k = ; k <= ; k++){
tmp[i][j] += (a[i][k]*b[k][j])%mod;
}
}
}
for(int i = ; i <= ; i++){
for(int j = ; j <= ; j++){
s[i][j] = tmp[i][j]%mod;
}
}
return;
}ll a, b, c, d, p, n;
ll B[][];
void fp(ll n, ll tmp){
ll A[][];
mem(A, );
A[][] = d;
A[][] = c;
A[][] = ;
A[][] = tmp;
A[][] = ;
//prt(A);
while(n){
if(n&) mtpl(A,B,B);
mtpl(A,A,A);
n>>=;
}
return;
}
int main() {
int T;
scanf("%d",&T);
while(T--){ scanf("%I64d %I64d %I64d %I64d %I64d %I64d", &a, &b, &c, &d, &p, &n); if(n==){
printf("%I64d\n", a);
continue;
}
if(n==){
printf("%I64d\n", b);
continue;
} mem(B, );
for(int i = ; i <= ; i++)B[i][i]=;
B[][] = b;
B[][] = a;
B[][] = ;
ll l , r;
for(l = , r = ; l <= n; l = r + ){
if(p/l) r = min(p, p/(p/l));
else r = n;
ll tmp = p/l;
fp(r-l+,tmp);
} printf("%I64d\n", B[][]);
}
return ;
}
/*
4
1 2 1 2 5 3
1 2 1 2 3 5
1 2 3 4 7 4
2 3 3 3 3 4 */
HDU6395 Sequence(矩阵快速幂+数论分块)的更多相关文章
- hdu6395 (矩阵快速幂+分块)
Online Judge Online Exercise Online Teaching Online Contests Exercise Author F.A.Q Hand In Hand Onli ...
- HDU5950 Recursive sequence (矩阵快速幂加速递推) (2016ACM/ICPC亚洲赛区沈阳站 Problem C)
题目链接:传送门 题目: Recursive sequence Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total ...
- HDU5950 Recursive sequence —— 矩阵快速幂
题目链接:https://vjudge.net/problem/HDU-5950 Recursive sequence Time Limit: 2000/1000 MS (Java/Others) ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- UVA - 10689 Yet another Number Sequence 矩阵快速幂
Yet another Number Sequence Let’s define another number sequence, given by the foll ...
- HDU6395-Sequence 矩阵快速幂+除法分块 矩阵快速幂模板
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog Problem:Portal传送门 原题目描述在最下面. Solution ...
- Yet Another Number Sequence——[矩阵快速幂]
Description Everyone knows what the Fibonacci sequence is. This sequence can be defined by the recur ...
- 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 ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
随机推荐
- keuectl命令
Kubernetes命令行 kubectl用于运行Kubernetes集群命令的管理工具 kubectl命令行语法 kubectl [command] [TYPE] [NAME] [flags] co ...
- Windows 7 + Tiny Linux 4.19 + XFS + Vmware Workstation 15 (PRO) 下篇dockerの奥义
美好的事物总是来自不易,而我是一个docker新手 从以上开场,请各位follow me站在一个初学者的角度,一步一步用最简单的视角审视docker和它的真实存在 上篇预告:Windows 7 + T ...
- Docker入门之快速安装和卸载使用Centos7
一.检查内核版本 注意:Docker要求操作系统必须是64位,如果使用的Centos内核版本为3.10以上 执行命令:uname -r 二.安装依赖软件包 执行命令:yum install -y y ...
- Go和Java的性能对比,真的如此吗?
前两天我看到了一篇文章,测试Java和Go和Python的性能,其测试内容是一个排序,排序一亿次,然后看那个语言耗时最短,我先贴一下这个文章的测试结果,Java竟然比Go快了一倍不止,Go不是号称接近 ...
- wannafly camp day1
题目描述: 恬恬的生日临近了.宇扬给她准备了一个大 蛋糕. 正如往常一样,宇扬在蛋糕上插了nnn支蜡烛,并把蛋糕分为mmm个区域.因为某种原因,他必须把第iii根蜡烛插在第aia\_iai个区域或第 ...
- 基于selenium爬取京东
爬取iphone 注意:browser对象会发生变化,当对当前网页做任意操作时 import time from selenium import webdriver from selenium.web ...
- ancconda创建爬虫项目
# 安装 conda env list conda create -n <envname> conda activate <envname> conda install scr ...
- Django 导入配置文件
from django.conf import settings
- hdu4841
今天天气确实很好! 接下来是圆桌问题,顺便做个vector容器的笔记方便以后复习.嘿嘿 Problem Description圆桌上围坐着2n个人.其中n个人是好人,另外n个人是坏人.如果从第一个人开 ...
- 「 深入浅出 」集合Map
系列文章: 「 深入浅出 」java集合Collection和Map 「 深入浅出 」集合List 「 深入浅出 」集合Set 前面已经介绍完了Collection接口下的集合实现类,今天我们来介绍M ...