fzu Problem 2198 快来快来数一数 (快速幂+优化)
2024-08-27 21:15:03
题目链接:
题目描述:
给出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;
}
写的好丑!不要喷我 (捂脸逃~~~~)
最新文章
- java实现甘特图的2种方法:SwiftGantt和Jfree (转)
- webapi 通过dynamic 接收可变参数
- DataGrid--多记录CRUD
- codeforce好地方啊 Bear and Elections *
- python和pywin32实现窗口查找、遍历和点击
- 检测到有潜在危险的 Request.Form 值。 说明: ASP.NET 在请求中检测到包含潜在危险的数据
- WP7 MD5加密
- PHP CLI模式开发(转)
- 优酷播放器demo
- hive left outer join的问题
- ORACLE用户表空间使用情况查询
- jquery 中dataTable显示加载中,图片或文字
- java基础(持续整理)
- ECMAScript 原始类型
- js中,for循环里面放ajax,ajax访问不到变量以及每次循环获取不到数据问题总结
- AI,大数据,复杂系统 最精 40本大书单
- JS实现简单的运行代码 &; 侧边广告
- ios蓝牙自定义快捷键
- HashMap的长度为什么要是2的n次方
- 09 ORM 多表操作,创建表,添加记录
热门文章
- 理解和使用WPF 验证机制
- (白书训练计划)UVa 11572 Unique Snowflakes(窗体滑动法)
- 使用Genymotion调试出现错误INSTALL_FAILED_CPU_ABI_INCOMPATIBLE解决的方法
- adb常用命令整理
- locate和grep命令
- bzoj2436: [Noi2011]Noi嘉年华
- java 简单贪吃蛇
- Silverlight中使用MVVM(4)
- UIFont 字体样式 [UIFont fontWithName~];
- angularJS ng-if的用法