fzu Problem 2198 快来快来数一数 (快速幂+优化)
题目链接:
题目描述:
给出n个六边形排成一排,a[i]代表i个六边形能组成的生成树个数,设定s[i]等于a[1]+a[2]+a[3]+....+a[i-1]+a[i],问s[n]为多少?
解题思路:
n取值范围[1, 1018],打表内存不够,然后就要考虑快速幂咯!纳尼!!!!快速幂写出来竟然超时,敢信?果然还是见题太少了。(GG)
对于a[n] = 6*a[n-1] - a[n-2],可以很明显看出。
然后求和的时候就要化简一番了,但是并不是很难,最终的公式是:s[n] = 6*s[n-1] - s[n-2] + 5;然后构造矩阵,预处理构造矩阵,跑快速幂即可!!
代码:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <cstring>
using namespace std; #define LL long long
const LL maxn = ;
const LL mod = ;
struct mat
{
LL col, row;
LL p[maxn][maxn];
} pp[]; mat mul (mat a, mat b);
mat pow (LL n, LL res, mat b); int main ()
{
LL n, t;
mat b;
scanf ("%lld", &t);
memset (pp[].p, , sizeof(pp[].p));
memset (b.p, , sizeof(b.p));
pp[].col = pp[].row = b.row = maxn;
b.col = ;
pp[].p[][] = ;
pp[].p[][] = -;
pp[].p[][] = pp[].p[][] = pp[].p[][] = ;
b.p[][] = ;
b.p[][] = ;
b.p[][] = ;
for (int i=; i<; i++)
pp[i] = mul(pp[i-], pp[i-]); while (t --)
{
mat a;
scanf ("%lld", &n);
a = pow(n-, , b);
printf ("%lld\n", a.p[][] % mod);
} return ;
} mat mul (mat a, mat b)
{
mat c;
c.col = a.col;
c.row = b.row;
memset (c.p, , sizeof(c.p));
for (int k=; k<a.row; k++)
for (int i=; i<a.col; i++)
{
if (a.p[i][k] == ) continue;
for (int j=; j<b.row; j++)
{
if (b.p[k][j] == ) continue;
c.p[i][j] = (c.p[i][j] + a.p[i][k] * b.p[k][j] + mod) % mod;
}
}
return c;
} mat pow (LL n, LL res, mat b)
{
while (n)
{
if (n % )
b = mul (b, pp[res]);
res ++;
n /= ;
}
return b;
}
写的好丑!不要喷我 (捂脸逃~~~~)
fzu Problem 2198 快来快来数一数 (快速幂+优化)的更多相关文章
- 省选模拟赛 Problem 3. count (矩阵快速幂优化DP)
Discription DarrellDarrellDarrell 在思考一道计算题. 给你一个尺寸为 1×N1 × N1×N 的长条,你可以在上面切很多刀,要求竖直地切并且且完后每块的长度都是整数. ...
- poj 3734 方块涂色 求红色 绿色方块都为偶数的方案数 (矩阵快速幂)
N个方块排成一列 用红,蓝,绿,黄4种颜色去涂色,求红色方块 和绿色方块个数同时为偶数的 方案数 对10007取余 Sample Input 212Sample Output 2//(蓝,黄)6//( ...
- [BZOJ3209]花神的数论题 组合数+快速幂
3209: 花神的数论题 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2498 Solved: 1129[Submit][Status][Disc ...
- CodeForces - 450B Jzzhu and Sequences —— 斐波那契数、矩阵快速幂
题目链接:https://vjudge.net/problem/CodeForces-450B B. Jzzhu and Sequences time limit per test 1 second ...
- CF954F Runner's Problem(DP+矩阵快速幂优化)
这题是一年前某场我参加过的Education Round codeforces的F题,当时我显然是不会的. 现在看看感觉应该是能做出的. 不扯了写题解: 考虑朴素的DP,在不存在障碍的情况下:f[i] ...
- fzu2198 快来快来数一数
Accept: 204 Submit: 627 Time Limit: 1000 mSec Memory Limit : 65536 KB Problem Description n个六 ...
- P1908 逆序对——树状数组&离散化&快读快写の学习
题目简述: 对于给定的一段正整数序列,逆序对就是序列中 a_i>a_jai>aj 且 i<ji<j 的有序对. 输出序列中逆序对的数目. 知识补充: 树状数组: 这东西就是 ...
- OpenGL快问快答
OpenGL快问快答 本文内容主要来自对(http://www.opengl.org/wiki/FAQ)的翻译,随机加入了本人的观点.与原文相比,章节未必完整,含义未必雷同,顺序未必一致.仅供参考. ...
- FZu Problem 2236 第十四个目标 (线段树 + dp)
题目链接: FZu Problem 2236 第十四个目标 题目描述: 给出一个n个数的序列,问这个序列内严格递增序列有多少个?不要求连续 解题思路: 又遇到了用线段树来优化dp的题目,线段树节点里 ...
随机推荐
- 写入文本文件时“\n”不是回车换行而是个方块“■”的解决方法
用“\n”写入文本文件时,打开文本文件显示的为什么不是回车换行而是个黑方块“■”,但用file()读取时还是认为是一行一行的? 首先在WINDOWS里回车换行是"\r\n"; 而L ...
- HDU 6058 Kanade's sum 二分,链表
Kanade's sum Problem Description Give you an array A[1..n]of length n. Let f(l,r,k) be the k-th larg ...
- Writing a Simple YARN Application 从hadoop生态抽出yarn ,单独使用yarn
Apache Hadoop 2.9.1 – Hadoop: Writing YARN Applications https://hadoop.apache.org/docs/current/hadoo ...
- 使用Qt发送HTTPS请求
示例代码: #include "mainwindow.h" #include "ui_mainwindow.h" #include <QNetworkAc ...
- JSP 用poi 读取Excel
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- UICollectionView基础用法
初始化部分: UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init]; self.myColl ...
- I2S
音频数据传输而制定: Inter—IC Sound : 单线 时钟和数据一条线,时分复用: 标准的I2S总线电缆是由3根串行导线组成的:1根是时分多路复用(简称TDM)数据线:1根是字选择线:1根是时 ...
- adb client, adb server, adbd原理浅析(附带我的操作过程)【转】
本文转载自:http://blog.csdn.net/stpeace/article/details/24933813 adb是什么? adb就是Android调试桥,很形象啊. 先来看adb原理的逻 ...
- HDU3338 Kakuro Extension —— 最大流、方格填数类似数独
题目链接:https://vjudge.net/problem/HDU-3338 Kakuro Extension Time Limit: 2000/1000 MS (Java/Others) ...
- I.MX6 不一定要设置BOOT_MODE进入烧录模式
/************************************************************************* * I.MX6 不一定要设置BOOT_MODE进入 ...